mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-04 01:32:11 +00:00
Fix NPE on IndexQuery with source and version.
Original Pull Request #1894 Closes #1893
This commit is contained in:
parent
1c8e0e03d3
commit
36b449c385
@ -700,15 +700,17 @@ class RequestFactory {
|
|||||||
String indexName = index.getIndexName();
|
String indexName = index.getIndexName();
|
||||||
IndexRequest indexRequest;
|
IndexRequest indexRequest;
|
||||||
|
|
||||||
if (query.getObject() != null) {
|
Object queryObject = query.getObject();
|
||||||
String id = StringUtils.isEmpty(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId();
|
|
||||||
|
if (queryObject != null) {
|
||||||
|
String id = StringUtils.isEmpty(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
|
||||||
// If we have a query id and a document id, do not ask ES to generate one.
|
// If we have a query id and a document id, do not ask ES to generate one.
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
indexRequest = new IndexRequest(indexName).id(id);
|
indexRequest = new IndexRequest(indexName).id(id);
|
||||||
} else {
|
} else {
|
||||||
indexRequest = new IndexRequest(indexName);
|
indexRequest = new IndexRequest(indexName);
|
||||||
}
|
}
|
||||||
indexRequest.source(elasticsearchConverter.mapObject(query.getObject()).toJson(), Requests.INDEX_CONTENT_TYPE);
|
indexRequest.source(elasticsearchConverter.mapObject(queryObject).toJson(), Requests.INDEX_CONTENT_TYPE);
|
||||||
} else if (query.getSource() != null) {
|
} else if (query.getSource() != null) {
|
||||||
indexRequest = new IndexRequest(indexName).id(query.getId()).source(query.getSource(),
|
indexRequest = new IndexRequest(indexName).id(query.getId()).source(query.getSource(),
|
||||||
Requests.INDEX_CONTENT_TYPE);
|
Requests.INDEX_CONTENT_TYPE);
|
||||||
@ -719,7 +721,8 @@ class RequestFactory {
|
|||||||
|
|
||||||
if (query.getVersion() != null) {
|
if (query.getVersion() != null) {
|
||||||
indexRequest.version(query.getVersion());
|
indexRequest.version(query.getVersion());
|
||||||
VersionType versionType = retrieveVersionTypeFromPersistentEntity(query.getObject().getClass());
|
VersionType versionType = retrieveVersionTypeFromPersistentEntity(
|
||||||
|
queryObject != null ? queryObject.getClass() : null);
|
||||||
indexRequest.versionType(versionType);
|
indexRequest.versionType(versionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -754,15 +757,16 @@ class RequestFactory {
|
|||||||
|
|
||||||
IndexRequestBuilder indexRequestBuilder;
|
IndexRequestBuilder indexRequestBuilder;
|
||||||
|
|
||||||
if (query.getObject() != null) {
|
Object queryObject = query.getObject();
|
||||||
String id = StringUtils.isEmpty(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId();
|
if (queryObject != null) {
|
||||||
|
String id = StringUtils.isEmpty(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
|
||||||
// If we have a query id and a document id, do not ask ES to generate one.
|
// If we have a query id and a document id, do not ask ES to generate one.
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
indexRequestBuilder = client.prepareIndex(indexName, type, id);
|
indexRequestBuilder = client.prepareIndex(indexName, type, id);
|
||||||
} else {
|
} else {
|
||||||
indexRequestBuilder = client.prepareIndex(indexName, type);
|
indexRequestBuilder = client.prepareIndex(indexName, type);
|
||||||
}
|
}
|
||||||
indexRequestBuilder.setSource(elasticsearchConverter.mapObject(query.getObject()).toJson(),
|
indexRequestBuilder.setSource(elasticsearchConverter.mapObject(queryObject).toJson(),
|
||||||
Requests.INDEX_CONTENT_TYPE);
|
Requests.INDEX_CONTENT_TYPE);
|
||||||
} else if (query.getSource() != null) {
|
} else if (query.getSource() != null) {
|
||||||
indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource(),
|
indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource(),
|
||||||
@ -774,7 +778,8 @@ class RequestFactory {
|
|||||||
|
|
||||||
if (query.getVersion() != null) {
|
if (query.getVersion() != null) {
|
||||||
indexRequestBuilder.setVersion(query.getVersion());
|
indexRequestBuilder.setVersion(query.getVersion());
|
||||||
VersionType versionType = retrieveVersionTypeFromPersistentEntity(query.getObject().getClass());
|
VersionType versionType = retrieveVersionTypeFromPersistentEntity(
|
||||||
|
queryObject != null ? queryObject.getClass() : null);
|
||||||
indexRequestBuilder.setVersionType(versionType);
|
indexRequestBuilder.setVersionType(versionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1640,12 +1645,13 @@ class RequestFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private VersionType retrieveVersionTypeFromPersistentEntity(Class<?> clazz) {
|
private VersionType retrieveVersionTypeFromPersistentEntity(@Nullable Class<?> clazz) {
|
||||||
|
|
||||||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = elasticsearchConverter
|
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = elasticsearchConverter
|
||||||
.getMappingContext();
|
.getMappingContext();
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
ElasticsearchPersistentEntity<?> persistentEntity = clazz != null ? mappingContext.getPersistentEntity(clazz)
|
||||||
|
: null;
|
||||||
|
|
||||||
VersionType versionType = null;
|
VersionType versionType = null;
|
||||||
|
|
||||||
|
@ -3576,6 +3576,22 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
assertThat(foundEntity.getScriptedRate()).isEqualTo(84.0);
|
assertThat(foundEntity.getScriptedRate()).isEqualTo(84.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #1893
|
||||||
|
@DisplayName("should index document from source with version")
|
||||||
|
void shouldIndexDocumentFromSourceWithVersion() {
|
||||||
|
|
||||||
|
String source = "{\n" + //
|
||||||
|
" \"answer\": 42\n" + //
|
||||||
|
"}";
|
||||||
|
IndexQuery query = new IndexQueryBuilder() //
|
||||||
|
.withId("42") //
|
||||||
|
.withSource(source) //
|
||||||
|
.withVersion(42L) //
|
||||||
|
.build();
|
||||||
|
|
||||||
|
operations.index(query, IndexCoordinates.of(indexNameProvider.indexName()));
|
||||||
|
}
|
||||||
|
|
||||||
// region entities
|
// region entities
|
||||||
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
||||||
@Setting(shards = 1, replicas = 0, refreshInterval = "-1")
|
@Setting(shards = 1, replicas = 0, refreshInterval = "-1")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user