Skip to content

Commit d9b23ed

Browse files
authored
Remove Elasticsearch classes from suggest response data.
Original Pull Request #1940 Closes #1302
1 parent d1528ed commit d9b23ed

38 files changed

+1129
-185
lines changed

Diff for: src/main/asciidoc/reference/elasticsearch-migration-guide-4.2-4.3.adoc

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Check the sections on <<elasticsearch-migration-guide-4.2-4.3.deprecations>> and
2121
[[elasticsearch-migration-guide-4.2-4.3.deprecations]]
2222
== Deprecations
2323

24+
=== suggest methods
25+
26+
In `SearchOperations`, and so in `ElasticsearchOperations` as well, the `suggest` methods taking a `org.elasticsearch.search.suggest.SuggestBuilder` as argument and returning a `org.elasticsearch.action.search.SearchResponse` have been deprecated.
27+
Use `SearchHits<T> search(Query query, Class<T> clazz)` instead, passing in a `NativeSearchQuery` which can contain a `SuggestBuilder` and read the suggest results from the returned `SearchHit<T>`.
28+
29+
In `ReactiveSearchOperations` the new `suggest` methods return a `Mono<org.springframework.data.elasticsearch.core.suggest.response.Suggest>` now.
30+
Here as well the old methods are deprecated.
31+
2432
[[elasticsearch-migration-guide-4.2-4.3.breaking-changes]]
2533
== Breaking Changes
2634

@@ -59,3 +67,7 @@ Some properties of the `org.springframework.data.elasticsearch.core.query.BulkOp
5967
=== IndicesOptions change
6068

6169
Spring Data Elasticsearch now uses `org.springframework.data.elasticsearch.core.query.IndicesOptions` instead of `org.elasticsearch.action.support.IndicesOptions`.
70+
71+
=== Completion classes
72+
73+
The classes from the package `org.springframework.data.elasticsearch.core.completion` have been moved to `org.springframework.data.elasticsearch.core.suggest`.

Diff for: src/main/asciidoc/reference/elasticsearch-operations.adoc

+16-11
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ The default implementations of the interfaces offer:
2020
[NOTE]
2121
====
2222
.Index management and automatic creation of indices and mappings.
23+
The `IndexOperations` interface and the provided implementation which can be obtained from an `ElasticsearchOperations` instance - for example with a call to `operations.indexOps(clazz)`- give the user the ability to create indices, put mappings or store template and alias information in the Elasticsearch cluster.
24+
Details of the index that will be created can be set by using the `@Setting` annotation, refer to <<elasticsearc.misc.index.settings>> for further information.
2325
24-
The `IndexOperations` interface and the provided implementation which can be obtained from an `ElasticsearchOperations` instance - for example with a call to `operations.indexOps(clazz)`- give the user the ability to create indices, put mappings or store template and alias information in the Elasticsearch cluster. Details of the index that will be created
25-
can be set by using the `@Setting` annotation, refer to <<elasticsearc.misc.index.settings>> for further information.
26-
27-
**None of these operations are done automatically** by the implementations of `IndexOperations` or `ElasticsearchOperations`. It is the user's responsibility to call the methods.
26+
**None of these operations are done automatically** by the implementations of `IndexOperations` or `ElasticsearchOperations`.
27+
It is the user's responsibility to call the methods.
2828
2929
There is support for automatic creation of indices and writing the mappings when using Spring Data Elasticsearch repositories, see <<elasticsearch.repositories.autocreation>>
3030
@@ -58,6 +58,7 @@ public class TransportClientConfig extends ElasticsearchConfigurationSupport {
5858
}
5959
}
6060
----
61+
6162
<1> Setting up the <<elasticsearch.clients.transport>>.
6263
Deprecated as of version 4.0.
6364
<2> Creating the `ElasticsearchTemplate` bean, offering both names, _elasticsearchOperations_ and _elasticsearchTemplate_.
@@ -82,6 +83,7 @@ public class RestClientConfig extends AbstractElasticsearchConfiguration {
8283
// no special bean creation needed <2>
8384
}
8485
----
86+
8587
<1> Setting up the <<elasticsearch.clients.rest>>.
8688
<2> The base class `AbstractElasticsearchConfiguration` already provides the `elasticsearchTemplate` bean.
8789
====
@@ -127,6 +129,7 @@ public class TestController {
127129
}
128130
129131
----
132+
130133
<1> Let Spring inject the provided `ElasticsearchOperations` bean in the constructor.
131134
<2> Store some entity in the Elasticsearch cluster.
132135
<3> Retrieve the entity with a query by id.
@@ -164,6 +167,7 @@ Contains the following information:
164167
* Maximum score
165168
* A list of `SearchHit<T>` objects
166169
* Returned aggregations
170+
* Returned suggest results
167171

168172
.SearchPage<T>
169173
Defines a Spring Data `Page` that contains a `SearchHits<T>` element and can be used for paging access using repository methods.
@@ -182,12 +186,12 @@ Almost all of the methods defined in the `SearchOperations` and `ReactiveSearchO
182186
[[elasticsearch.operations.criteriaquery]]
183187
=== CriteriaQuery
184188

