Add support for includeNamedQueriesScore in Query
* Add support for includeNamedQueriesScore in Query * Add integration tests in NativeQueryIntegrationTest class Signed-off-by: veljko.potparic <veljko_velid@yahoo.com>
This commit is contained in:
@@ -1426,6 +1426,7 @@ class RequestConverter extends AbstractQueryProcessor {
|
||||
.searchType(searchType) //
|
||||
.timeout(timeStringMs(query.getTimeout())) //
|
||||
.requestCache(query.getRequestCache()) //
|
||||
.includeNamedQueriesScore(query.getIncludeNamedQueriesScore()) //
|
||||
;
|
||||
|
||||
var pointInTime = query.getPointInTime();
|
||||
|
||||
@@ -81,6 +81,7 @@ public class BaseQuery implements Query {
|
||||
protected List<IdWithRouting> idsWithRouting = Collections.emptyList();
|
||||
protected List<RuntimeField> runtimeFields = new ArrayList<>();
|
||||
@Nullable protected PointInTime pointInTime;
|
||||
@Nullable protected Boolean includeNamedQueriesScore;
|
||||
private boolean queryIsUpdatedByConverter = false;
|
||||
@Nullable private Integer reactiveBatchSize = null;
|
||||
@Nullable private Boolean allowNoIndices = null;
|
||||
@@ -123,6 +124,7 @@ public class BaseQuery implements Query {
|
||||
this.docValueFields = builder.getDocValueFields();
|
||||
this.scriptedFields = builder.getScriptedFields();
|
||||
this.runtimeFields = builder.getRuntimeFields();
|
||||
this.includeNamedQueriesScore = builder.getIncludeNamedQueriesScore();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -455,6 +457,11 @@ public class BaseQuery implements Query {
|
||||
this.requestCache = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIncludeNamedQueriesScore(@Nullable Boolean value) {
|
||||
this.includeNamedQueriesScore = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Boolean getRequestCache() {
|
||||
@@ -480,6 +487,11 @@ public class BaseQuery implements Query {
|
||||
return indicesBoost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Boolean getIncludeNamedQueriesScore() {
|
||||
return this.includeNamedQueriesScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 5.0
|
||||
*/
|
||||
|
||||
@@ -71,6 +71,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
||||
@Nullable Integer reactiveBatchSize;
|
||||
private final List<DocValueField> docValueFields = new ArrayList<>();
|
||||
private final List<ScriptedField> scriptedFields = new ArrayList<>();
|
||||
@Nullable private Boolean includeNamedQueryScore;
|
||||
|
||||
@Nullable
|
||||
public Sort getSort() {
|
||||
@@ -177,6 +178,11 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
||||
return requestCache;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Boolean getIncludeNamedQueriesScore(){
|
||||
return includeNamedQueryScore;
|
||||
}
|
||||
|
||||
public List<Query.IdWithRouting> getIdsWithRouting() {
|
||||
return idsWithRouting;
|
||||
}
|
||||
@@ -381,6 +387,11 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
||||
return self();
|
||||
}
|
||||
|
||||
public SELF withIncludeNamedQueryScore (@Nullable Boolean namedQueryScore) {
|
||||
this.includeNamedQueryScore = namedQueryScore;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Ids with routing values for a multi-get request run with this query. Not used in any other searches.
|
||||
*
|
||||
|
||||
@@ -491,6 +491,20 @@ public interface Query {
|
||||
*/
|
||||
public Integer getRequestSize();
|
||||
|
||||
/**
|
||||
* Sets the include_named_queries_score value for the query.
|
||||
* If true, the response includes the score contribution from any named queries.
|
||||
*
|
||||
* @param value new value
|
||||
*/
|
||||
void setIncludeNamedQueriesScore(@Nullable Boolean value);
|
||||
|
||||
/**
|
||||
* @return the include_named_queries_score value for this query.
|
||||
*/
|
||||
@Nullable
|
||||
Boolean getIncludeNamedQueriesScore();
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
|
||||
+64
@@ -22,6 +22,8 @@ import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
|
||||
import org.assertj.core.data.Offset;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@@ -172,6 +174,68 @@ public abstract class NativeQueryIntegrationTests {
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
|
||||
}
|
||||
|
||||
@Test // #3248
|
||||
@DisplayName("should be able to use includeNamedQueriesScore in a NativeQuery")
|
||||
void shouldBeAbleToUseIncludeQueriesScoreInANativeQuery() {
|
||||
|
||||
var entity = new SampleEntity();
|
||||
entity.setId("7");
|
||||
entity.setText("seven");
|
||||
operations.save(entity);
|
||||
entity = new SampleEntity();
|
||||
entity.setId("42");
|
||||
entity.setText("matched");
|
||||
operations.save(entity);
|
||||
|
||||
var matchQuery = MatchQuery.of(m -> m.field("text")
|
||||
.query("matched")
|
||||
.queryName("namedQuery"));
|
||||
|
||||
var nativeQuery = NativeQuery.builder()
|
||||
.withQuery(matchQuery._toQuery())
|
||||
.withIncludeNamedQueryScore(true)
|
||||
.build();
|
||||
|
||||
var searchHits = operations.search(nativeQuery, SampleEntity.class);
|
||||
|
||||
assertThat(searchHits.getTotalHits()).isEqualTo(1);
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
|
||||
assertThat(searchHits.getSearchHit(0).getMatchedQueries()).containsKey("namedQuery");
|
||||
assertThat(searchHits.getSearchHit(0).getMatchedQueries().get("namedQuery")).isGreaterThan(0.0);
|
||||
assertThat(searchHits.getSearchHit(0).getMatchedQueries().get("namedQuery"))
|
||||
.isCloseTo(searchHits.getMaxScore(), Offset.offset(0.01));
|
||||
}
|
||||
|
||||
@Test // #3248
|
||||
@DisplayName("should not be able to use named queries score in a NativeQuery when disabled")
|
||||
void shouldNotBeAbleToUseNamedQueriesScoreInANativeQueryWhenDisabled() {
|
||||
|
||||
var entity = new SampleEntity();
|
||||
entity.setId("7");
|
||||
entity.setText("seven");
|
||||
operations.save(entity);
|
||||
entity = new SampleEntity();
|
||||
entity.setId("42");
|
||||
entity.setText("matched");
|
||||
operations.save(entity);
|
||||
|
||||
var matchQuery = MatchQuery.of(m -> m.field("text")
|
||||
.query("matched")
|
||||
.queryName("namedQuery"));
|
||||
|
||||
var nativeQuery = NativeQuery.builder()
|
||||
.withQuery(matchQuery._toQuery())
|
||||
.withIncludeNamedQueryScore(false)
|
||||
.build();
|
||||
|
||||
var searchHits = operations.search(nativeQuery, SampleEntity.class);
|
||||
|
||||
assertThat(searchHits.getTotalHits()).isEqualTo(1);
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
|
||||
assertThat(searchHits.getSearchHit(0).getMatchedQueries()).containsKey("namedQuery");
|
||||
assertThat(searchHits.getSearchHit(0).getMatchedQueries().get("namedQuery")).isNull();
|
||||
}
|
||||
|
||||
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
||||
static class SampleEntity {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user