Package org.elasticsearch.search.builder
Class SearchSourceBuilder
java.lang.Object
org.elasticsearch.search.builder.SearchSourceBuilder
- All Implemented Interfaces:
Writeable,ToXContent,ToXContentObject,Rewriteable<SearchSourceBuilder>
public final class SearchSourceBuilder extends java.lang.Object implements Writeable, ToXContentObject, Rewriteable<SearchSourceBuilder>
A search source builder allowing to easily build search source. Simple
construction using
searchSource().-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSearchSourceBuilder.IndexBooststatic classSearchSourceBuilder.ScriptFieldNested classes/interfaces inherited from interface org.elasticsearch.common.xcontent.ToXContent
ToXContent.DelegatingMapParams, ToXContent.MapParams, ToXContent.ParamsNested classes/interfaces inherited from interface org.elasticsearch.common.io.stream.Writeable
Writeable.Reader<V>, Writeable.Writer<V> -
Field Summary
-
Constructor Summary
Constructors Constructor Description SearchSourceBuilder()Constructs a new search source builder.SearchSourceBuilder(StreamInput in)Read from a stream. -
Method Summary
Modifier and Type Method Description SearchSourceBuilderaddRescorer(RescorerBuilder<?> rescoreBuilder)SearchSourceBuilderaggregation(AggregationBuilder aggregation)Add an aggregation to perform as part of the search.SearchSourceBuilderaggregation(PipelineAggregationBuilder aggregation)Add an aggregation to perform as part of the search.AggregatorFactories.Builderaggregations()Gets the bytes representing the aggregation builders for this request.SearchSourceBuilderclearRescorers()CollapseBuildercollapse()SearchSourceBuildercollapse(CollapseBuilder collapse)SearchSourceBuildercopyWithNewSlice(SliceBuilder slice)Create a shallow copy of this builder with a new slice configuration.SearchSourceBuilderdocValueField(java.lang.String name)Adds a field to load from the doc values and return as part of the search request.SearchSourceBuilderdocValueField(java.lang.String name, java.lang.String format)Adds a field to load from the doc values and return as part of the search request.java.util.List<FetchDocValuesContext.FieldAndFormat>docValueFields()Gets the docvalue fields.booleanequals(java.lang.Object obj)java.lang.Booleanexplain()Indicates whether each search hit will be returned with an explanation of the hit (ranking)SearchSourceBuilderexplain(java.lang.Boolean explain)Should eachSearchHitbe returned with an explanation of the hit (ranking).java.util.List<SearchExtBuilder>ext()SearchSourceBuilderext(java.util.List<SearchExtBuilder> searchExtBuilders)FetchSourceContextfetchSource()Gets theFetchSourceContextwhich defines how the _source should be fetched.SearchSourceBuilderfetchSource(boolean fetch)Indicates whether the response should contain the stored _source for every hitSearchSourceBuilderfetchSource(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.SearchSourceBuilderfetchSource(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.SearchSourceBuilderfetchSource(FetchSourceContext fetchSourceContext)Indicate how the _source should be fetched.intfrom()Gets the from index to start the search from.SearchSourceBuilderfrom(int from)From index to start the search from.static SearchSourceBuilderfromXContent(XContentParser parser)static SearchSourceBuilderfromXContent(XContentParser parser, boolean checkTrailingTokens)inthashCode()static HighlightBuilderhighlight()A static factory method to construct new search highlights.HighlightBuilderhighlighter()Gets the highlighter builder for this request.SearchSourceBuilderhighlighter(HighlightBuilder highlightBuilder)Adds highlight to perform as part of the search.SearchSourceBuilderindexBoost(java.lang.String index, float indexBoost)Sets the boost a specific index or alias will receive when the query is executed against it.java.util.List<SearchSourceBuilder.IndexBoost>indexBoosts()Gets the boost a specific indices or aliases will receive when the query is executed against them.XContentBuilderinnerToXContent(XContentBuilder builder, ToXContent.Params params)booleanisSuggestOnly()java.lang.FloatminScore()Gets the minimum score below which docs will be filtered out.SearchSourceBuilderminScore(float minScore)Sets the minimum score below which docs will be filtered out.voidparseXContent(XContentParser parser)voidparseXContent(XContentParser parser, boolean checkTrailingTokens)Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent.QueryBuilderpostFilter()Gets the post filter for this requestSearchSourceBuilderpostFilter(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).booleanprofile()Return whether to profile query execution, ornullif unspecified.SearchSourceBuilderprofile(boolean profile)Should the query be profiled.QueryBuilderquery()Gets the query for this requestSearchSourceBuilderquery(QueryBuilder query)Sets the search query for this request.java.util.List<RescorerBuilder>rescores()Gets the bytes representing the rescore builders for this request.SearchSourceBuilderrewrite(QueryRewriteContext context)Rewrites this search source builder into its primitive form.SearchSourceBuilderscriptField(java.lang.String name, Script script)Adds a script field under the given name with the provided script.SearchSourceBuilderscriptField(java.lang.String name, Script script, boolean ignoreFailure)Adds a script field under the given name with the provided script.java.util.List<SearchSourceBuilder.ScriptField>scriptFields()Gets the script fields.java.lang.Object[]searchAfter()The sort values that indicates which docs this request should "search after".SearchSourceBuildersearchAfter(java.lang.Object[] values)Set the sort values that indicates which docs this request should "search after".static SearchSourceBuildersearchSource()A static factory method to construct a new search source.java.lang.BooleanseqNoAndPrimaryTerm()Indicates whetherSearchHits should be returned with the sequence number and primary term of the last modification of the document.SearchSourceBuilderseqNoAndPrimaryTerm(java.lang.Boolean seqNoAndPrimaryTerm)Should eachSearchHitbe returned with the sequence number and primary term of the last modification of the document.intsize()Gets the number of search hits to return.SearchSourceBuildersize(int size)The number of search hits to return.SliceBuilderslice()Gets the slice used to filter the search hits, the top hits and the aggregations.SearchSourceBuilderslice(SliceBuilder builder)Sets a filter that will restrict the search hits, the top hits and the aggregations to a slice of the results of the main query.SearchSourceBuildersort(java.lang.String name)Add a sort against the given field name.SearchSourceBuildersort(java.lang.String name, SortOrder order)Adds a sort against the given field name and the sort ordering.SearchSourceBuildersort(SortBuilder<?> sort)Adds a sort builder.java.util.List<SortBuilder<?>>sorts()Gets the bytes representing the sort builders for this request.java.util.List<java.lang.String>stats()The stats groups this request will be aggregated under.SearchSourceBuilderstats(java.util.List<java.lang.String> statsGroups)The stats groups this request will be aggregated under.SearchSourceBuilderstoredField(java.lang.String name)Adds a stored field to load and return as part of the search request.StoredFieldsContextstoredFields()Gets the stored fields context.SearchSourceBuilderstoredFields(java.util.List<java.lang.String> fields)Sets the stored fields to load and return as part of the search request.SearchSourceBuilderstoredFields(StoredFieldsContext context)Indicates how the stored fields should be fetched.SuggestBuildersuggest()Gets the suggester builder for this request.SearchSourceBuildersuggest(SuggestBuilder suggestBuilder)intterminateAfter()Gets the number of documents to terminate after collecting.SearchSourceBuilderterminateAfter(int terminateAfter)An optional terminate_after to terminate the search after collectingterminateAfterdocumentsTimeValuetimeout()Gets the timeout to control how long search is allowed to take.SearchSourceBuildertimeout(TimeValue timeout)An optional timeout to control how long search is allowed to take.java.lang.StringtoString()java.lang.StringtoString(ToXContent.Params params)XContentBuildertoXContent(XContentBuilder builder, ToXContent.Params params)booleantrackScores()Indicates whether scores will be tracked for this request.SearchSourceBuildertrackScores(boolean trackScores)Applies when sorting, and controls if scores will be tracked as well.SearchSourceBuildertrackTotalHits(boolean trackTotalHits)Indicates if the total hit count for the query should be tracked.java.lang.IntegertrackTotalHitsUpTo()Returns the total hit count that should be tracked or null if the value is unset.SearchSourceBuildertrackTotalHitsUpTo(int trackTotalHitsUpTo)java.lang.Booleanversion()Indicates whether the document's version will be included in the search hits.SearchSourceBuilderversion(java.lang.Boolean version)Should eachSearchHitbe returned with a version associated with it.voidwriteTo(StreamOutput out)Write this into the StreamOutput.
-
Field Details
-
FROM_FIELD
-
SIZE_FIELD
-
TIMEOUT_FIELD
-
TERMINATE_AFTER_FIELD
-
QUERY_FIELD
-
POST_FILTER_FIELD
-
MIN_SCORE_FIELD
-
VERSION_FIELD
-
SEQ_NO_PRIMARY_TERM_FIELD
-
EXPLAIN_FIELD
-
_SOURCE_FIELD
-
STORED_FIELDS_FIELD
-
DOCVALUE_FIELDS_FIELD
-
SCRIPT_FIELDS_FIELD
-
SCRIPT_FIELD
-
IGNORE_FAILURE_FIELD
-
SORT_FIELD
-
TRACK_SCORES_FIELD
-
TRACK_TOTAL_HITS_FIELD
-
INDICES_BOOST_FIELD
-
AGGREGATIONS_FIELD
-
AGGS_FIELD
-
HIGHLIGHT_FIELD
-
SUGGEST_FIELD
-
RESCORE_FIELD
-
STATS_FIELD
-
EXT_FIELD
-
PROFILE_FIELD
-
SEARCH_AFTER
-
COLLAPSE
-
SLICE
-
-
Constructor Details
-
SearchSourceBuilder
public SearchSourceBuilder()Constructs a new search source builder. -
SearchSourceBuilder
Read from a stream.- Throws:
java.io.IOException
-
-
Method Details
-
fromXContent
- Throws:
java.io.IOException
-
fromXContent
public static SearchSourceBuilder fromXContent(XContentParser parser, boolean checkTrailingTokens) throws java.io.IOException- Throws:
java.io.IOException
-
searchSource
A static factory method to construct a new search source. -
highlight
A static factory method to construct new search highlights. -
writeTo
Description copied from interface:WriteableWrite this into the StreamOutput. -
query
Sets the search query for this request.- See Also:
QueryBuilders
-
query
Gets the query for this request -
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. -
postFilter
Gets the post filter for this request -
from
From index to start the search from. Defaults to0. -
from
public int from()Gets the from index to start the search from. -
size
The number of search hits to return. Defaults to10. -
size
public int size()Gets the number of search hits to return. -
minScore
Sets the minimum score below which docs will be filtered out. -
minScore
public java.lang.Float minScore()Gets the minimum score below which docs will be filtered out. -
explain
Should eachSearchHitbe returned with an explanation of the hit (ranking). -
explain
public java.lang.Boolean explain()Indicates whether each search hit will be returned with an explanation of the hit (ranking) -
version
Should eachSearchHitbe returned with a version associated with it. -
version
public java.lang.Boolean version()Indicates whether the document's version will be included in the search hits. -
seqNoAndPrimaryTerm
Should eachSearchHitbe returned with the sequence number and primary term of the last modification of the document. -
seqNoAndPrimaryTerm
public java.lang.Boolean seqNoAndPrimaryTerm()Indicates whetherSearchHits should be returned with the sequence number and primary term of the last modification of the document. -
timeout
An optional timeout to control how long search is allowed to take. -
timeout
Gets the timeout to control how long search is allowed to take. -
terminateAfter
An optional terminate_after to terminate the search after collectingterminateAfterdocuments -
terminateAfter
public int terminateAfter()Gets the number of documents to terminate after collecting. -
sort
Adds a sort against the given field name and the sort ordering.- Parameters:
name- The name of the fieldorder- The sort ordering
-
sort
Add a sort against the given field name.- Parameters:
name- The name of the field to sort by
-
sort
Adds a sort builder. -
sorts
Gets the bytes representing the sort builders for this request. -
trackScores
Applies when sorting, and controls if scores will be tracked as well. Defaults tofalse. -
trackScores
public boolean trackScores()Indicates whether scores will be tracked for this request. -
trackTotalHits
Indicates if the total hit count for the query should be tracked. -
trackTotalHitsUpTo
Returns the total hit count that should be tracked or null if the value is unset. Defaults to null. -
trackTotalHitsUpTo
-
searchAfter
public java.lang.Object[] searchAfter()The sort values that indicates which docs this request should "search after". The sort values of the search_after must be equal to the number of sort fields in the query and they should be of the same type (or parsable as such). Defaults tonull. -
searchAfter
Set the sort values that indicates which docs this request should "search after". -
slice
Sets a filter that will restrict the search hits, the top hits and the aggregations to a slice of the results of the main query. -
slice
Gets the slice used to filter the search hits, the top hits and the aggregations. -
collapse
-
collapse
-
aggregation
Add an aggregation to perform as part of the search. -
aggregation
Add an aggregation to perform as part of the search. -
aggregations
Gets the bytes representing the aggregation builders for this request. -
highlighter
Adds highlight to perform as part of the search. -
highlighter
Gets the highlighter builder for this request. -
suggest
-
suggest
Gets the suggester builder for this request. -
addRescorer
-
clearRescorers
-
profile
Should the query be profiled. Defaults tofalse -
profile
public boolean profile()Return whether to profile query execution, ornullif unspecified. -
rescores
Gets the bytes representing the rescore builders for this request. -
fetchSource
Indicates whether the response should contain the stored _source for every hit -
fetchSource
public SearchSourceBuilder fetchSource(@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
-
fetchSource
public SearchSourceBuilder fetchSource(@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
-
fetchSource
Indicate how the _source should be fetched. -
fetchSource
Gets theFetchSourceContextwhich defines how the _source should be fetched. -
storedField
Adds a stored field to load and return as part of the search request. If none are specified, the source of the document will be return. -
storedFields
Sets the stored fields to load and return as part of the search request. If none are specified, the source of the document will be returned. -
storedFields
Indicates how the stored fields should be fetched. -
storedFields
Gets the stored fields context. -
docValueFields
Gets the docvalue fields. -
docValueField
Adds a field to load from the doc values and return as part of the search request. -
docValueField
Adds a field to load from the doc values and return as part of the search request. -
scriptField
Adds a script field under the given name with the provided script.- Parameters:
name- The name of the fieldscript- The script
-
scriptField
public SearchSourceBuilder scriptField(java.lang.String name, Script script, boolean ignoreFailure)Adds a script field under the given name with the provided script.- Parameters:
name- The name of the fieldscript- The script
-
scriptFields
Gets the script fields. -
indexBoost
Sets the boost a specific index or alias will receive when the query is executed against it.- Parameters:
index- The index or alias to apply the boost againstindexBoost- The boost to apply to the index
-
indexBoosts
Gets the boost a specific indices or aliases will receive when the query is executed against them. -
stats
The stats groups this request will be aggregated under. -
stats
public java.util.List<java.lang.String> stats()The stats groups this request will be aggregated under. -
ext
-
ext
-
isSuggestOnly
public boolean isSuggestOnly()- Returns:
- true if the source only has suggest
-
rewrite
Rewrites this search source builder into its primitive form. e.g. by rewriting the QueryBuilder. If the builder did not change the identity reference must be returned otherwise the builder will be rewritten infinitely.- Specified by:
rewritein interfaceRewriteable<SearchSourceBuilder>- Throws:
java.io.IOException
-
copyWithNewSlice
Create a shallow copy of this builder with a new slice configuration. -
parseXContent
- Throws:
java.io.IOException
-
parseXContent
public void parseXContent(XContentParser parser, boolean checkTrailingTokens) throws java.io.IOExceptionParse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent. Use this if you need to set up different defaults than a regular SearchSourceBuilder would have and usefromXContent(XContentParser, boolean)if you have normal defaults.- Parameters:
parser- The xContent parser.checkTrailingTokens- If true throws a parsing exception when extra tokens are found after the main object.- Throws:
java.io.IOException
-
innerToXContent
public XContentBuilder innerToXContent(XContentBuilder builder, ToXContent.Params params) throws java.io.IOException- Throws:
java.io.IOException
-
toXContent
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws java.io.IOException- Specified by:
toXContentin interfaceToXContent- Throws:
java.io.IOException
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
toString
-