185-
`CriteriaQuery` based queries allow the creation of queries to search for data without knowing the syntax or basics of Elasticsearch queries. They allow the user to build queries by simply chaining and combining `Criteria` objects that specifiy the criteria the searched documents must fulfill.
189+
`CriteriaQuery` based queries allow the creation of queries to search for data without knowing the syntax or basics of Elasticsearch queries.
190+
They allow the user to build queries by simply chaining and combining `Criteria` objects that specifiy the criteria the searched documents must fulfill.
186191

187192
NOTE: when talking about AND or OR when combining criteria keep in mind, that in Elasticsearch AND are converted to a **must** condition and OR to a **should**
188193

189-
`Criteria` and their usage are best explained by example
190-
(let's assume we have a `Book` entity with a `price` property):
194+
`Criteria` and their usage are best explained by example (let's assume we have a `Book` entity with a `price` property):
191195

192196
.Get books with a given price
193197
====
@@ -211,19 +215,21 @@ Query query = new CriteriaQuery(criteria);
211215

212216
When chaining `Criteria`, by default a AND logic is used:
213217

214-
.Get all persons with first name _James_ and last name _Miller_:
218+
.Get all persons with first name _James_ and last name _Miller_:
215219
====
216220
[source,java]
217221
----
218222
Criteria criteria = new Criteria("lastname").is("Miller") <1>
219223
.and("firstname").is("James") <2>
220224
Query query = new CriteriaQuery(criteria);
221225
----
226+
222227
<1> the first `Criteria`
223228
<2> the and() creates a new `Criteria` and chaines it to the first one.
224229
====
225230

226-
If you want to create nested queries, you need to use subqueries for this. Let's assume we want to find all persons with a last name of _Miller_ and a first name of either _Jack_ or _John_:
231+
If you want to create nested queries, you need to use subqueries for this.
232+
Let's assume we want to find all persons with a last name of _Miller_ and a first name of either _Jack_ or _John_:
227233

228234
.Nested subqueries
229235
====
@@ -236,6 +242,7 @@ Criteria miller = new Criteria("lastName").is("Miller") <.>
236242
);
237243
Query query = new CriteriaQuery(criteria);
238244
----
245+
239246
<.> create a first `Criteria` for the last name
240247
<.> this is combined with AND to a subCriteria
241248
<.> This sub Criteria is an OR combination for the first name _John_
@@ -281,5 +288,3 @@ Query query = new NativeSearchQueryBuilder()
281288
SearchHits<Person> searchHits = operations.search(query, Person.class);
282289
----
283290
====
284-
285-

Diff for: src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchRestTransportTemplate.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2021 the original author or authors.
2+
* Copyright 2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.action.search.SearchResponse;
3333
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
3434
import org.elasticsearch.index.query.QueryBuilders;
35+
import org.elasticsearch.search.suggest.SuggestBuilder;
3536
import org.springframework.data.elasticsearch.BulkFailureException;
3637
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
3738
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
@@ -103,11 +104,12 @@ public <T> List<SearchHits<T>> multiSearch(List<? extends Query> queries, Class<
103104

104105
MultiSearchResponse.Item[] items = getMultiSearchResult(request);
105106

107+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
106108
SearchDocumentResponseCallback<SearchHits<T>> callback = new ReadSearchDocumentResponseCallback<>(clazz, index);
107109
List<SearchHits<T>> res = new ArrayList<>(queries.size());
108110
int c = 0;
109111
for (Query query : queries) {
110-
res.add(callback.doWith(SearchDocumentResponse.from(items[c++].getResponse())));
112+
res.add(callback.doWith(SearchDocumentResponse.from(items[c++].getResponse(), documentCallback::doWith)));
111113
}
112114
return res;
113115
}
@@ -134,11 +136,13 @@ public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class
134136
for (Query query : queries) {
135137
Class entityClass = it1.next();
136138

139+
IndexCoordinates index = getIndexCoordinatesFor(entityClass);
140+
ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
137141
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass,
138-
getIndexCoordinatesFor(entityClass));
142+
index);
139143

140144
SearchResponse response = items[c++].getResponse();
141-
res.add(callback.doWith(SearchDocumentResponse.from(response)));
145+
res.add(callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith)));
142146
}
143147
return res;
144148
}
@@ -166,11 +170,12 @@ public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class
166170
for (Query query : queries) {
167171
Class entityClass = it1.next();
168172

173+
ReadDocumentCallback<?> documentCallback = new ReadDocumentCallback<>(elasticsearchConverter, entityClass, index);
169174
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass,
170175
index);
171176

172177
SearchResponse response = items[c++].getResponse();
173-
res.add(callback.doWith(SearchDocumentResponse.from(response)));
178+
res.add(callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith)));
174179
}
175180
return res;
176181
}
@@ -204,5 +209,11 @@ protected String getRuntimeLibraryVersion() {
204209
return Version.CURRENT.toString();
205210
}
206211

