mirror of
				https://github.com/spring-projects/spring-data-elasticsearch.git
				synced 2025-10-31 06:38:44 +00:00 
			
		
		
		
	DATAES-462 - Add query builder option to track scores.
Original pull request: #207.
This commit is contained in:
		
							parent
							
								
									62a03a8fb7
								
							
						
					
					
						commit
						112600261d
					
				| @ -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(); | ||||||
|  | |||||||
| @ -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; | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -36,12 +36,13 @@ 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 { | ||||||
| 
 | 
 | ||||||
| 	private QueryBuilder queryBuilder; | 	private QueryBuilder queryBuilder; | ||||||
| 	private QueryBuilder filterBuilder; | 	private QueryBuilder filterBuilder; | ||||||
|     private List<ScriptField> scriptFields = new ArrayList<>(); | 	private List<ScriptField> scriptFields = new ArrayList<>(); | ||||||
| 	private List<SortBuilder> sortBuilders = new ArrayList<>(); | 	private List<SortBuilder> sortBuilders = new ArrayList<>(); | ||||||
| 	private List<FacetRequest> facetRequests = new ArrayList<>(); | 	private List<FacetRequest> facetRequests = new ArrayList<>(); | ||||||
| 	private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>(); | 	private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>(); | ||||||
| @ -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; | ||||||
| @ -73,10 +75,10 @@ public class NativeSearchQueryBuilder { | |||||||
| 		return this; | 		return this; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|     public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) { | 	public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) { | ||||||
|         this.scriptFields.add(scriptField); | 		this.scriptFields.add(scriptField); | ||||||
|         return this; | 		return this; | ||||||
|     } | 	} | ||||||
| 
 | 
 | ||||||
| 	public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder aggregationBuilder) { | 	public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder aggregationBuilder) { | ||||||
| 		this.aggregationBuilders.add(aggregationBuilder); | 		this.aggregationBuilders.add(aggregationBuilder); | ||||||
| @ -119,8 +121,8 @@ public class NativeSearchQueryBuilder { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) { | 	public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) { | ||||||
| 				this.sourceFilter = sourceFilter; | 		this.sourceFilter = sourceFilter; | ||||||
| 				return this; | 		return this; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public NativeSearchQueryBuilder withMinScore(float minScore) { | 	public NativeSearchQueryBuilder withMinScore(float minScore) { | ||||||
| @ -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); | ||||||
| @ -168,13 +178,13 @@ public class NativeSearchQueryBuilder { | |||||||
| 			nativeSearchQuery.addSourceFilter(sourceFilter); | 			nativeSearchQuery.addSourceFilter(sourceFilter); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if(indicesBoost != null) { | 		if (indicesBoost != null) { | ||||||
| 		    nativeSearchQuery.setIndicesBoost(indicesBoost); | 			nativeSearchQuery.setIndicesBoost(indicesBoost); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|         if (!isEmpty(scriptFields)) { | 		if (!isEmpty(scriptFields)) { | ||||||
|             nativeSearchQuery.setScriptFields(scriptFields); | 			nativeSearchQuery.setScriptFields(scriptFields); | ||||||
|         } | 		} | ||||||
| 
 | 
 | ||||||
| 		if (!isEmpty(facetRequests)) { | 		if (!isEmpty(facetRequests)) { | ||||||
| 			nativeSearchQuery.setFacets(facetRequests); | 			nativeSearchQuery.setFacets(facetRequests); | ||||||
|  | |||||||
| @ -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 | ||||||
| 	 * | 	 * | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user