Package org.elasticsearch.action.search
Class SearchRequestBuilder
- java.lang.Object
-
- org.elasticsearch.action.ActionRequestBuilder<SearchRequest,SearchResponse,SearchRequestBuilder>
-
- org.elasticsearch.action.search.SearchRequestBuilder
-
public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest,SearchResponse,SearchRequestBuilder>
A search action request builder.
-
-
Field Summary
-
Fields inherited from class org.elasticsearch.action.ActionRequestBuilder
action, client, request
-
-
Constructor Summary
Constructors Constructor Description SearchRequestBuilder(ElasticsearchClient client, SearchAction action)
-
Method Summary
Modifier and Type Method Description SearchRequestBuilder
addAggregation(AggregationBuilder aggregation)
Adds an aggregation to the search operation.SearchRequestBuilder
addAggregation(PipelineAggregationBuilder aggregation)
Adds an aggregation to the search operation.SearchRequestBuilder
addDocValueField(java.lang.String name)
Adds a docvalue based field to load and return.SearchRequestBuilder
addDocValueField(java.lang.String name, java.lang.String format)
Adds a docvalue based field to load and return.SearchRequestBuilder
addIndexBoost(java.lang.String index, float indexBoost)
Sets the boost a specific index will receive when the query is executed against it.SearchRequestBuilder
addRescorer(RescorerBuilder<?> rescorer)
Adds a new rescorer.SearchRequestBuilder
addRescorer(RescorerBuilder<?> rescorer, int window)
Adds a new rescorer.SearchRequestBuilder
addScriptField(java.lang.String name, Script script)
Adds a script based field to load and return.SearchRequestBuilder
addSort(java.lang.String field, SortOrder order)
Adds a sort against the given field name and the sort ordering.SearchRequestBuilder
addSort(SortBuilder sort)
Adds a generic sort builder.SearchRequestBuilder
addStoredField(java.lang.String field)
Adds a stored field to load and return (note, it must be stored) as part of the search request.SearchRequestBuilder
clearRescorers()
Clears all rescorers from the builder.SearchRequestBuilder
fields(java.lang.String... fields)
Deprecated.UsestoredFields(String...)
instead.SearchRequestBuilder
highlighter(HighlightBuilder highlightBuilder)
SearchRequestBuilder
searchAfter(java.lang.Object[] values)
Set the sort values that indicates which docs this request should "search after".SearchRequestBuilder
seqNoAndPrimaryTerm(boolean seqNoAndPrimaryTerm)
Should eachSearchHit
be returned with the sequence number and primary term of the last modification of the document.SearchRequestBuilder
setAllowPartialSearchResults(boolean allowPartialSearchResults)
Sets if this request should allow partial results.SearchRequestBuilder
setBatchedReduceSize(int batchedReduceSize)
Sets the number of shard results that should be reduced at once on the coordinating node.SearchRequestBuilder
setCollapse(CollapseBuilder collapse)
SearchRequestBuilder
setExplain(boolean explain)
Should eachSearchHit
be returned with an explanation of the hit (ranking).SearchRequestBuilder
setFetchSource(boolean fetch)
Indicates whether the response should contain the stored _source for every hitSearchRequestBuilder
setFetchSource(java.lang.String[] includes, java.lang.String[] excludes)
Indicate that _source should be returned with every hit, with an "include" and/or "exclude" set which can include simple wildcard elements.SearchRequestBuilder
setFetchSource(java.lang.String include, java.lang.String exclude)
Indicate that _source should be returned with every hit, with an "include" and/or "exclude" set which can include simple wildcard elements.SearchRequestBuilder
setFrom(int from)
From index to start the search from.SearchRequestBuilder
setIndices(java.lang.String... indices)
Sets the indices the search will be executed on.SearchRequestBuilder
setIndicesOptions(IndicesOptions indicesOptions)
Specifies what type of requested indices to ignore and wildcard indices expressions.SearchRequestBuilder
setMaxConcurrentShardRequests(int maxConcurrentShardRequests)
Sets the number of shard requests that should be executed concurrently.SearchRequestBuilder
setMinScore(float minScore)
Sets the minimum score below which docs will be filtered out.SearchRequestBuilder
setPostFilter(QueryBuilder postFilter)
Sets a filter that will be executed after the query has been executed and only has affect on the search hits (not aggregations).SearchRequestBuilder
setPreference(java.lang.String preference)
Sets the preference to execute the search.SearchRequestBuilder
setPreFilterShardSize(int preFilterShardSize)
Sets a threshold that enforces a pre-filter roundtrip to pre-filter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold.SearchRequestBuilder
setProfile(boolean profile)
Should the query be profiled.SearchRequestBuilder
setQuery(QueryBuilder queryBuilder)
Constructs a new search source builder with a search query.SearchRequestBuilder
setRequestCache(java.lang.Boolean requestCache)
Sets if this request should use the request cache or not, assuming that it can (for example, if "now" is used, it will never be cached).SearchRequestBuilder
setRescorer(RescorerBuilder<?> rescorer)
Clears all rescorers on the builder and sets the first one.SearchRequestBuilder
setRescorer(RescorerBuilder rescorer, int window)
Clears all rescorers on the builder and sets the first one.SearchRequestBuilder
setRouting(java.lang.String routing)
A comma separated list of routing values to control the shards the search will be executed on.SearchRequestBuilder
setRouting(java.lang.String... routing)
The routing values to control the shards that the search will be executed on.SearchRequestBuilder
setScroll(java.lang.String keepAlive)
If set, will enable scrolling of the search request for the specified timeout.SearchRequestBuilder
setScroll(TimeValue keepAlive)
If set, will enable scrolling of the search request for the specified timeout.SearchRequestBuilder
setScroll(Scroll scroll)
If set, will enable scrolling of the search request.SearchRequestBuilder
setSearchType(java.lang.String searchType)
The a string representation search type to execute, defaults toSearchType.DEFAULT
.SearchRequestBuilder
setSearchType(SearchType searchType)
The search type to execute, defaults toSearchType.DEFAULT
.SearchRequestBuilder
setSize(int size)
The number of search hits to return.SearchRequestBuilder
setSource(SearchSourceBuilder source)
Sets the source of the request as a SearchSourceBuilder.SearchRequestBuilder
setStats(java.lang.String... statsGroups)
The stats groups this request will be aggregated under.SearchRequestBuilder
setStats(java.util.List<java.lang.String> statsGroups)
The stats groups this request will be aggregated under.SearchRequestBuilder
setTerminateAfter(int terminateAfter)
An optional document count, upon collecting which the search query will early terminateSearchRequestBuilder
setTimeout(TimeValue timeout)
An optional timeout to control how long search is allowed to take.SearchRequestBuilder
setTrackScores(boolean trackScores)
Applies when sorting, and controls if scores will be tracked as well.SearchRequestBuilder
setTrackTotalHits(boolean trackTotalHits)
Indicates if the total hit count for the query should be tracked.SearchRequestBuilder
setTypes(java.lang.String... types)
The document types to execute the search against.SearchRequestBuilder
setVersion(boolean version)
Should eachSearchHit
be returned with its version.SearchRequestBuilder
slice(SliceBuilder builder)
SearchRequestBuilder
storedFields(java.lang.String... fields)
Adds stored fields to load and return (note, it must be stored) as part of the search request.SearchRequestBuilder
suggest(SuggestBuilder suggestBuilder)
Delegates toSearchSourceBuilder.suggest(SuggestBuilder)
java.lang.String
toString()
-
-
-
Constructor Detail
-
SearchRequestBuilder
public SearchRequestBuilder(ElasticsearchClient client, SearchAction action)
-
-
Method Detail
-
setIndices
public SearchRequestBuilder setIndices(java.lang.String... indices)
Sets the indices the search will be executed on.
-
setTypes
public SearchRequestBuilder setTypes(java.lang.String... types)
The document types to execute the search against. Defaults to be executed against all types.
-
setSearchType
public SearchRequestBuilder setSearchType(SearchType searchType)
The search type to execute, defaults toSearchType.DEFAULT
.
-
setSearchType
public SearchRequestBuilder setSearchType(java.lang.String searchType)
The a string representation search type to execute, defaults toSearchType.DEFAULT
. Can be one of "dfs_query_then_fetch"/"dfsQueryThenFetch", "dfs_query_and_fetch"/"dfsQueryAndFetch", "query_then_fetch"/"queryThenFetch", and "query_and_fetch"/"queryAndFetch".
-
setScroll
public SearchRequestBuilder setScroll(Scroll scroll)
If set, will enable scrolling of the search request.
-
setScroll
public SearchRequestBuilder setScroll(TimeValue keepAlive)
If set, will enable scrolling of the search request for the specified timeout.
-
setScroll
public SearchRequestBuilder setScroll(java.lang.String keepAlive)
If set, will enable scrolling of the search request for the specified timeout.
-
setTimeout
public SearchRequestBuilder setTimeout(TimeValue timeout)
An optional timeout to control how long search is allowed to take.
-
setTerminateAfter
public SearchRequestBuilder setTerminateAfter(int terminateAfter)
An optional document count, upon collecting which the search query will early terminate
-
setRouting
public SearchRequestBuilder setRouting(java.lang.String routing)
A comma separated list of routing values to control the shards the search will be executed on.
-
setRouting
public SearchRequestBuilder setRouting(java.lang.String... routing)
The routing values to control the shards that the search will be executed on.
-
setPreference
public SearchRequestBuilder setPreference(java.lang.String preference)
Sets the preference to execute the search. Defaults to randomize across shards. Can be set to_local
to prefer local shards,_primary
to execute only on primary shards, or a custom value, which guarantees that the same order will be used across different requests.
-
setIndicesOptions
public SearchRequestBuilder setIndicesOptions(IndicesOptions indicesOptions)
Specifies what type of requested indices to ignore and wildcard indices expressions.For example indices that don't exist.
-
setQuery
public SearchRequestBuilder setQuery(QueryBuilder queryBuilder)
Constructs a new search source builder with a search query.- See Also:
QueryBuilders
-
setPostFilter
public SearchRequestBuilder setPostFilter(QueryBuilder postFilter)
Sets a filter that will be executed after the query has been executed and only has affect on the search hits (not aggregations). This filter is always executed as last filtering mechanism.
-
setMinScore
public SearchRequestBuilder setMinScore(float minScore)
Sets the minimum score below which docs will be filtered out.
-
setFrom
public SearchRequestBuilder setFrom(int from)
From index to start the search from. Defaults to0
.
-
setSize
public SearchRequestBuilder setSize(int size)
The number of search hits to return. Defaults to10
.
-
setExplain
public SearchRequestBuilder setExplain(boolean explain)
Should eachSearchHit
be returned with an explanation of the hit (ranking).
-
setVersion
public SearchRequestBuilder setVersion(boolean version)
Should eachSearchHit
be returned with its version.
-
seqNoAndPrimaryTerm
public SearchRequestBuilder seqNoAndPrimaryTerm(boolean seqNoAndPrimaryTerm)
Should eachSearchHit
be returned with the sequence number and primary term of the last modification of the document.
-
addIndexBoost
public SearchRequestBuilder addIndexBoost(java.lang.String index, float indexBoost)
Sets the boost a specific index will receive when the query is executed against it.- Parameters:
index
- The index to apply the boost againstindexBoost
- The boost to apply to the index
-
setStats
public SearchRequestBuilder setStats(java.lang.String... statsGroups)
The stats groups this request will be aggregated under.
-
setStats
public SearchRequestBuilder setStats(java.util.List<java.lang.String> statsGroups)
The stats groups this request will be aggregated under.
-
setFetchSource
public SearchRequestBuilder setFetchSource(boolean fetch)
Indicates whether the response should contain the stored _source for every hit
-
setFetchSource
public SearchRequestBuilder setFetchSource(@Nullable java.lang.String include, @Nullable java.lang.String exclude)
Indicate that _source should be returned with every hit, with an "include" and/or "exclude" set which can include simple wildcard elements.- Parameters:
include
- An optional include (optionally wildcarded) pattern to filter the returned _sourceexclude
- An optional exclude (optionally wildcarded) pattern to filter the returned _source
-
setFetchSource
public SearchRequestBuilder setFetchSource(@Nullable java.lang.String[] includes, @Nullable java.lang.String[] excludes)
Indicate that _source should be returned with every hit, with an "include" and/or "exclude" set which can include simple wildcard elements.- Parameters:
includes
- An optional list of include (optionally wildcarded) pattern to filter the returned _sourceexcludes
- An optional list of exclude (optionally wildcarded) pattern to filter the returned _source
-
addDocValueField
public SearchRequestBuilder addDocValueField(java.lang.String name, java.lang.String format)
Adds a docvalue based field to load and return. The field does not have to be stored, but its recommended to use non analyzed or numeric fields.- Parameters:
name
- The field to get from the docvalue
-
addDocValueField
public SearchRequestBuilder addDocValueField(java.lang.String name)
Adds a docvalue based field to load and return. The field does not have to be stored, but its recommended to use non analyzed or numeric fields.- Parameters:
name
- The field to get from the docvalue
-
addStoredField
public SearchRequestBuilder addStoredField(java.lang.String field)
Adds a stored field to load and return (note, it must be stored) as part of the search request.
-
addScriptField
public SearchRequestBuilder addScriptField(java.lang.String name, Script script)
Adds a script based field to load and return. The field does not have to be stored, but its recommended to use non analyzed or numeric fields.- Parameters:
name
- The name that will represent this value in the return hitscript
- The script to use
-
addSort
public SearchRequestBuilder addSort(java.lang.String field, SortOrder order)
Adds a sort against the given field name and the sort ordering.- Parameters:
field
- The name of the fieldorder
- The sort ordering
-
addSort
public SearchRequestBuilder addSort(SortBuilder sort)
Adds a generic sort builder.- See Also:
SortBuilders
-
searchAfter
public SearchRequestBuilder searchAfter(java.lang.Object[] values)
Set the sort values that indicates which docs this request should "search after".
-
slice
public SearchRequestBuilder slice(SliceBuilder builder)
-
setTrackScores
public SearchRequestBuilder setTrackScores(boolean trackScores)
Applies when sorting, and controls if scores will be tracked as well. Defaults tofalse
.
-
setTrackTotalHits
public SearchRequestBuilder setTrackTotalHits(boolean trackTotalHits)
Indicates if the total hit count for the query should be tracked. Defaults totrue
-
fields
@Deprecated public SearchRequestBuilder fields(java.lang.String... fields)
Deprecated.UsestoredFields(String...)
instead.Adds stored fields to load and return (note, it must be stored) as part of the search request. To disable the stored fields entirely (source and metadata fields) usestoredField("_none_")
.
-
storedFields
public SearchRequestBuilder storedFields(java.lang.String... fields)
Adds stored fields to load and return (note, it must be stored) as part of the search request. To disable the stored fields entirely (source and metadata fields) usestoredField("_none_")
.
-
addAggregation
public SearchRequestBuilder addAggregation(AggregationBuilder aggregation)
Adds an aggregation to the search operation.
-
addAggregation
public SearchRequestBuilder addAggregation(PipelineAggregationBuilder aggregation)
Adds an aggregation to the search operation.
-
highlighter
public SearchRequestBuilder highlighter(HighlightBuilder highlightBuilder)
-
suggest
public SearchRequestBuilder suggest(SuggestBuilder suggestBuilder)
Delegates toSearchSourceBuilder.suggest(SuggestBuilder)
-
setRescorer
public SearchRequestBuilder setRescorer(RescorerBuilder<?> rescorer)
Clears all rescorers on the builder and sets the first one. To use multiple rescore windows useaddRescorer(org.elasticsearch.search.rescore.RescorerBuilder, int)
.- Parameters:
rescorer
- rescorer configuration- Returns:
- this for chaining
-
setRescorer
public SearchRequestBuilder setRescorer(RescorerBuilder rescorer, int window)
Clears all rescorers on the builder and sets the first one. To use multiple rescore windows useaddRescorer(org.elasticsearch.search.rescore.RescorerBuilder, int)
.- Parameters:
rescorer
- rescorer configurationwindow
- rescore window- Returns:
- this for chaining
-
addRescorer
public SearchRequestBuilder addRescorer(RescorerBuilder<?> rescorer)
Adds a new rescorer.- Parameters:
rescorer
- rescorer configuration- Returns:
- this for chaining
-
addRescorer
public SearchRequestBuilder addRescorer(RescorerBuilder<?> rescorer, int window)
Adds a new rescorer.- Parameters:
rescorer
- rescorer configurationwindow
- rescore window- Returns:
- this for chaining
-
clearRescorers
public SearchRequestBuilder clearRescorers()
Clears all rescorers from the builder.- Returns:
- this for chaining
-
setSource
public SearchRequestBuilder setSource(SearchSourceBuilder source)
Sets the source of the request as a SearchSourceBuilder.
-
setRequestCache
public SearchRequestBuilder setRequestCache(java.lang.Boolean requestCache)
Sets if this request should use the request cache or not, assuming that it can (for example, if "now" is used, it will never be cached). By default (not set, or null, will default to the index level setting if request cache is enabled or not).
-
setAllowPartialSearchResults
public SearchRequestBuilder setAllowPartialSearchResults(boolean allowPartialSearchResults)
Sets if this request should allow partial results. (If method is not called, will default to the cluster level setting).
-
setProfile
public SearchRequestBuilder setProfile(boolean profile)
Should the query be profiled. Defaults tofalse
-
setCollapse
public SearchRequestBuilder setCollapse(CollapseBuilder collapse)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
setBatchedReduceSize
public SearchRequestBuilder setBatchedReduceSize(int batchedReduceSize)
Sets the number of shard results that should be reduced at once on the coordinating node. This value should be used as a protection mechanism to reduce the memory overhead per search request if the potential number of shards in the request can be large.
-
setMaxConcurrentShardRequests
public SearchRequestBuilder setMaxConcurrentShardRequests(int maxConcurrentShardRequests)
Sets the number of shard requests that should be executed concurrently. This value should be used as a protection mechanism to reduce the number of shard requests fired per high level search request. Searches that hit the entire cluster can be throttled with this number to reduce the cluster load. The default grows with the number of nodes in the cluster but is at most256
.
-
setPreFilterShardSize
public SearchRequestBuilder setPreFilterShardSize(int preFilterShardSize)
Sets a threshold that enforces a pre-filter roundtrip to pre-filter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold. This filter roundtrip can limit the number of shards significantly if for instance a shard can not match any documents based on it's rewrite method ie. if date filters are mandatory to match but the shard bounds and the query are disjoint. The default is128
-
-