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 usingsearchSource()
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SearchSourceBuilder.IndexBoost
static class
SearchSourceBuilder.ScriptField
-
Nested classes/interfaces inherited from interface org.elasticsearch.common.xcontent.ToXContent
ToXContent.DelegatingMapParams, ToXContent.MapParams, ToXContent.Params
-
Nested classes/interfaces inherited from interface org.elasticsearch.common.io.stream.Writeable
Writeable.Reader<V>, Writeable.Writer<V>
-
-
Field Summary
-
Fields inherited from interface org.elasticsearch.index.query.Rewriteable
MAX_REWRITE_ROUNDS
-
Fields inherited from interface org.elasticsearch.common.xcontent.ToXContent
EMPTY_PARAMS
-
-
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 SearchSourceBuilder
addRescorer(RescorerBuilder<?> rescoreBuilder)
SearchSourceBuilder
aggregation(AggregationBuilder aggregation)
Add an aggregation to perform as part of the search.SearchSourceBuilder
aggregation(PipelineAggregationBuilder aggregation)
Add an aggregation to perform as part of the search.AggregatorFactories.Builder
aggregations()
Gets the bytes representing the aggregation builders for this request.SearchSourceBuilder
clearRescorers()
CollapseBuilder
collapse()
SearchSourceBuilder
collapse(CollapseBuilder collapse)
SearchSourceBuilder
copyWithNewSlice(SliceBuilder slice)
Create a shallow copy of this builder with a new slice configuration.SearchSourceBuilder
docValueField(java.lang.String name)
Adds a field to load from the doc values and return as part of the search request.SearchSourceBuilder
docValueField(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<DocValueFieldsContext.FieldAndFormat>
docValueFields()
Gets the docvalue fields.boolean
equals(java.lang.Object obj)
java.lang.Boolean
explain()
Indicates whether each search hit will be returned with an explanation of the hit (ranking)SearchSourceBuilder
explain(java.lang.Boolean explain)
Should eachSearchHit
be returned with an explanation of the hit (ranking).java.util.List<SearchExtBuilder>
ext()
SearchSourceBuilder
ext(java.util.List<SearchExtBuilder> searchExtBuilders)
FetchSourceContext
fetchSource()
Gets theFetchSourceContext
which defines how the _source should be fetched.SearchSourceBuilder
fetchSource(boolean fetch)
Indicates whether the response should contain the stored _source for every hitSearchSourceBuilder
fetchSource(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.SearchSourceBuilder
fetchSource(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.SearchSourceBuilder
fetchSource(FetchSourceContext fetchSourceContext)
Indicate how the _source should be fetched.int
from()
Gets the from index to start the search from.SearchSourceBuilder
from(int from)
From index to start the search from.static SearchSourceBuilder
fromXContent(XContentParser parser)
static SearchSourceBuilder
fromXContent(XContentParser parser, boolean checkTrailingTokens)
int
hashCode()
static HighlightBuilder
highlight()
A static factory method to construct new search highlights.HighlightBuilder
highlighter()
Gets the highlighter builder for this request.SearchSourceBuilder
highlighter(HighlightBuilder highlightBuilder)
Adds highlight to perform as part of the search.SearchSourceBuilder
indexBoost(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.XContentBuilder
innerToXContent(XContentBuilder builder, ToXContent.Params params)
boolean
isSuggestOnly()
java.lang.Float
minScore()
Gets the minimum score below which docs will be filtered out.SearchSourceBuilder
minScore(float minScore)
Sets the minimum score below which docs will be filtered out.void
parseXContent(XContentParser parser)
void
parseXContent(XContentParser parser, boolean checkTrailingTokens)
Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent.QueryBuilder
postFilter()
Gets the post filter for this requestSearchSourceBuilder
postFilter(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).boolean
profile()
Return whether to profile query execution, ornull
if unspecified.SearchSourceBuilder
profile(boolean profile)
Should the query be profiled.QueryBuilder
query()
Gets the query for this requestSearchSourceBuilder
query(QueryBuilder query)
Sets the search query for this request.java.util.List<RescorerBuilder>
rescores()
Gets the bytes representing the rescore builders for this request.SearchSourceBuilder
rewrite(QueryRewriteContext context)
Rewrites this search source builder into its primitive form.SearchSourceBuilder
scriptField(java.lang.String name, Script script)
Adds a script field under the given name with the provided script.SearchSourceBuilder
scriptField(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".SearchSourceBuilder
searchAfter(java.lang.Object[] values)
Set the sort values that indicates which docs this request should "search after".static SearchSourceBuilder
searchSource()
A static factory method to construct a new search source.java.lang.Boolean
seqNoAndPrimaryTerm()
Indicates whetherSearchHit
s should be returned with the sequence number and primary term of the last modification of the document.SearchSourceBuilder
seqNoAndPrimaryTerm(java.lang.Boolean seqNoAndPrimaryTerm)
Should eachSearchHit
be returned with the sequence number and primary term of the last modification of the document.int
size()
Gets the number of search hits to return.SearchSourceBuilder
size(int size)
The number of search hits to return.SliceBuilder
slice()
Gets the slice used to filter the search hits, the top hits and the aggregations.SearchSourceBuilder
slice(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.SearchSourceBuilder
sort(java.lang.String name)
Add a sort against the given field name.SearchSourceBuilder
sort(java.lang.String name, SortOrder order)
Adds a sort against the given field name and the sort ordering.SearchSourceBuilder
sort(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.SearchSourceBuilder
stats(java.util.List<java.lang.String> statsGroups)
The stats groups this request will be aggregated under.SearchSourceBuilder
storedField(java.lang.String name)
Adds a stored field to load and return as part of the search request.StoredFieldsContext
storedFields()
Gets the stored fields context.SearchSourceBuilder
storedFields(java.util.List<java.lang.String> fields)
Sets the stored fields to load and return as part of the search request.SearchSourceBuilder
storedFields(StoredFieldsContext context)
Indicates how the stored fields should be fetched.SuggestBuilder
suggest()
Gets the suggester builder for this request.SearchSourceBuilder
suggest(SuggestBuilder suggestBuilder)
int
terminateAfter()
Gets the number of documents to terminate after collecting.SearchSourceBuilder
terminateAfter(int terminateAfter)
An optional terminate_after to terminate the search after collectingterminateAfter
documentsTimeValue
timeout()
Gets the timeout to control how long search is allowed to take.SearchSourceBuilder
timeout(TimeValue timeout)
An optional timeout to control how long search is allowed to take.java.lang.String
toString()
java.lang.String
toString(ToXContent.Params params)
XContentBuilder
toXContent(XContentBuilder builder, ToXContent.Params params)
boolean
trackScores()
Indicates whether scores will be tracked for this request.SearchSourceBuilder
trackScores(boolean trackScores)
Applies when sorting, and controls if scores will be tracked as well.boolean
trackTotalHits()
Indicates if the total hit count for the query should be tracked.SearchSourceBuilder
trackTotalHits(boolean trackTotalHits)
java.lang.Boolean
version()
Indicates whether the document's version will be included in the search hits.SearchSourceBuilder
version(java.lang.Boolean version)
Should eachSearchHit
be returned with a version associated with it.void
writeTo(StreamOutput out)
Write this into the StreamOutput.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.elasticsearch.common.xcontent.ToXContentObject
isFragment
-
-
-
-
Field Detail
-
FROM_FIELD
public static final ParseField FROM_FIELD
-
SIZE_FIELD
public static final ParseField SIZE_FIELD
-
TIMEOUT_FIELD
public static final ParseField TIMEOUT_FIELD
-
TERMINATE_AFTER_FIELD
public static final ParseField TERMINATE_AFTER_FIELD
-
QUERY_FIELD
public static final ParseField QUERY_FIELD
-
POST_FILTER_FIELD
public static final ParseField POST_FILTER_FIELD
-
MIN_SCORE_FIELD
public static final ParseField MIN_SCORE_FIELD
-
VERSION_FIELD
public static final ParseField VERSION_FIELD
-
SEQ_NO_PRIMARY_TERM_FIELD
public static final ParseField SEQ_NO_PRIMARY_TERM_FIELD
-
EXPLAIN_FIELD
public static final ParseField EXPLAIN_FIELD
-
_SOURCE_FIELD
public static final ParseField _SOURCE_FIELD
-
STORED_FIELDS_FIELD
public static final ParseField STORED_FIELDS_FIELD
-
DOCVALUE_FIELDS_FIELD
public static final ParseField DOCVALUE_FIELDS_FIELD
-
SCRIPT_FIELDS_FIELD
public static final ParseField SCRIPT_FIELDS_FIELD
-
SCRIPT_FIELD
public static final ParseField SCRIPT_FIELD
-
IGNORE_FAILURE_FIELD
public static final ParseField IGNORE_FAILURE_FIELD
-
SORT_FIELD
public static final ParseField SORT_FIELD
-
TRACK_SCORES_FIELD
public static final ParseField TRACK_SCORES_FIELD
-
TRACK_TOTAL_HITS_FIELD
public static final ParseField TRACK_TOTAL_HITS_FIELD
-
INDICES_BOOST_FIELD
public static final ParseField INDICES_BOOST_FIELD
-
AGGREGATIONS_FIELD
public static final ParseField AGGREGATIONS_FIELD
-
AGGS_FIELD
public static final ParseField AGGS_FIELD
-
HIGHLIGHT_FIELD
public static final ParseField HIGHLIGHT_FIELD
-
SUGGEST_FIELD
public static final ParseField SUGGEST_FIELD
-
RESCORE_FIELD
public static final ParseField RESCORE_FIELD
-
STATS_FIELD
public static final ParseField STATS_FIELD
-
EXT_FIELD
public static final ParseField EXT_FIELD
-
PROFILE_FIELD
public static final ParseField PROFILE_FIELD
-
SEARCH_AFTER
public static final ParseField SEARCH_AFTER
-
COLLAPSE
public static final ParseField COLLAPSE
-
SLICE
public static final ParseField SLICE
-
ALL_FIELDS_FIELDS
public static final ParseField ALL_FIELDS_FIELDS
-
-
Constructor Detail
-
SearchSourceBuilder
public SearchSourceBuilder()
Constructs a new search source builder.
-
SearchSourceBuilder
public SearchSourceBuilder(StreamInput in) throws java.io.IOException
Read from a stream.- Throws:
java.io.IOException
-
-
Method Detail
-
fromXContent
public static SearchSourceBuilder fromXContent(XContentParser parser) throws java.io.IOException
- Throws:
java.io.IOException
-
fromXContent
public static SearchSourceBuilder fromXContent(XContentParser parser, boolean checkTrailingTokens) throws java.io.IOException
- Throws:
java.io.IOException
-
searchSource
public static SearchSourceBuilder searchSource()
A static factory method to construct a new search source.
-
highlight
public static HighlightBuilder highlight()
A static factory method to construct new search highlights.
-
writeTo
public void writeTo(StreamOutput out) throws java.io.IOException
Description copied from interface:Writeable
Write this into the StreamOutput.
-
query
public SearchSourceBuilder query(QueryBuilder query)
Sets the search query for this request.- See Also:
QueryBuilders
-
query
public QueryBuilder query()
Gets the query for this request
-
postFilter
public SearchSourceBuilder postFilter(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.
-
postFilter
public QueryBuilder postFilter()
Gets the post filter for this request
-
from
public SearchSourceBuilder from(int from)
From index to start the search from. Defaults to0
.
-
from
public int from()
Gets the from index to start the search from.
-
size
public SearchSourceBuilder size(int size)
The number of search hits to return. Defaults to10
.
-
size
public int size()
Gets the number of search hits to return.
-
minScore
public SearchSourceBuilder minScore(float 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
public SearchSourceBuilder explain(java.lang.Boolean explain)
Should eachSearchHit
be 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
public SearchSourceBuilder version(java.lang.Boolean version)
Should eachSearchHit
be 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
public SearchSourceBuilder seqNoAndPrimaryTerm(java.lang.Boolean seqNoAndPrimaryTerm)
Should eachSearchHit
be returned with the sequence number and primary term of the last modification of the document.
-
seqNoAndPrimaryTerm
public java.lang.Boolean seqNoAndPrimaryTerm()
Indicates whetherSearchHit
s should be returned with the sequence number and primary term of the last modification of the document.
-
timeout
public SearchSourceBuilder timeout(TimeValue timeout)
An optional timeout to control how long search is allowed to take.
-
timeout
public TimeValue timeout()
Gets the timeout to control how long search is allowed to take.
-
terminateAfter
public SearchSourceBuilder terminateAfter(int terminateAfter)
An optional terminate_after to terminate the search after collectingterminateAfter
documents
-
terminateAfter
public int terminateAfter()
Gets the number of documents to terminate after collecting.
-
sort
public SearchSourceBuilder sort(java.lang.String name, SortOrder order)
Adds a sort against the given field name and the sort ordering.- Parameters:
name
- The name of the fieldorder
- The sort ordering
-
sort
public SearchSourceBuilder sort(java.lang.String name)
Add a sort against the given field name.- Parameters:
name
- The name of the field to sort by
-
sort
public SearchSourceBuilder sort(SortBuilder<?> sort)
Adds a sort builder.
-
sorts
public java.util.List<SortBuilder<?>> sorts()
Gets the bytes representing the sort builders for this request.
-
trackScores
public SearchSourceBuilder trackScores(boolean 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
public boolean trackTotalHits()
Indicates if the total hit count for the query should be tracked.
-
trackTotalHits
public SearchSourceBuilder trackTotalHits(boolean trackTotalHits)
-
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
public SearchSourceBuilder searchAfter(java.lang.Object[] values)
Set the sort values that indicates which docs this request should "search after".
-
slice
public SearchSourceBuilder slice(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.
-
slice
public SliceBuilder slice()
Gets the slice used to filter the search hits, the top hits and the aggregations.
-
collapse
public CollapseBuilder collapse()
-
collapse
public SearchSourceBuilder collapse(CollapseBuilder collapse)
-
aggregation
public SearchSourceBuilder aggregation(AggregationBuilder aggregation)
Add an aggregation to perform as part of the search.
-
aggregation
public SearchSourceBuilder aggregation(PipelineAggregationBuilder aggregation)
Add an aggregation to perform as part of the search.
-
aggregations
public AggregatorFactories.Builder aggregations()
Gets the bytes representing the aggregation builders for this request.
-
highlighter
public SearchSourceBuilder highlighter(HighlightBuilder highlightBuilder)
Adds highlight to perform as part of the search.
-
highlighter
public HighlightBuilder highlighter()
Gets the highlighter builder for this request.
-
suggest
public SearchSourceBuilder suggest(SuggestBuilder suggestBuilder)
-
suggest
public SuggestBuilder suggest()
Gets the suggester builder for this request.
-
addRescorer
public SearchSourceBuilder addRescorer(RescorerBuilder<?> rescoreBuilder)
-
clearRescorers
public SearchSourceBuilder clearRescorers()
-
profile
public SearchSourceBuilder profile(boolean profile)
Should the query be profiled. Defaults tofalse
-
profile
public boolean profile()
Return whether to profile query execution, ornull
if unspecified.
-
rescores
public java.util.List<RescorerBuilder> rescores()
Gets the bytes representing the rescore builders for this request.
-
fetchSource
public SearchSourceBuilder fetchSource(boolean fetch)
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
public SearchSourceBuilder fetchSource(@Nullable FetchSourceContext fetchSourceContext)
Indicate how the _source should be fetched.
-
fetchSource
public FetchSourceContext fetchSource()
Gets theFetchSourceContext
which defines how the _source should be fetched.
-
storedField
public SearchSourceBuilder storedField(java.lang.String name)
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
public SearchSourceBuilder storedFields(java.util.List<java.lang.String> fields)
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
public SearchSourceBuilder storedFields(StoredFieldsContext context)
Indicates how the stored fields should be fetched.
-
storedFields
public StoredFieldsContext storedFields()
Gets the stored fields context.
-
docValueFields
public java.util.List<DocValueFieldsContext.FieldAndFormat> docValueFields()
Gets the docvalue fields.
-
docValueField
public SearchSourceBuilder docValueField(java.lang.String name, @Nullable java.lang.String format)
Adds a field to load from the doc values and return as part of the search request.
-
docValueField
public SearchSourceBuilder docValueField(java.lang.String name)
Adds a field to load from the doc values and return as part of the search request.
-
scriptField
public SearchSourceBuilder scriptField(java.lang.String name, Script script)
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
public java.util.List<SearchSourceBuilder.ScriptField> scriptFields()
Gets the script fields.
-
indexBoost
public SearchSourceBuilder indexBoost(java.lang.String index, float 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
public java.util.List<SearchSourceBuilder.IndexBoost> indexBoosts()
Gets the boost a specific indices or aliases will receive when the query is executed against them.
-
stats
public SearchSourceBuilder stats(java.util.List<java.lang.String> statsGroups)
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
public SearchSourceBuilder ext(java.util.List<SearchExtBuilder> searchExtBuilders)
-
ext
public java.util.List<SearchExtBuilder> ext()
-
isSuggestOnly
public boolean isSuggestOnly()
- Returns:
- true if the source only has suggest
-
rewrite
public SearchSourceBuilder rewrite(QueryRewriteContext context) throws java.io.IOException
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:
rewrite
in interfaceRewriteable<SearchSourceBuilder>
- Throws:
java.io.IOException
-
copyWithNewSlice
public SearchSourceBuilder copyWithNewSlice(SliceBuilder slice)
Create a shallow copy of this builder with a new slice configuration.
-
parseXContent
public void parseXContent(XContentParser parser) throws java.io.IOException
- Throws:
java.io.IOException
-
parseXContent
public void parseXContent(XContentParser parser, boolean checkTrailingTokens) throws java.io.IOException
Parse 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:
toXContent
in interfaceToXContent
- Throws:
java.io.IOException
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(ToXContent.Params params)
-
-