212+
@Override
213+
@Deprecated
214+
public SearchResponse suggest(SuggestBuilder suggestion, Class<?> clazz) {
215+
return suggest(suggestion, getIndexCoordinatesFor(clazz));
216+
}
217+
207218
// endregion
208219
}

Diff for: src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.List;
2323
import java.util.stream.Collectors;
2424

25-
import org.elasticsearch.action.search.SearchResponse;
26-
import org.elasticsearch.search.suggest.SuggestBuilder;
2725
import org.springframework.beans.BeansException;
2826
import org.springframework.context.ApplicationContext;
2927
import org.springframework.context.ApplicationContextAware;
@@ -432,11 +430,6 @@ protected void searchScrollClear(String scrollId) {
432430
*/
433431
abstract protected void searchScrollClear(List<String> scrollIds);
434432

435-
@Override
436-
public SearchResponse suggest(SuggestBuilder suggestion, Class<?> clazz) {
437-
return suggest(suggestion, getIndexCoordinatesFor(clazz));
438-
}
439-
440433
// endregion
441434

442435
// region Helper methods
@@ -758,6 +751,7 @@ public ReadSearchDocumentResponseCallback(Class<T> type, IndexCoordinates index)
758751
this.type = type;
759752
}
760753

754+
@NonNull
761755
@Override
762756
public SearchHits<T> doWith(SearchDocumentResponse response) {
763757
List<T> entities = response.getSearchDocuments().stream().map(delegate::doWith).collect(Collectors.toList());
@@ -778,6 +772,7 @@ public ReadSearchScrollDocumentResponseCallback(Class<T> type, IndexCoordinates
778772
this.type = type;
779773
}
780774

775+
@NonNull
781776
@Override
782777
public SearchScrollHits<T> doWith(SearchDocumentResponse response) {
783778
List<T> entities = response.getSearchDocuments().stream().map(delegate::doWith).collect(Collectors.toList());

Diff for: src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ public <T> SearchHits<T> search(Query query, Class<T> clazz, IndexCoordinates in
317317
SearchRequest searchRequest = requestFactory.searchRequest(query, clazz, index);
318318
SearchResponse response = execute(client -> client.search(searchRequest, RequestOptions.DEFAULT));
319319

320+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
320321
SearchDocumentResponseCallback<SearchHits<T>> callback = new ReadSearchDocumentResponseCallback<>(clazz, index);
321-
return callback.doWith(SearchDocumentResponse.from(response));
322+
323+
return callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith));
322324
}
323325

324326
@Override
@@ -332,9 +334,10 @@ public <T> SearchScrollHits<T> searchScrollStart(long scrollTimeInMillis, Query
332334

333335
SearchResponse response = execute(client -> client.search(searchRequest, RequestOptions.DEFAULT));
334336

337+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
335338
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
336339
index);
337-
return callback.doWith(SearchDocumentResponse.from(response));
340+
return callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith));
338341
}
339342

340343
@Override
@@ -346,9 +349,10 @@ public <T> SearchScrollHits<T> searchScrollContinue(@Nullable String scrollId, l
346349

347350
SearchResponse response = execute(client -> client.scroll(request, RequestOptions.DEFAULT));
348351

349-
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = //
350-
new ReadSearchScrollDocumentResponseCallback<>(clazz, index);
351-
return callback.doWith(SearchDocumentResponse.from(response));
352+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
353+
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
354+
index);
355+
return callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith));
352356
}
353357

354358
@Override

Diff for: src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,9 @@ public <T> SearchHits<T> search(Query query, Class<T> clazz, IndexCoordinates in
353353
SearchRequestBuilder searchRequestBuilder = requestFactory.searchRequestBuilder(client, query, clazz, index);
354354
SearchResponse response = getSearchResponse(searchRequestBuilder);
355355

356+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
356357
SearchDocumentResponseCallback<SearchHits<T>> callback = new ReadSearchDocumentResponseCallback<>(clazz, index);
357-
return callback.doWith(SearchDocumentResponse.from(response));
358+
return callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith));
358359
}
359360

360361
@Override
@@ -369,9 +370,10 @@ public <T> SearchScrollHits<T> searchScrollStart(long scrollTimeInMillis, Query
369370

370371
SearchResponse response = getSearchResponseWithTimeout(action);
371372

373+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
372374
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
373375
index);
374-
return callback.doWith(SearchDocumentResponse.from(response));
376+
return callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith));
375377
}
376378

377379
@Override
@@ -385,9 +387,10 @@ public <T> SearchScrollHits<T> searchScrollContinue(@Nullable String scrollId, l
385387

386388
SearchResponse response = getSearchResponseWithTimeout(action);
387389

390+
ReadDocumentCallback<T> documentCallback = new ReadDocumentCallback<T>(elasticsearchConverter, clazz, index);
388391
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
389392
index);
390-
return callback.doWith(SearchDocumentResponse.from(response));
393+
return callback.doWith(SearchDocumentResponse.from(response, documentCallback::doWith));
391394
}
392395

393396
@Override

0 commit comments

Comments
 (0)