Skip to content

Commit 7314bfc

Browse files
committed
DATAES-972 - BeforeConvertCallback should be called before index query is built.
Originap PR: #555 (cherry picked from commit 9804334)
1 parent 5909c19 commit 7314bfc

File tree

7 files changed

+310
-160
lines changed

7 files changed

+310
-160
lines changed

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ public <T> T save(T entity, IndexCoordinates index) {
147147
Assert.notNull(entity, "entity must not be null");
148148
Assert.notNull(index, "index must not be null");
149149

150-
IndexQuery query = getIndexQuery(entity);
151-
index(query, index);
150+
T entityAfterBeforeConvert = maybeCallbackBeforeConvert(entity, index);
152151

153-
// suppressing because it's either entity itself or something of a correct type returned by an entity callback
154-
@SuppressWarnings("unchecked")
155-
T castResult = (T) query.getObject();
156-
return castResult;
152+
IndexQuery query = getIndexQuery(entityAfterBeforeConvert);
153+
doIndex(query, index);
154+
155+
T entityAfterAfterSave = maybeCallbackAfterSave(entityAfterBeforeConvert, index);
156+
157+
return entityAfterAfterSave;
157158
}
158159

159160
@Override
@@ -192,6 +193,20 @@ public <T> Iterable<T> save(T... entities) {
192193
return save(Arrays.asList(entities));
193194
}
194195

196+
@Override
197+
public String index(IndexQuery query, IndexCoordinates index) {
198+
199+
maybeCallbackBeforeConvertWithQuery(query, index);
200+
201+
String documentId = doIndex(query, index);
202+
203+
maybeCallbackAfterSaveWithQuery(query, index);
204+
205+
return documentId;
206+
}
207+
208+
public abstract String doIndex(IndexQuery query, IndexCoordinates indexCoordinates);
209+
195210
@Override
196211
@Nullable
197212
public <T> T get(String id, Class<T> clazz) {
@@ -261,11 +276,38 @@ public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOp
261276
return bulkIndex(queries, bulkOptions, getIndexCoordinatesFor(clazz));
262277
}
263278

279+
@Override
280+
public final List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions,
281+
IndexCoordinates index) {
282+
283+
Assert.notNull(queries, "List of IndexQuery must not be null");
284+
Assert.notNull(bulkOptions, "BulkOptions must not be null");
285+
286+
return bulkOperation(queries, bulkOptions, index);
287+
}
288+
264289
@Override
265290
public void bulkUpdate(List<UpdateQuery> queries, Class<?> clazz) {
266291
bulkUpdate(queries, getIndexCoordinatesFor(clazz));
267292
}
268293

294+
public List<IndexedObjectInformation> bulkOperation(List<?> queries, BulkOptions bulkOptions,
295+
IndexCoordinates index) {
296+
297+
Assert.notNull(queries, "List of IndexQuery must not be null");
298+
Assert.notNull(bulkOptions, "BulkOptions must not be null");
299+
300+
maybeCallbackBeforeConvertWithQueries(queries, index);
301+
302+
List<IndexedObjectInformation> indexedObjectInformations = doBulkOperation(queries, bulkOptions, index);
303+
304+
maybeCallbackAfterSaveWithQueries(queries, index);
305+
306+
return indexedObjectInformations;
307+
}
308+
309+
public abstract List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
310+
IndexCoordinates index);
269311
// endregion
270312

271313
// region SearchOperations
@@ -620,6 +662,20 @@ protected void maybeCallbackBeforeConvertWithQuery(Object query, IndexCoordinate
620662
if (queryObject != null) {
621663
queryObject = maybeCallbackBeforeConvert(queryObject, index);
622664
indexQuery.setObject(queryObject);
665+
// the callback might have set som values relevant for the IndexQuery
666+
IndexQuery newQuery = getIndexQuery(queryObject);
667+
668+
if (indexQuery.getRouting() == null && newQuery.getRouting() != null) {
669+
indexQuery.setRouting(newQuery.getRouting());
670+
}
671+
672+
if (indexQuery.getSeqNo() == null && newQuery.getSeqNo() != null) {
673+
indexQuery.setSeqNo(newQuery.getSeqNo());
674+
}
675+
676+
if (indexQuery.getPrimaryTerm() == null && newQuery.getPrimaryTerm() != null) {
677+
indexQuery.setPrimaryTerm(newQuery.getPrimaryTerm());
678+
}
623679
}
624680
}
625681
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ public IndexOperations indexOps(IndexCoordinates index) {
137137
// endregion
138138

139139
// region DocumentOperations
140-
@Override
141-
public String index(IndexQuery query, IndexCoordinates index) {
142-
143-
maybeCallbackBeforeConvertWithQuery(query, index);
140+
public String doIndex(IndexQuery query, IndexCoordinates index) {
144141

145142
IndexRequest request = requestFactory.indexRequest(query, index);
146143
IndexResponse indexResponse = execute(client -> client.index(request, RequestOptions.DEFAULT));
@@ -152,8 +149,6 @@ public String index(IndexQuery query, IndexCoordinates index) {
152149
indexResponse.getPrimaryTerm(), indexResponse.getVersion()));
153150
}
154151

155-
maybeCallbackAfterSaveWithQuery(query, index);
156-
157152
return indexResponse.getId();
158153
}
159154

