Skip to content

Commit

Permalink
fix include_named_queries_score propogation through request
Browse files Browse the repository at this point in the history
Signed-off-by: Dharin Shah <8616130+Dharin-shah@users.noreply.github.com>
  • Loading branch information
Dharin-shah committed Feb 1, 2024
1 parent 3418647 commit 6881e10
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public void testSimpleMatchedQueryFromFilteredQuery() throws Exception {
.should(rangeQuery("number").gte(2).queryName("test2"))
)
)
.setIncludeNamedQueriesScore(true)
.get();
assertHitCount(searchResponse, 3L);
for (SearchHit hit : searchResponse.getHits()) {
Expand All @@ -123,6 +124,7 @@ public void testSimpleMatchedQueryFromFilteredQuery() throws Exception {
.setQuery(
boolQuery().should(rangeQuery("number").lte(2).queryName("test1")).should(rangeQuery("number").gt(2).queryName("test2"))
)
.setIncludeNamedQueriesScore(true)
.get();
assertHitCount(searchResponse, 3L);
for (SearchHit hit : searchResponse.getHits()) {
Expand Down Expand Up @@ -251,6 +253,7 @@ public void testRegExpQuerySupportsName() throws InterruptedException {

SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.regexpQuery("title", "title1").queryName("regex"))
.setIncludeNamedQueriesScore(true)
.get();
assertHitCount(searchResponse, 1L);

Expand All @@ -273,9 +276,10 @@ public void testPrefixQuerySupportsName() throws InterruptedException {
refresh();
indexRandomForConcurrentSearch("test1");

SearchResponse searchResponse = client().prepareSearch()
var query = client().prepareSearch()
.setQuery(QueryBuilders.prefixQuery("title", "title").queryName("prefix"))
.get();
.setIncludeNamedQueriesScore(true);
var searchResponse = query.get();
assertHitCount(searchResponse, 1L);

for (SearchHit hit : searchResponse.getHits()) {
Expand Down Expand Up @@ -323,6 +327,7 @@ public void testWildcardQuerySupportsName() throws InterruptedException {

SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.wildcardQuery("title", "titl*").queryName("wildcard"))
.setIncludeNamedQueriesScore(true)
.get();
assertHitCount(searchResponse, 1L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,15 @@ public SearchRequestBuilder setTrackScores(boolean trackScores) {
return this;
}

/**
* Applies when fetching scores with named queries, and controls if scores will be tracked as well.
* Defaults to {@code false}.
*/
public SearchRequestBuilder setIncludeNamedQueriesScore(boolean includeNamedQueriesScore) {
sourceBuilder().includeNamedQueriesScores(includeNamedQueriesScore);
return this;
}

/**
* Indicates if the total hit count for the query should be tracked. Requests will count total hit count accurately
* up to 10,000 by default, see {@link #setTrackTotalHitsUpTo(int)} to change this value or set to true/false to always/never
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ private void parseSource(DefaultSearchContext context, SearchSourceBuilder sourc
}
}
context.trackScores(source.trackScores());
context.includeNamedQueriesScore(source.includeNamedQueriesScore());
if (source.trackTotalHitsUpTo() != null
&& source.trackTotalHitsUpTo() != SearchContext.TRACK_TOTAL_HITS_ACCURATE
&& context.scrollContext() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public SearchSourceBuilder(StreamInput in) throws IOException {
terminateAfter = in.readVInt();
timeout = in.readOptionalTimeValue();
trackScores = in.readBoolean();
includeNamedQueriesScore = in.readBoolean();
version = in.readOptionalBoolean();
seqNoAndPrimaryTerm = in.readOptionalBoolean();
extBuilders = in.readNamedWriteableList(SearchExtBuilder.class);
Expand Down Expand Up @@ -325,6 +326,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(terminateAfter);
out.writeOptionalTimeValue(timeout);
out.writeBoolean(trackScores);
out.writeBoolean(includeNamedQueriesScore);
out.writeOptionalBoolean(version);
out.writeOptionalBoolean(seqNoAndPrimaryTerm);
out.writeNamedWriteableList(extBuilders);
Expand Down Expand Up @@ -1122,6 +1124,7 @@ private SearchSourceBuilder shallowCopy(
rewrittenBuilder.terminateAfter = terminateAfter;
rewrittenBuilder.timeout = timeout;
rewrittenBuilder.trackScores = trackScores;
rewrittenBuilder.includeNamedQueriesScore = includeNamedQueriesScore;
rewrittenBuilder.trackTotalHitsUpTo = trackTotalHitsUpTo;
rewrittenBuilder.version = version;
rewrittenBuilder.seqNoAndPrimaryTerm = seqNoAndPrimaryTerm;
Expand Down Expand Up @@ -1774,6 +1777,7 @@ public int hashCode() {
terminateAfter,
timeout,
trackScores,
includeNamedQueriesScore,
version,
seqNoAndPrimaryTerm,
profile,
Expand Down Expand Up @@ -1816,6 +1820,7 @@ public boolean equals(Object obj) {
&& Objects.equals(terminateAfter, other.terminateAfter)
&& Objects.equals(timeout, other.timeout)
&& Objects.equals(trackScores, other.trackScores)
&& Objects.equals(includeNamedQueriesScore, other.includeNamedQueriesScore)
&& Objects.equals(version, other.version)
&& Objects.equals(seqNoAndPrimaryTerm, other.seqNoAndPrimaryTerm)
&& Objects.equals(profile, other.profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,13 @@ public FieldDoc searchAfter() {
return in.searchAfter();
}

public abstract SearchContext includeNamedQueriesScore(boolean includeNamedQueriesScore);
public SearchContext includeNamedQueriesScore(boolean includeNamedQueriesScore) {
return in.includeNamedQueriesScore(includeNamedQueriesScore);
}

public boolean includeNamedQueriesScore() {
return in.includeNamedQueriesScore();
}

@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
Expand Down

0 comments on commit 6881e10

Please sign in to comment.