mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-28 14:52:20 +00:00
Enhancement for random Id of elasticsearch if an entity does not have one
This commit is contained in:
parent
e54b263be9
commit
c689470cac
@ -261,8 +261,16 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
String type = isBlank(query.getType())?
|
||||
retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getType();
|
||||
|
||||
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName,type,query.getId())
|
||||
.setSource(objectMapper.writeValueAsString(query.getObject()));
|
||||
IndexRequestBuilder indexRequestBuilder = null;
|
||||
|
||||
if(query.getId() != null){
|
||||
indexRequestBuilder = client.prepareIndex(indexName,type,query.getId());
|
||||
}else {
|
||||
indexRequestBuilder = client.prepareIndex(indexName,type);
|
||||
}
|
||||
|
||||
indexRequestBuilder.setSource(objectMapper.writeValueAsString(query.getObject()));
|
||||
|
||||
if(query.getVersion() != null){
|
||||
indexRequestBuilder.setVersion(query.getVersion());
|
||||
indexRequestBuilder.setVersionType(EXTERNAL);
|
||||
|
@ -55,10 +55,13 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
||||
public ID getId(T entity) {
|
||||
ElasticsearchPersistentProperty id = entityMetadata.getIdProperty();
|
||||
try {
|
||||
return (ID) BeanWrapper.create(entity, null).getProperty(id);
|
||||
if(id != null){
|
||||
return (ID) BeanWrapper.create(entity, null).getProperty(id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("ID could not be resolved", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
Loading…
x
Reference in New Issue
Block a user