Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.InlineScript;
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.SortOrder;
import co.elastic.clients.elasticsearch._types.VersionType;
Expand Down Expand Up @@ -1153,9 +1154,12 @@ public MsearchRequest searchMsearchRequest(
var query = param.query();
mrb.searches(sb -> sb //
.header(h -> {
var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
: searchType(query.getSearchType());

h //
.index(Arrays.asList(param.index().getIndexNames())) //
.searchType(searchType(query.getSearchType())) //
.searchType(searchType) //
.requestCache(query.getRequestCache()) //
;

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

if (query instanceof NativeQuery) {
prepareNativeSearch((NativeQuery) query, bb);
if (query instanceof NativeQuery nativeQuery) {
prepareNativeSearch(nativeQuery, bb);
}
return bb;
} //
Expand All @@ -1279,12 +1283,15 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu

ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntity(clazz);

var searchType = (query instanceof NativeQuery nativeQuery && nativeQuery.getKnnQuery() != null) ? null
: searchType(query.getSearchType());

builder //
.version(true) //
.trackScores(query.getTrackScores()) //
.allowNoIndices(query.getAllowNoIndices()) //
.source(getSourceConfig(query)) //
.searchType(searchType(query.getSearchType())) //
.searchType(searchType) //
.timeout(timeStringMs(query.getTimeout())) //
.requestCache(query.getRequestCache()) //
;
Expand Down Expand Up @@ -1361,8 +1368,8 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu
query.getScriptedFields().forEach(scriptedField -> builder.scriptFields(scriptedField.getFieldName(),
sf -> sf.script(getScript(scriptedField.getScriptData()))));

if (query instanceof NativeQuery) {
prepareNativeSearch((NativeQuery) query, builder);
if (query instanceof NativeQuery nativeQuery) {
prepareNativeSearch(nativeQuery, builder);
}

if (query.getTrackTotalHits() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class BaseQuery implements Query {
protected float minScore;
@Nullable protected Collection<String> ids;
@Nullable protected String route;
protected SearchType searchType = SearchType.QUERY_THEN_FETCH;
@Nullable protected SearchType searchType = SearchType.QUERY_THEN_FETCH;
@Nullable protected IndicesOptions indicesOptions;
protected boolean trackScores;
@Nullable protected String preference;
Expand Down Expand Up @@ -278,10 +278,11 @@ public void setRoute(String route) {
this.route = route;
}

public void setSearchType(SearchType searchType) {
public void setSearchType(@Nullable SearchType searchType) {
this.searchType = searchType;
}

@Nullable
@Override
public SearchType getSearchType() {
return searchType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
private float minScore;
private final Collection<String> ids = new ArrayList<>();
@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;
private boolean trackScores;
@Nullable private String preference;
Expand Down Expand Up @@ -140,6 +140,7 @@ public List<IndexBoost> getIndicesBoost() {
return indicesBoost;
}

@Nullable
public Query.SearchType getSearchType() {
return searchType;
}
Expand Down Expand Up @@ -250,23 +251,23 @@ public SELF withMaxResults(Integer maxResults) {
return self();
}

/**
* Set Ids for a multi-get request run with this query. Not used in any other searches.
*
* @param ids list of id values
*/
/**
* Set Ids for a multi-get request run with this query. Not used in any other searches.
*
* @param ids list of id values
*/
public SELF withIds(String... ids) {

this.ids.clear();
this.ids.addAll(Arrays.asList(ids));
return self();
}

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

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

public SELF withSearchType(Query.SearchType searchType) {
public SELF withSearchType(@Nullable Query.SearchType searchType) {
this.searchType = searchType;
return self();
}
Expand Down Expand Up @@ -382,12 +383,12 @@ public SELF withRequestCache(@Nullable Boolean requestCache) {
return self();
}

/**
* 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}
* @since 4.3
*/
/**
* 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}
* @since 4.3
*/
public SELF withIdsWithRouting(List<Query.IdWithRouting> idsWithRouting) {

Assert.notNull(idsWithRouting, "idsWithRouting must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ static Query multiGetQuery(Collection<String> ids) {
*
* @return
*/
@Nullable
SearchType getSearchType();

/**
Expand Down