Example usage for org.springframework.data.elasticsearch.core.mapping ElasticsearchPersistentEntity getIndexType

List of usage examples for org.springframework.data.elasticsearch.core.mapping ElasticsearchPersistentEntity getIndexType

Introduction

In this page you can find the example usage for org.springframework.data.elasticsearch.core.mapping ElasticsearchPersistentEntity getIndexType.

Prototype

String getIndexType();

Source Link

Usage

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java

@Override
public <T> boolean putMapping(Class<T> clazz) {
    ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
    PutMappingRequestBuilder requestBuilder = client.admin().indices()
            .preparePutMapping(persistentEntity.getIndexName()).setType(persistentEntity.getIndexType());

    try {//from   www  . ja  v  a2s. c  o  m
        XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(),
                persistentEntity.getIdProperty().getFieldName(), elasticsearchConverter.getMappingContext());
        return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged();
    } catch (Exception e) {
        throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
    }
}

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java

@Override
public <T> T queryForObject(GetQuery query, Class<T> clazz) {
    ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
    GetResponse response = client//from  w w w  .ja  v a2 s . co m
            .prepareGet(persistentEntity.getIndexName(), persistentEntity.getIndexType(), query.getId())
            .execute().actionGet();
    return mapResult(response.getSourceAsString(), clazz);
}

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java

@Override
public <T> long count(SearchQuery query, Class<T> clazz) {
    ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
    CountRequestBuilder countRequestBuilder = client.prepareCount(persistentEntity.getIndexName())
            .setTypes(persistentEntity.getIndexType());
    if (query.getQuery() != null) {
        countRequestBuilder.setQuery(query.getQuery());
    }//from w w  w. j a va 2s . c  o m
    return countRequestBuilder.execute().actionGet().getCount();
}

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java

@Override
public <T> String delete(Class<T> clazz, String id) {
    ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
    return delete(persistentEntity.getIndexName(), persistentEntity.getIndexType(), id);
}

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java

@Override
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
    ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
    client.prepareDeleteByQuery(persistentEntity.getIndexName()).setTypes(persistentEntity.getIndexType())
            .setQuery(deleteQuery.getQuery()).execute().actionGet();
}

From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java

@Override
public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) {
    int startRecord = 0;
    ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
    String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName()
            : persistentEntity.getIndexName();
    String type = isNotBlank(query.getType()) ? query.getType() : persistentEntity.getIndexType();

    Assert.notNull(indexName, "No 'indexName' defined for MoreLikeThisQuery");
    Assert.notNull(type, "No 'type' defined for MoreLikeThisQuery");
    Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");

    MoreLikeThisRequestBuilder requestBuilder = client.prepareMoreLikeThis(indexName, type, query.getId());

    if (query.getPageable() != null) {
        startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
        requestBuilder.setSearchSize(query.getPageable().getPageSize());
    }//from  www .j a va2  s.  c o m
    requestBuilder.setSearchFrom(startRecord);

    if (isNotEmpty(query.getSearchIndices())) {
        requestBuilder.setSearchIndices(toArray(query.getSearchIndices()));
    }
    if (isNotEmpty(query.getSearchTypes())) {
        requestBuilder.setSearchTypes(toArray(query.getSearchTypes()));
    }
    if (isNotEmpty(query.getFields())) {
        requestBuilder.setField(toArray(query.getFields()));
    }
    if (isNotBlank(query.getRouting())) {
        requestBuilder.setRouting(query.getRouting());
    }
    if (query.getPercentTermsToMatch() != null) {
        requestBuilder.setPercentTermsToMatch(query.getPercentTermsToMatch());
    }
    if (query.getMinTermFreq() != null) {
        requestBuilder.setMinTermFreq(query.getMinTermFreq());
    }
    if (query.getMaxQueryTerms() != null) {
        requestBuilder.maxQueryTerms(query.getMaxQueryTerms());
    }
    if (isNotEmpty(query.getStopWords())) {
        requestBuilder.setStopWords(toArray(query.getStopWords()));
    }
    if (query.getMinDocFreq() != null) {
        requestBuilder.setMinDocFreq(query.getMinDocFreq());
    }
    if (query.getMaxDocFreq() != null) {
        requestBuilder.setMaxDocFreq(query.getMaxDocFreq());
    }
    if (query.getMinWordLen() != null) {
        requestBuilder.setMinWordLen(query.getMinWordLen());
    }
    if (query.getMaxWordLen() != null) {
        requestBuilder.setMaxWordLen(query.getMaxWordLen());
    }
    if (query.getBoostTerms() != null) {
        requestBuilder.setBoostTerms(query.getBoostTerms());
    }

    SearchResponse response = requestBuilder.execute().actionGet();
    return mapResults(response, clazz, query.getPageable());
}

From source file:org.springframework.data.elasticsearch.core.MappingBuilder.java

private static void mapParentField(Class clazz,
        MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
        XContentBuilder xContentBuilder) throws IOException {
    ParentId parentId = getParentIdAnnotation(clazz.getDeclaredFields());
    if (parentId != null) {
        ElasticsearchPersistentEntity<?> parentPersistentEntity = mappingContext
                .getPersistentEntity(parentId.type());
        if (parentPersistentEntity != null) {
            xContentBuilder.startObject(PARENT_FIELD)
                    .field(PARENT_FIELD_TYPE, parentPersistentEntity.getIndexType()).endObject();
        }//ww  w .  j  av  a 2 s .co m
    }
}