Skip to content

Commit 69a15c7

Browse files
committed
DATAES-462 - add query builder option to track scores
1 parent 28629a6 commit 69a15c7

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,8 @@ private SearchRequestBuilder prepareSearch(Query query) {
991991
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices()))
992992
.setSearchType(query.getSearchType())
993993
.setTypes(toArray(query.getTypes()))
994-
.setVersion(true);
994+
.setVersion(true)
995+
.setTrackScores(query.getTrackScores());
995996

996997
if (query.getSourceFilter() != null) {
997998
SourceFilter sourceFilter = query.getSourceFilter();

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.query;
1717

18-
import static java.util.Collections.addAll;
18+
import static java.util.Collections.*;
19+
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.List;
23+
2224
import org.elasticsearch.action.search.SearchType;
2325
import org.elasticsearch.action.support.IndicesOptions;
2426
import org.springframework.data.domain.Pageable;
@@ -32,6 +34,7 @@
3234
* @author Mohsin Husen
3335
* @author Mark Paluch
3436
* @author Alen Turkovic
37+
* @author Sascha Woo
3538
*/
3639
abstract class AbstractQuery implements Query {
3740

@@ -46,6 +49,7 @@ abstract class AbstractQuery implements Query {
4649
protected String route;
4750
protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH;
4851
protected IndicesOptions indicesOptions;
52+
protected boolean trackScores;
4953

5054
@Override
5155
public Sort getSort() {
@@ -160,4 +164,13 @@ public IndicesOptions getIndicesOptions() {
160164
public void setIndicesOptions(IndicesOptions indicesOptions) {
161165
this.indicesOptions = indicesOptions;
162166
}
167+
168+
@Override
169+
public boolean getTrackScores() {
170+
return trackScores;
171+
}
172+
173+
public void setTrackScores(boolean trackScores) {
174+
this.trackScores = trackScores;
175+
}
163176
}

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
* @author Artur Konczak
3737
* @author Mark Paluch
3838
* @author Alen Turkovic
39+
* @author Sascha Woo
3940
*/
4041
public class NativeSearchQueryBuilder {
4142

4243
private QueryBuilder queryBuilder;
4344
private QueryBuilder filterBuilder;
44-
private List<ScriptField> scriptFields = new ArrayList<>();
45+
private List<ScriptField> scriptFields = new ArrayList<>();
4546
private List<SortBuilder> sortBuilders = new ArrayList<>();
4647
private List<FacetRequest> facetRequests = new ArrayList<>();
4748
private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>();
@@ -53,6 +54,7 @@ public class NativeSearchQueryBuilder {
5354
private SourceFilter sourceFilter;
5455
private List<IndexBoost> indicesBoost;
5556
private float minScore;
57+
private boolean trackScores;
5658
private Collection<String> ids;
5759
private String route;
5860
private SearchType searchType;
@@ -73,10 +75,10 @@ public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder) {
7375
return this;
7476
}
7577

76-
public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) {
77-
this.scriptFields.add(scriptField);
78-
return this;
79-
}
78+
public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) {
79+
this.scriptFields.add(scriptField);
80+
return this;
81+
}
8082

8183
public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder aggregationBuilder) {
8284
this.aggregationBuilders.add(aggregationBuilder);
@@ -119,15 +121,20 @@ public NativeSearchQueryBuilder withFields(String... fields) {
119121
}
120122

121123
public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) {
122-
this.sourceFilter = sourceFilter;
123-
return this;
124+
this.sourceFilter = sourceFilter;
125+
return this;
124126
}
125127

126128
public NativeSearchQueryBuilder withMinScore(float minScore) {
127129
this.minScore = minScore;
128130
return this;
129131
}
130132

133+
public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
134+
this.trackScores = trackScores;
135+
return this;
136+
}
137+
131138
public NativeSearchQueryBuilder withIds(Collection<String> ids) {
132139
this.ids = ids;
133140
return this;
@@ -149,8 +156,11 @@ public NativeSearchQueryBuilder withIndicesOptions(IndicesOptions indicesOptions
149156
}
150157

151158
public NativeSearchQuery build() {
152-
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
159+
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders,
160+
highlightFields);
161+
153162
nativeSearchQuery.setPageable(pageable);
163+
nativeSearchQuery.setTrackScores(trackScores);
154164

155165
if (indices != null) {
156166
nativeSearchQuery.addIndices(indices);
@@ -168,13 +178,13 @@ public NativeSearchQuery build() {
168178
nativeSearchQuery.addSourceFilter(sourceFilter);
169179
}
170180

171-
if(indicesBoost != null) {
172-
nativeSearchQuery.setIndicesBoost(indicesBoost);
181+
if (indicesBoost != null) {
182+
nativeSearchQuery.setIndicesBoost(indicesBoost);
173183
}
174184

175-
if (!isEmpty(scriptFields)) {
176-
nativeSearchQuery.setScriptFields(scriptFields);
177-
}
185+
if (!isEmpty(scriptFields)) {
186+
nativeSearchQuery.setScriptFields(scriptFields);
187+
}
178188

179189
if (!isEmpty(facetRequests)) {
180190
nativeSearchQuery.setFacets(facetRequests);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Mohsin Husen
3131
* @author Mark Paluch
3232
* @author Alen Turkovic
33+
* @author Sascha Woo
3334
*/
3435
public interface Query {
3536

@@ -114,8 +115,7 @@ public interface Query {
114115
void addSourceFilter(SourceFilter sourceFilter);
115116

116117
/**
117-
* Get SourceFilter to be returned to get include and exclude source
118-
* fields as part of search request.
118+
* Get SourceFilter to be returned to get include and exclude source fields as part of search request.
119119
*
120120
* @return SourceFilter
121121
*/
@@ -128,6 +128,13 @@ public interface Query {
128128
*/
129129
float getMinScore();
130130

131+
/**
132+
* Get if scores will be computed and tracked, regardless of whether sorting on a field. Defaults to <tt>false</tt>.
133+
*
134+
* @return
135+
*/
136+
boolean getTrackScores();
137+
131138
/**
132139
* Get Ids
133140
*
@@ -142,7 +149,6 @@ public interface Query {
142149
*/
143150
String getRoute();
144151

145-
146152
/**
147153
* Type of search
148154
*

0 commit comments

Comments
 (0)