DATAES-228 Fix id handling for indexing

When an id is set in the IndexQuery object, use it without requiring an
@Document annotation. When it is not, try to resolve it from the object
when it has @Document and @Id annotations.
This commit is contained in:
Mark Janssen 2016-08-07 15:41:15 +02:00 committed by Artur Konczak
parent 53b587101d
commit 2b60128390

View File

@ -28,6 +28,7 @@ import static org.springframework.util.CollectionUtils.isEmpty;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.ListenableActionFuture;
@ -102,6 +103,7 @@ import org.springframework.util.Assert;
* @author Mason Chan * @author Mason Chan
* @author Young Gu * @author Young Gu
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Janssen
*/ */
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware { public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
@ -1013,13 +1015,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
IndexRequestBuilder indexRequestBuilder = null; IndexRequestBuilder indexRequestBuilder = null;
if (query.getObject() != null) { if (query.getObject() != null) {
String entityId = null; String id = isBlank(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId();
if (isDocument(query.getObject().getClass())) {
entityId = getPersistentEntityId(query.getObject());
}
// 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 (query.getId() != null && entityId != null) { if (id != null) {
indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()); indexRequestBuilder = client.prepareIndex(indexName, type, id);
} else { } else {
indexRequestBuilder = client.prepareIndex(indexName, type); indexRequestBuilder = client.prepareIndex(indexName, type);
} }