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

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

Introduction

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

Prototype

@Nullable
P getIdProperty();

Source Link

Document

Returns the id property of the PersistentEntity .

Usage

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

private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {

    if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {

        ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
        PersistentProperty<?> idProperty = persistentEntity.getIdProperty();

        // Only deal with String because ES generated Ids are strings !
        if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
            persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
        }/*from w  ww .  java  2  s . com*/
    }
}

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 {// w  w  w.  ja va 2 s .c om
        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);
    }
}