Make SearchType in query nullable, set to null in NativeQuery with knnQuery.

Original Pull Request #2570
Closes #2569
This commit is contained in:
Peter-Josef Meisch 2023-05-20 17:31:30 +02:00 committed by GitHub
parent 6edd3cf1fe
commit 36e3b93ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 26 deletions

View File

@ -22,6 +22,7 @@ import co.elastic.clients.elasticsearch._types.Conflicts;
import co.elastic.clients.elasticsearch._types.FieldValue; import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.InlineScript; import co.elastic.clients.elasticsearch._types.InlineScript;
import co.elastic.clients.elasticsearch._types.OpType; import co.elastic.clients.elasticsearch._types.OpType;
import co.elastic.clients.elasticsearch._types.SearchType;
import co.elastic.clients.elasticsearch._types.SortOptions; import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder; import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.VersionType; import co.elastic.clients.elasticsearch._types.VersionType;
@ -1153,9 +1154,12 @@ class RequestConverter {
var query = param.query(); var query = param.query();
mrb.searches(sb -> sb // mrb.searches(sb -> sb //
.header(h -> { .header(h -> {
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
: searchType(query.getSearchType());
h // h //
.index(Arrays.asList(param.index().getIndexNames())) // .index(Arrays.asList(param.index().getIndexNames())) //
.searchType(searchType(query.getSearchType())) // .searchType(searchType) //
.requestCache(query.getRequestCache()) // .requestCache(query.getRequestCache()) //
; ;
@ -1256,8 +1260,8 @@ class RequestConverter {
query.getScriptedFields().forEach(scriptedField -> bb.scriptFields(scriptedField.getFieldName(), query.getScriptedFields().forEach(scriptedField -> bb.scriptFields(scriptedField.getFieldName(),
sf -> sf.script(getScript(scriptedField.getScriptData())))); sf -> sf.script(getScript(scriptedField.getScriptData()))));
if (query instanceof NativeQuery) { if (query instanceof NativeQuery nativeQuery) {
prepareNativeSearch((NativeQuery) query, bb); prepareNativeSearch(nativeQuery, bb);
} }
return bb; return bb;
} // } //
@ -1279,12 +1283,15 @@ class RequestConverter {
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntity(clazz); ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntity(clazz);
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
: searchType(query.getSearchType());
builder // builder //
.version(true) // .version(true) //
.trackScores(query.getTrackScores()) // .trackScores(query.getTrackScores()) //
.allowNoIndices(query.getAllowNoIndices()) // .allowNoIndices(query.getAllowNoIndices()) //
.source(getSourceConfig(query)) // .source(getSourceConfig(query)) //
.searchType(searchType(query.getSearchType())) // .searchType(searchType) //
.timeout(timeStringMs(query.getTimeout())) // .timeout(timeStringMs(query.getTimeout())) //
.requestCache(query.getRequestCache()) // .requestCache(query.getRequestCache()) //
; ;
@ -1361,8 +1368,8 @@ class RequestConverter {
query.getScriptedFields().forEach(scriptedField -> builder.scriptFields(scriptedField.getFieldName(), query.getScriptedFields().forEach(scriptedField -> builder.scriptFields(scriptedField.getFieldName(),
sf -> sf.script(getScript(scriptedField.getScriptData())))); sf -> sf.script(getScript(scriptedField.getScriptData()))));
if (query instanceof NativeQuery) { if (query instanceof NativeQuery nativeQuery) {
prepareNativeSearch((NativeQuery) query, builder); prepareNativeSearch(nativeQuery, builder);
} }
if (query.getTrackTotalHits() != null) { if (query.getTrackTotalHits() != null) {

View File

@ -58,7 +58,7 @@ public class BaseQuery implements Query {
protected float minScore; protected float minScore;
@Nullable protected Collection<String> ids; @Nullable protected Collection<String> ids;
@Nullable protected String route; @Nullable protected String route;
protected SearchType searchType = SearchType.QUERY_THEN_FETCH; @Nullable protected SearchType searchType = SearchType.QUERY_THEN_FETCH;
@Nullable protected IndicesOptions indicesOptions; @Nullable protected IndicesOptions indicesOptions;
protected boolean trackScores; protected boolean trackScores;
@Nullable protected String preference; @Nullable protected String preference;
@ -278,10 +278,11 @@ public class BaseQuery implements Query {
this.route = route; this.route = route;
} }
public void setSearchType(SearchType searchType) { public void setSearchType(@Nullable SearchType searchType) {
this.searchType = searchType; this.searchType = searchType;
} }
@Nullable
@Override @Override
public SearchType getSearchType() { public SearchType getSearchType() {
return searchType; return searchType;

View File

@ -46,7 +46,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
private float minScore; private float minScore;
private final Collection<String> ids = new ArrayList<>(); private final Collection<String> ids = new ArrayList<>();
@Nullable private String route; @Nullable private String route;
private Query.SearchType searchType = Query.SearchType.QUERY_THEN_FETCH; @Nullable private Query.SearchType searchType = Query.SearchType.QUERY_THEN_FETCH;
@Nullable private IndicesOptions indicesOptions; @Nullable private IndicesOptions indicesOptions;
private boolean trackScores; private boolean trackScores;
@Nullable private String preference; @Nullable private String preference;
@ -140,6 +140,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return indicesBoost; return indicesBoost;
} }
@Nullable
public Query.SearchType getSearchType() { public Query.SearchType getSearchType() {
return searchType; return searchType;
} }
@ -250,11 +251,11 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return self(); return self();
} }
/** /**
* Set Ids for a multi-get request run with this query. Not used in any other searches. * Set Ids for a multi-get request run with this query. Not used in any other searches.
* *
* @param ids list of id values * @param ids list of id values
*/ */
public SELF withIds(String... ids) { public SELF withIds(String... ids) {
this.ids.clear(); this.ids.clear();
@ -262,11 +263,11 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return self(); return self();
} }
/** /**
* Set Ids for a multi-get request run with this query. Not used in any other searches. * Set Ids for a multi-get request run with this query. Not used in any other searches.
* *
* @param ids list of id values * @param ids list of id values
*/ */
public SELF withIds(Collection<String> ids) { public SELF withIds(Collection<String> ids) {
Assert.notNull(ids, "ids must not be null"); Assert.notNull(ids, "ids must not be null");
@ -342,7 +343,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return self(); return self();
} }
public SELF withSearchType(Query.SearchType searchType) { public SELF withSearchType(@Nullable Query.SearchType searchType) {
this.searchType = searchType; this.searchType = searchType;
return self(); return self();
} }
@ -382,12 +383,12 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return self(); return self();
} }
/** /**
* Set Ids with routing values for a multi-get request run with this query. Not used in any other searches. * Set Ids with routing values for a multi-get request run with this query. Not used in any other searches.
* *
* @param idsWithRouting list of id values, must not be {@literal null} * @param idsWithRouting list of id values, must not be {@literal null}
* @since 4.3 * @since 4.3
*/ */
public SELF withIdsWithRouting(List<Query.IdWithRouting> idsWithRouting) { public SELF withIdsWithRouting(List<Query.IdWithRouting> idsWithRouting) {
Assert.notNull(idsWithRouting, "idsWithRouting must not be null"); Assert.notNull(idsWithRouting, "idsWithRouting must not be null");

View File

@ -221,6 +221,7 @@ public interface Query {
* *
* @return * @return
*/ */
@Nullable
SearchType getSearchType(); SearchType getSearchType();
/** /**