@@ -187,16 +182,6 @@ protected boolean doExists(String id, IndexCoordinates index) {
187182
return execute(client -> client.get(request, RequestOptions.DEFAULT).isExists());
188183
}
189184

190-
@Override
191-
public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions,
192-
IndexCoordinates index) {
193-
194-
Assert.notNull(queries, "List of IndexQuery must not be null");
195-
Assert.notNull(bulkOptions, "BulkOptions must not be null");
196-
197-
return doBulkOperation(queries, bulkOptions, index);
198-
}
199-
200185
@Override
201186
public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) {
202187

@@ -237,14 +222,12 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
237222
return new UpdateResponse(result);
238223
}
239224

240-
private List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
225+
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
241226
IndexCoordinates index) {
242-
maybeCallbackBeforeConvertWithQueries(queries, index);
243227
BulkRequest bulkRequest = requestFactory.bulkRequest(queries, bulkOptions, index);
244228
List<IndexedObjectInformation> indexedObjectInformationList = checkForBulkOperationFailure(
245229
execute(client -> client.bulk(bulkRequest, RequestOptions.DEFAULT)));
246230
updateIndexedObjectsWithQueries(queries, indexedObjectInformationList);
247-
maybeCallbackAfterSaveWithQueries(queries, index);
248231
return indexedObjectInformationList;
249232
}
250233
// endregion

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,7 @@ public void setSearchTimeout(String searchTimeout) {
144144
// endregion
145145

146146
// region DocumentOperations
147-
@Override
148-
public String index(IndexQuery query, IndexCoordinates index) {
149-
150-
maybeCallbackBeforeConvertWithQuery(query, index);
147+
public String doIndex(IndexQuery query, IndexCoordinates index) {
151148

152149
IndexRequestBuilder indexRequestBuilder = requestFactory.indexRequestBuilder(client, query, index);
153150
ActionFuture<IndexResponse> future = indexRequestBuilder.execute();
@@ -166,8 +163,6 @@ public String index(IndexQuery query, IndexCoordinates index) {
166163
response.getPrimaryTerm(), response.getVersion()));
167164
}
168165

169-
maybeCallbackAfterSaveWithQuery(query, index);
170-
171166
return documentId;
172167
}
173168

@@ -201,22 +196,6 @@ protected boolean doExists(String id, IndexCoordinates index) {
201196
return getRequestBuilder.execute().actionGet().isExists();
202197
}
203198

204-
@Override
205-
public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions,
206-
IndexCoordinates index) {
207-
208-
Assert.notNull(queries, "List of IndexQuery must not be null");
209-
Assert.notNull(bulkOptions, "BulkOptions must not be null");
210-
211-
List<IndexedObjectInformation> indexedObjectInformations = doBulkOperation(queries, bulkOptions, index);
212-
213-
updateIndexedObjectsWithQueries(queries, indexedObjectInformations);
214-
215-
maybeCallbackAfterSaveWithQueries(queries, index);
216-
217-
return indexedObjectInformations;
218-
}
219-
220199
@Override
221200
public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) {
222201

@@ -261,11 +240,13 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
261240
return new UpdateResponse(result);
262241
}
263242

264-
private List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
243+
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
265244
IndexCoordinates index) {
266-
maybeCallbackBeforeConvertWithQueries(queries, index);
267245
BulkRequestBuilder bulkRequest = requestFactory.bulkRequestBuilder(client, queries, bulkOptions, index);
268-
return checkForBulkOperationFailure(bulkRequest.execute().actionGet());
246+
final List<IndexedObjectInformation> indexedObjectInformations = checkForBulkOperationFailure(
247+
bulkRequest.execute().actionGet());
248+
updateIndexedObjectsWithQueries(queries, indexedObjectInformations);
249+
return indexedObjectInformations;
269250
}
270251
// endregion
271252

0 commit comments

Comments
 (0)