Skip to content

Commit 56ff6fc

Browse files
authored
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>
1 parent 6f4a50c commit 56ff6fc

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,7 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu
14261426
.searchType(searchType) //
14271427
.timeout(timeStringMs(query.getTimeout())) //
14281428
.requestCache(query.getRequestCache()) //
1429+
.includeNamedQueriesScore(query.getIncludeNamedQueriesScore()) //
14291430
;
14301431

14311432
var pointInTime = query.getPointInTime();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class BaseQuery implements Query {
8181
protected List<IdWithRouting> idsWithRouting = Collections.emptyList();
8282
protected List<RuntimeField> runtimeFields = new ArrayList<>();
8383
@Nullable protected PointInTime pointInTime;
84+
@Nullable protected Boolean includeNamedQueriesScore;
8485
private boolean queryIsUpdatedByConverter = false;
8586
@Nullable private Integer reactiveBatchSize = null;
8687
@Nullable private Boolean allowNoIndices = null;
@@ -123,6 +124,7 @@ public <Q extends BaseQuery, B extends BaseQueryBuilder<Q, B>> BaseQuery(BaseQue
123124
this.docValueFields = builder.getDocValueFields();
124125
this.scriptedFields = builder.getScriptedFields();
125126
this.runtimeFields = builder.getRuntimeFields();
127+
this.includeNamedQueriesScore = builder.getIncludeNamedQueriesScore();
126128
}
127129

128130
/**
@@ -455,6 +457,11 @@ public void setRequestCache(@Nullable Boolean value) {
455457
this.requestCache = value;
456458
}
457459

460+
@Override
461+
public void setIncludeNamedQueriesScore(@Nullable Boolean value) {
462+
this.includeNamedQueriesScore = value;
463+
}
464+
458465
@Override
459466
@Nullable
460467
public Boolean getRequestCache() {
@@ -480,6 +487,11 @@ public List<IndexBoost> getIndicesBoost() {
480487
return indicesBoost;
481488
}
482489

490+
@Override
491+
public @Nullable Boolean getIncludeNamedQueriesScore() {
492+
return this.includeNamedQueriesScore;
493+
}
494+
483495
/**
484496
* @since 5.0
485497
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
7171
@Nullable Integer reactiveBatchSize;
7272
private final List<DocValueField> docValueFields = new ArrayList<>();
7373
private final List<ScriptedField> scriptedFields = new ArrayList<>();
74+
@Nullable private Boolean includeNamedQueryScore;
7475

7576
@Nullable
7677
public Sort getSort() {
@@ -177,6 +178,11 @@ public Boolean getRequestCache() {
177178
return requestCache;
178179
}
179180

181+
@Nullable
182+
public Boolean getIncludeNamedQueriesScore(){
183+
return includeNamedQueryScore;
184+
}
185+
180186
public List<Query.IdWithRouting> getIdsWithRouting() {
181187
return idsWithRouting;
182188
}
@@ -381,6 +387,11 @@ public SELF withRequestCache(@Nullable Boolean requestCache) {
381387
return self();
382388
}
383389

390+
public SELF withIncludeNamedQueryScore (@Nullable Boolean namedQueryScore) {
391+
this.includeNamedQueryScore = namedQueryScore;
392+
return self();
393+
}
394+
384395
/**
385396
* Set Ids with routing values for a multi-get request run with this query. Not used in any other searches.
386397
*

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ default Integer getReactiveBatchSize() {
491491
*/
492492
public Integer getRequestSize();
493493

494+
/**
495+
* Sets the include_named_queries_score value for the query.
496+
* If true, the response includes the score contribution from any named queries.
497+
*
498+
* @param value new value
499+
*/
500+
void setIncludeNamedQueriesScore(@Nullable Boolean value);
501+
502+
/**
503+
* @return the include_named_queries_score value for this query.
504+
*/
505+
@Nullable
506+
Boolean getIncludeNamedQueriesScore();
507+
494508
/**
495509
* @since 4.3
496510
*/

src/test/java/org/springframework/data/elasticsearch/core/query/NativeQueryIntegrationTests.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.util.List;
2424

25+
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
26+
import org.assertj.core.data.Offset;
2527
import org.jspecify.annotations.Nullable;
2628
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.DisplayName;
@@ -172,6 +174,68 @@ void shouldBeAbleToUseStringQueryInANativeQuery() {
172174
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
173175
}
174176

177+
@Test // #3248
178+
@DisplayName("should be able to use includeNamedQueriesScore in a NativeQuery")
179+
void shouldBeAbleToUseIncludeQueriesScoreInANativeQuery() {
180+
181+
var entity = new SampleEntity();
182+
entity.setId("7");
183+
entity.setText("seven");
184+
operations.save(entity);
185+
entity = new SampleEntity();
186+
entity.setId("42");
187+
entity.setText("matched");
188+
operations.save(entity);
189+
190+
var matchQuery = MatchQuery.of(m -> m.field("text")
191+
.query("matched")
192+
.queryName("namedQuery"));
193+
194+
var nativeQuery = NativeQuery.builder()
195+
.withQuery(matchQuery._toQuery())
196+
.withIncludeNamedQueryScore(true)
197+
.build();
198+
199+
var searchHits = operations.search(nativeQuery, SampleEntity.class);
200+
201+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
202+
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
203+
assertThat(searchHits.getSearchHit(0).getMatchedQueries()).containsKey("namedQuery");
204+
assertThat(searchHits.getSearchHit(0).getMatchedQueries().get("namedQuery")).isGreaterThan(0.0);
205+
assertThat(searchHits.getSearchHit(0).getMatchedQueries().get("namedQuery"))
206+
.isCloseTo(searchHits.getMaxScore(), Offset.offset(0.01));
207+
}
208+
209+
@Test // #3248
210+
@DisplayName("should not be able to use named queries score in a NativeQuery when disabled")
211+
void shouldNotBeAbleToUseNamedQueriesScoreInANativeQueryWhenDisabled() {
212+
213+
var entity = new SampleEntity();
214+
entity.setId("7");
215+
entity.setText("seven");
216+
operations.save(entity);
217+
entity = new SampleEntity();
218+
entity.setId("42");
219+
entity.setText("matched");
220+
operations.save(entity);
221+
222+
var matchQuery = MatchQuery.of(m -> m.field("text")
223+
.query("matched")
224+
.queryName("namedQuery"));
225+
226+
var nativeQuery = NativeQuery.builder()
227+
.withQuery(matchQuery._toQuery())
228+
.withIncludeNamedQueryScore(false)
229+
.build();
230+
231+
var searchHits = operations.search(nativeQuery, SampleEntity.class);
232+
233+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
234+
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
235+
assertThat(searchHits.getSearchHit(0).getMatchedQueries()).containsKey("namedQuery");
236+
assertThat(searchHits.getSearchHit(0).getMatchedQueries().get("namedQuery")).isNull();
237+
}
238+
175239
@Document(indexName = "#{@indexNameProvider.indexName()}")
176240
static class SampleEntity {
177241

0 commit comments

Comments
 (0)