Skip to content

Commit 36e3b93

Browse files
authored
Make SearchType in query nullable, set to null in NativeQuery with knnQuery.
Original Pull Request #2570 Closes #2569
1 parent 6edd3cf commit 36e3b93

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import co.elastic.clients.elasticsearch._types.FieldValue;
2323
import co.elastic.clients.elasticsearch._types.InlineScript;
2424
import co.elastic.clients.elasticsearch._types.OpType;
25+
import co.elastic.clients.elasticsearch._types.SearchType;
2526
import co.elastic.clients.elasticsearch._types.SortOptions;
2627
import co.elastic.clients.elasticsearch._types.SortOrder;
2728
import co.elastic.clients.elasticsearch._types.VersionType;
@@ -1153,9 +1154,12 @@ public MsearchRequest searchMsearchRequest(
11531154
var query = param.query();
11541155
mrb.searches(sb -> sb //
11551156
.header(h -> {
1157+
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
1158+
: searchType(query.getSearchType());
1159+
11561160
h //
11571161
.index(Arrays.asList(param.index().getIndexNames())) //
1158-
.searchType(searchType(query.getSearchType())) //
1162+
.searchType(searchType) //
11591163
.requestCache(query.getRequestCache()) //
11601164
;
11611165

@@ -1256,8 +1260,8 @@ public MsearchRequest searchMsearchRequest(
12561260
query.getScriptedFields().forEach(scriptedField -> bb.scriptFields(scriptedField.getFieldName(),
12571261
sf -> sf.script(getScript(scriptedField.getScriptData()))));
12581262

1259-
if (query instanceof NativeQuery) {
1260-
prepareNativeSearch((NativeQuery) query, bb);
1263+
if (query instanceof NativeQuery nativeQuery) {
1264+
prepareNativeSearch(nativeQuery, bb);
12611265
}
12621266
return bb;
12631267
} //
@@ -1279,12 +1283,15 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu
12791283

12801284
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntity(clazz);
12811285

1286+
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
1287+
: searchType(query.getSearchType());
1288+
12821289
builder //
12831290
.version(true) //
12841291
.trackScores(query.getTrackScores()) //
12851292
.allowNoIndices(query.getAllowNoIndices()) //
12861293
.source(getSourceConfig(query)) //
1287-
.searchType(searchType(query.getSearchType())) //
1294+
.searchType(searchType) //
12881295
.timeout(timeStringMs(query.getTimeout())) //
12891296
.requestCache(query.getRequestCache()) //
12901297
;
@@ -1361,8 +1368,8 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu
13611368
query.getScriptedFields().forEach(scriptedField -> builder.scriptFields(scriptedField.getFieldName(),
13621369
sf -> sf.script(getScript(scriptedField.getScriptData()))));
13631370

1364-
if (query instanceof NativeQuery) {
1365-
prepareNativeSearch((NativeQuery) query, builder);
1371+
if (query instanceof NativeQuery nativeQuery) {
1372+
prepareNativeSearch(nativeQuery, builder);
13661373
}
13671374

13681375
if (query.getTrackTotalHits() != null) {

src/main/java/org/springframework/data/elasticsearch/core/query/BaseQuery.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class BaseQuery implements Query {
5858
protected float minScore;
5959
@Nullable protected Collection<String> ids;
6060
@Nullable protected String route;
61-
protected SearchType searchType = SearchType.QUERY_THEN_FETCH;
61+
@Nullable protected SearchType searchType = SearchType.QUERY_THEN_FETCH;
6262
@Nullable protected IndicesOptions indicesOptions;
6363
protected boolean trackScores;
6464
@Nullable protected String preference;
@@ -278,10 +278,11 @@ public void setRoute(String route) {
278278
this.route = route;
279279
}
280280

281-
public void setSearchType(SearchType searchType) {
281+
public void setSearchType(@Nullable SearchType searchType) {
282282
this.searchType = searchType;
283283
}
284284

285+
@Nullable
285286
@Override
286287
public SearchType getSearchType() {
287288
return searchType;

src/main/java/org/springframework/data/elasticsearch/core/query/BaseQueryBuilder.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
4646
private float minScore;
4747
private final Collection<String> ids = new ArrayList<>();
4848
@Nullable private String route;
49-
private Query.SearchType searchType = Query.SearchType.QUERY_THEN_FETCH;
49+
@Nullable private Query.SearchType searchType = Query.SearchType.QUERY_THEN_FETCH;
5050
@Nullable private IndicesOptions indicesOptions;
5151
private boolean trackScores;
5252
@Nullable private String preference;
@@ -140,6 +140,7 @@ public List<IndexBoost> getIndicesBoost() {
140140
return indicesBoost;
141141
}
142142

143+
@Nullable
143144
public Query.SearchType getSearchType() {
144145
return searchType;
145146
}
@@ -250,23 +251,23 @@ public SELF withMaxResults(Integer maxResults) {
250251
return self();
251252
}
252253

253-
/**
254-
* Set Ids for a multi-get request run with this query. Not used in any other searches.
255-
*
256-
* @param ids list of id values
257-
*/
254+
/**
255+
* Set Ids for a multi-get request run with this query. Not used in any other searches.
256+
*
257+
* @param ids list of id values
258+
*/
258259
public SELF withIds(String... ids) {
259260

260261
this.ids.clear();
261262
this.ids.addAll(Arrays.asList(ids));
262263
return self();
263264
}
264265

265-
/**
266-
* Set Ids for a multi-get request run with this query. Not used in any other searches.
267-
*
268-
* @param ids list of id values
269-
*/
266+
/**
267+
* Set Ids for a multi-get request run with this query. Not used in any other searches.
268+
*
269+
* @param ids list of id values
270+
*/
270271
public SELF withIds(Collection<String> ids) {
271272

272273
Assert.notNull(ids, "ids must not be null");
@@ -342,7 +343,7 @@ public SELF withIndicesBoost(IndexBoost... indicesBoost) {
342343
return self();
343344
}
344345

345-
public SELF withSearchType(Query.SearchType searchType) {
346+
public SELF withSearchType(@Nullable Query.SearchType searchType) {
346347
this.searchType = searchType;
347348
return self();
348349
}
@@ -382,12 +383,12 @@ public SELF withRequestCache(@Nullable Boolean requestCache) {
382383
return self();
383384
}
384385

385-
/**
386-
* Set Ids with routing values for a multi-get request run with this query. Not used in any other searches.
387-
*
388-
* @param idsWithRouting list of id values, must not be {@literal null}
389-
* @since 4.3
390-
*/
386+
/**
387+
* Set Ids with routing values for a multi-get request run with this query. Not used in any other searches.
388+
*
389+
* @param idsWithRouting list of id values, must not be {@literal null}
390+
* @since 4.3
391+
*/
391392
public SELF withIdsWithRouting(List<Query.IdWithRouting> idsWithRouting) {
392393

393394
Assert.notNull(idsWithRouting, "idsWithRouting must not be null");

src/main/java/org/springframework/data/elasticsearch/core/query/Query.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ static Query multiGetQuery(Collection<String> ids) {
221221
*
222222
* @return
223223
*/
224+
@Nullable
224225
SearchType getSearchType();
225226

226227
/**

0 commit comments

Comments
 (0)