DATAES-462 - Add query builder option to track scores.

Original pull request: #207.
This commit is contained in:
xhaggi 2018-06-13 12:07:43 +02:00 committed by Oliver Gierke
parent 62a03a8fb7
commit 112600261d
4 changed files with 48 additions and 18 deletions

View File

@ -991,7 +991,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices())) SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices()))
.setSearchType(query.getSearchType()) .setSearchType(query.getSearchType())
.setTypes(toArray(query.getTypes())) .setTypes(toArray(query.getTypes()))
.setVersion(true); .setVersion(true)
.setTrackScores(query.getTrackScores());
if (query.getSourceFilter() != null) { if (query.getSourceFilter() != null) {
SourceFilter sourceFilter = query.getSourceFilter(); SourceFilter sourceFilter = query.getSourceFilter();

View File

@ -15,10 +15,12 @@
*/ */
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import static java.util.Collections.addAll; import static java.util.Collections.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -32,6 +34,7 @@ import org.springframework.util.Assert;
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Alen Turkovic * @author Alen Turkovic
* @author Sascha Woo
*/ */
abstract class AbstractQuery implements Query { abstract class AbstractQuery implements Query {
@ -46,6 +49,7 @@ abstract class AbstractQuery implements Query {
protected String route; protected String route;
protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH; protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH;
protected IndicesOptions indicesOptions; protected IndicesOptions indicesOptions;
protected boolean trackScores;
@Override @Override
public Sort getSort() { public Sort getSort() {
@ -160,4 +164,13 @@ abstract class AbstractQuery implements Query {
public void setIndicesOptions(IndicesOptions indicesOptions) { public void setIndicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions; this.indicesOptions = indicesOptions;
} }
@Override
public boolean getTrackScores() {
return trackScores;
}
public void setTrackScores(boolean trackScores) {
this.trackScores = trackScores;
}
} }

View File

@ -36,6 +36,7 @@ import org.springframework.data.elasticsearch.core.facet.FacetRequest;
* @author Artur Konczak * @author Artur Konczak
* @author Mark Paluch * @author Mark Paluch
* @author Alen Turkovic * @author Alen Turkovic
* @author Sascha Woo
*/ */
public class NativeSearchQueryBuilder { public class NativeSearchQueryBuilder {
@ -53,6 +54,7 @@ public class NativeSearchQueryBuilder {
private SourceFilter sourceFilter; private SourceFilter sourceFilter;
private List<IndexBoost> indicesBoost; private List<IndexBoost> indicesBoost;
private float minScore; private float minScore;
private boolean trackScores;
private Collection<String> ids; private Collection<String> ids;
private String route; private String route;
private SearchType searchType; private SearchType searchType;
@ -128,6 +130,11 @@ public class NativeSearchQueryBuilder {
return this; return this;
} }
public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
this.trackScores = trackScores;
return this;
}
public NativeSearchQueryBuilder withIds(Collection<String> ids) { public NativeSearchQueryBuilder withIds(Collection<String> ids) {
this.ids = ids; this.ids = ids;
return this; return this;
@ -149,8 +156,11 @@ public class NativeSearchQueryBuilder {
} }
public NativeSearchQuery build() { public NativeSearchQuery build() {
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields); NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders,
highlightFields);
nativeSearchQuery.setPageable(pageable); nativeSearchQuery.setPageable(pageable);
nativeSearchQuery.setTrackScores(trackScores);
if (indices != null) { if (indices != null) {
nativeSearchQuery.addIndices(indices); nativeSearchQuery.addIndices(indices);

View File

@ -30,6 +30,7 @@ import org.springframework.data.domain.Sort;
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch * @author Mark Paluch
* @author Alen Turkovic * @author Alen Turkovic
* @author Sascha Woo
*/ */
public interface Query { public interface Query {
@ -114,8 +115,7 @@ public interface Query {
void addSourceFilter(SourceFilter sourceFilter); void addSourceFilter(SourceFilter sourceFilter);
/** /**
* Get SourceFilter to be returned to get include and exclude source * Get SourceFilter to be returned to get include and exclude source fields as part of search request.
* fields as part of search request.
* *
* @return SourceFilter * @return SourceFilter
*/ */
@ -128,6 +128,13 @@ public interface Query {
*/ */
float getMinScore(); float getMinScore();
/**
* Get if scores will be computed and tracked, regardless of whether sorting on a field. Defaults to <tt>false</tt>.
*
* @return
*/
boolean getTrackScores();
/** /**
* Get Ids * Get Ids
* *
@ -142,7 +149,6 @@ public interface Query {
*/ */
String getRoute(); String getRoute();
/** /**
* Type of search * Type of search
* *