Class SearchContext
- java.lang.Object
-
- org.elasticsearch.common.util.concurrent.AbstractRefCounted
-
- org.elasticsearch.search.internal.SearchContext
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,Releasable,RefCounted
- Direct Known Subclasses:
FilteredSearchContext
public abstract class SearchContext extends AbstractRefCounted implements Releasable
This class encapsulates the state needed to execute a search. It holds a reference to the shards point in time snapshot (IndexReader / ContextIndexSearcher) and allows passing on state from one query / fetch phase to another. This class also implementsRefCountedsince in some situations like inSearchServicea SearchContext can be closed concurrently due to independent events ie. when an index gets removed. To prevent accessing closed IndexReader / IndexSearcher instances the SearchContext can be guarded by a reference count and fail if it's been closed by an external event.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSearchContext.LifetimeThe life time of an object that is used during search execution.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_TERMINATE_AFTERstatic intDEFAULT_TRACK_TOTAL_HITS_UP_TOstatic intTRACK_TOTAL_HITS_ACCURATEstatic intTRACK_TOTAL_HITS_DISABLED
-
Constructor Summary
Constructors Modifier Constructor Description protectedSearchContext()
-
Method Summary
Modifier and Type Method Description abstract voidaccessed(long accessTime)voidaddReleasable(Releasable releasable, SearchContext.Lifetime lifetime)Schedule the release of a resource.abstract voidaddRescore(RescoreContext rescore)abstract voidaddSearchExt(SearchExtBuilder searchExtBuilder)abstract SearchContextAggregationsaggregations()abstract SearchContextaggregations(SearchContextAggregations aggregations)abstract org.apache.lucene.search.QueryaliasFilter()protected voidalreadyClosed()abstract BigArraysbigArrays()abstract BitsetFilterCachebitsetFilterCache()abstract org.apache.lucene.search.QuerybuildFilteredQuery(org.apache.lucene.search.Query query)Automatically apply all required filters to the given query such as alias filters, types filters, etc.voidclearReleasables(SearchContext.Lifetime lifetime)voidclose()protected voidcloseInternal()abstract CollapseContextcollapse()abstract SearchContextcollapse(CollapseContext collapse)abstract DfsSearchResultdfsResult()abstract int[]docIdsToLoad()abstract SearchContextdocIdsToLoad(int[] docIdsToLoad, int docsIdsToLoadFrom, int docsIdsToLoadSize)abstract intdocIdsToLoadFrom()abstract intdocIdsToLoadSize()protected abstract voiddoClose()abstract DocValueFieldsContextdocValueFieldsContext()abstract SearchContextdocValueFieldsContext(DocValueFieldsContext docValueFieldsContext)abstract booleanexplain()abstract voidexplain(boolean explain)abstract FetchPhasefetchPhase()abstract FetchSearchResultfetchResult()abstract FetchSourceContextfetchSourceContext()abstract SearchContextfetchSourceContext(FetchSourceContext fetchSourceContext)abstract intfrom()abstract SearchContextfrom(int from)abstract <IFD extends IndexFieldData<?>>
IFDgetForField(MappedFieldType fieldType)abstract ObjectMappergetObjectMapper(java.lang.String name)abstract longgetOriginNanoTime()abstract ProfilersgetProfilers()Return a handle over the profilers for the current search request, ornullif profiling is not enabled.abstract QueryShardContextgetQueryShardContext()abstract longgetRelativeTimeInMillis()Returns time in milliseconds that can be used for relative time calculations.abstract SearchExtBuildergetSearchExt(java.lang.String name)abstract SearchTaskgetTask()abstract java.util.List<java.lang.String>groupStats()abstract voidgroupStats(java.util.List<java.lang.String> groupStats)abstract booleanhasFetchSourceContext()booleanhasOnlySuggest()abstract booleanhasScriptFields()abstract booleanhasStoredFields()abstract booleanhasStoredFieldsContext()abstract SearchContextHighlighthighlight()abstract voidhighlight(SearchContextHighlight highlight)abstract longid()abstract IndexShardindexShard()InnerHitsContextinnerHits()abstract booleanisCancelled()abstract longkeepAlive()abstract voidkeepAlive(long keepAlive)abstract longlastAccessTime()SearchLookuplookup()abstract booleanlowLevelCancellation()Indicates if the current index should perform frequent low level search cancellation check.abstract MapperServicemapperService()abstract java.lang.FloatminimumScore()abstract SearchContextminimumScore(float minimumScore)abstract intnumberOfShards()abstract ParsedQueryparsedPostFilter()abstract SearchContextparsedPostFilter(ParsedQuery postFilter)abstract ParsedQueryparsedQuery()abstract SearchContextparsedQuery(ParsedQuery query)abstract voidpreProcess(boolean rewrite)Should be called before executing the main query and after all other parameters have been set.abstract org.apache.lucene.search.Queryquery()The query to execute, might be rewritten.abstract floatqueryBoost()abstract java.util.Map<java.lang.Class<?>,org.apache.lucene.search.Collector>queryCollectors()Return a view of the additional query collectors that should be run for this context.abstract QuerySearchResultqueryResult()abstract ShardSearchRequestrequest()abstract java.util.List<RescoreContext>rescore()abstract ScriptFieldsContextscriptFields()abstract ScrollContextscrollContext()abstract SearchContextscrollContext(ScrollContext scroll)abstract org.apache.lucene.search.FieldDocsearchAfter()abstract SearchContextsearchAfter(org.apache.lucene.search.FieldDoc searchAfter)abstract ContextIndexSearchersearcher()abstract SearchTypesearchType()abstract booleanseqNoAndPrimaryTerm()indicates whether the sequence number and primary term of the last modification to each hit should be returnedabstract voidseqNoAndPrimaryTerm(boolean seqNoAndPrimaryTerm)controls whether the sequence number and primary term of the last modification to each hit should be returnedabstract voidsetTask(SearchTask task)abstract SearchShardTargetshardTarget()abstract SimilarityServicesimilarityService()abstract intsize()abstract SearchContextsize(int size)abstract MappedFieldTypesmartNameFieldType(java.lang.String name)Looks up the given field, but does not restrict to fields in the types set on this context.abstract SortAndFormatssort()abstract SearchContextsort(SortAndFormats sort)abstract java.lang.Stringsource()abstract booleansourceRequested()A shortcut function to see whether there is a fetchSourceContext and it says the source is requested.abstract StoredFieldsContextstoredFieldsContext()abstract SearchContextstoredFieldsContext(StoredFieldsContext storedFieldsContext)abstract booleanstoredFieldsRequested()A shortcut function to see whether there is a storedFieldsContext and it says the fields are requested.abstract SuggestionSearchContextsuggest()abstract voidsuggest(SuggestionSearchContext suggest)abstract intterminateAfter()abstract voidterminateAfter(int terminateAfter)abstract TimeValuetimeout()abstract voidtimeout(TimeValue timeout)java.lang.StringtoString()abstract booleantrackScores()abstract SearchContexttrackScores(boolean trackScores)abstract inttrackTotalHitsUpTo()Indicates the total number of hits to count accurately.abstract SearchContexttrackTotalHitsUpTo(int trackTotalHits)abstract booleanversion()abstract voidversion(boolean version)
-
-
-
Field Detail
-
DEFAULT_TERMINATE_AFTER
public static final int DEFAULT_TERMINATE_AFTER
- See Also:
- Constant Field Values
-
TRACK_TOTAL_HITS_ACCURATE
public static final int TRACK_TOTAL_HITS_ACCURATE
- See Also:
- Constant Field Values
-
TRACK_TOTAL_HITS_DISABLED
public static final int TRACK_TOTAL_HITS_DISABLED
- See Also:
- Constant Field Values
-
DEFAULT_TRACK_TOTAL_HITS_UP_TO
public static final int DEFAULT_TRACK_TOTAL_HITS_UP_TO
- See Also:
- Constant Field Values
-
-
Method Detail
-
setTask
public abstract void setTask(SearchTask task)
-
getTask
public abstract SearchTask getTask()
-
isCancelled
public abstract boolean isCancelled()
-
close
public final void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceReleasable
-
closeInternal
protected final void closeInternal()
- Specified by:
closeInternalin classAbstractRefCounted
-
alreadyClosed
protected void alreadyClosed()
- Overrides:
alreadyClosedin classAbstractRefCounted
-
doClose
protected abstract void doClose()
-
preProcess
public abstract void preProcess(boolean rewrite)
Should be called before executing the main query and after all other parameters have been set.- Parameters:
rewrite- if the set query should be rewritten against the searcher returned fromsearcher()
-
buildFilteredQuery
public abstract org.apache.lucene.search.Query buildFilteredQuery(org.apache.lucene.search.Query query)
Automatically apply all required filters to the given query such as alias filters, types filters, etc.
-
id
public abstract long id()
-
source
public abstract java.lang.String source()
-
request
public abstract ShardSearchRequest request()
-
searchType
public abstract SearchType searchType()
-
shardTarget
public abstract SearchShardTarget shardTarget()
-
numberOfShards
public abstract int numberOfShards()
-
queryBoost
public abstract float queryBoost()
-
getOriginNanoTime
public abstract long getOriginNanoTime()
-
scrollContext
public abstract ScrollContext scrollContext()
-
scrollContext
public abstract SearchContext scrollContext(ScrollContext scroll)
-
aggregations
public abstract SearchContextAggregations aggregations()
-
aggregations
public abstract SearchContext aggregations(SearchContextAggregations aggregations)
-
addSearchExt
public abstract void addSearchExt(SearchExtBuilder searchExtBuilder)
-
getSearchExt
public abstract SearchExtBuilder getSearchExt(java.lang.String name)
-
highlight
public abstract SearchContextHighlight highlight()
-
highlight
public abstract void highlight(SearchContextHighlight highlight)
-
innerHits
public InnerHitsContext innerHits()
-
suggest
public abstract SuggestionSearchContext suggest()
-
suggest
public abstract void suggest(SuggestionSearchContext suggest)
-
rescore
public abstract java.util.List<RescoreContext> rescore()
- Returns:
- list of all rescore contexts. empty if there aren't any.
-
addRescore
public abstract void addRescore(RescoreContext rescore)
-
hasScriptFields
public abstract boolean hasScriptFields()
-
scriptFields
public abstract ScriptFieldsContext scriptFields()
-
sourceRequested
public abstract boolean sourceRequested()
A shortcut function to see whether there is a fetchSourceContext and it says the source is requested.
-
hasFetchSourceContext
public abstract boolean hasFetchSourceContext()
-
fetchSourceContext
public abstract FetchSourceContext fetchSourceContext()
-
fetchSourceContext
public abstract SearchContext fetchSourceContext(FetchSourceContext fetchSourceContext)
-
docValueFieldsContext
public abstract DocValueFieldsContext docValueFieldsContext()
-
docValueFieldsContext
public abstract SearchContext docValueFieldsContext(DocValueFieldsContext docValueFieldsContext)
-
searcher
public abstract ContextIndexSearcher searcher()
-
indexShard
public abstract IndexShard indexShard()
-
mapperService
public abstract MapperService mapperService()
-
similarityService
public abstract SimilarityService similarityService()
-
bigArrays
public abstract BigArrays bigArrays()
-
bitsetFilterCache
public abstract BitsetFilterCache bitsetFilterCache()
-
getForField
public abstract <IFD extends IndexFieldData<?>> IFD getForField(MappedFieldType fieldType)
-
timeout
public abstract TimeValue timeout()
-
timeout
public abstract void timeout(TimeValue timeout)
-
terminateAfter
public abstract int terminateAfter()
-
terminateAfter
public abstract void terminateAfter(int terminateAfter)
-
lowLevelCancellation
public abstract boolean lowLevelCancellation()
Indicates if the current index should perform frequent low level search cancellation check. Enabling low-level checks will make long running searches to react to the cancellation request faster. However, since it will produce more cancellation checks it might slow the search performance down.
-
minimumScore
public abstract SearchContext minimumScore(float minimumScore)
-
minimumScore
public abstract java.lang.Float minimumScore()
-
sort
public abstract SearchContext sort(SortAndFormats sort)
-
sort
public abstract SortAndFormats sort()
-
trackScores
public abstract SearchContext trackScores(boolean trackScores)
-
trackScores
public abstract boolean trackScores()
-
trackTotalHitsUpTo
public abstract SearchContext trackTotalHitsUpTo(int trackTotalHits)
-
trackTotalHitsUpTo
public abstract int trackTotalHitsUpTo()
Indicates the total number of hits to count accurately. Defaults toDEFAULT_TRACK_TOTAL_HITS_UP_TO.
-
searchAfter
public abstract SearchContext searchAfter(org.apache.lucene.search.FieldDoc searchAfter)
-
searchAfter
public abstract org.apache.lucene.search.FieldDoc searchAfter()
-
collapse
public abstract SearchContext collapse(CollapseContext collapse)
-
collapse
public abstract CollapseContext collapse()
-
parsedPostFilter
public abstract SearchContext parsedPostFilter(ParsedQuery postFilter)
-
parsedPostFilter
public abstract ParsedQuery parsedPostFilter()
-
aliasFilter
public abstract org.apache.lucene.search.Query aliasFilter()
-
parsedQuery
public abstract SearchContext parsedQuery(ParsedQuery query)
-
parsedQuery
public abstract ParsedQuery parsedQuery()
-
query
public abstract org.apache.lucene.search.Query query()
The query to execute, might be rewritten.
-
from
public abstract int from()
-
from
public abstract SearchContext from(int from)
-
size
public abstract int size()
-
size
public abstract SearchContext size(int size)
-
hasStoredFields
public abstract boolean hasStoredFields()
-
hasStoredFieldsContext
public abstract boolean hasStoredFieldsContext()
-
storedFieldsRequested
public abstract boolean storedFieldsRequested()
A shortcut function to see whether there is a storedFieldsContext and it says the fields are requested.
-
storedFieldsContext
public abstract StoredFieldsContext storedFieldsContext()
-
storedFieldsContext
public abstract SearchContext storedFieldsContext(StoredFieldsContext storedFieldsContext)
-
explain
public abstract boolean explain()
-
explain
public abstract void explain(boolean explain)
-
groupStats
@Nullable public abstract java.util.List<java.lang.String> groupStats()
-
groupStats
public abstract void groupStats(java.util.List<java.lang.String> groupStats)
-
version
public abstract boolean version()
-
version
public abstract void version(boolean version)
-
seqNoAndPrimaryTerm
public abstract boolean seqNoAndPrimaryTerm()
indicates whether the sequence number and primary term of the last modification to each hit should be returned
-
seqNoAndPrimaryTerm
public abstract void seqNoAndPrimaryTerm(boolean seqNoAndPrimaryTerm)
controls whether the sequence number and primary term of the last modification to each hit should be returned
-
docIdsToLoad
public abstract int[] docIdsToLoad()
-
docIdsToLoadFrom
public abstract int docIdsToLoadFrom()
-
docIdsToLoadSize
public abstract int docIdsToLoadSize()
-
docIdsToLoad
public abstract SearchContext docIdsToLoad(int[] docIdsToLoad, int docsIdsToLoadFrom, int docsIdsToLoadSize)
-
accessed
public abstract void accessed(long accessTime)
-
lastAccessTime
public abstract long lastAccessTime()
-
keepAlive
public abstract long keepAlive()
-
keepAlive
public abstract void keepAlive(long keepAlive)
-
lookup
public SearchLookup lookup()
-
dfsResult
public abstract DfsSearchResult dfsResult()
-
queryResult
public abstract QuerySearchResult queryResult()
-
fetchPhase
public abstract FetchPhase fetchPhase()
-
fetchResult
public abstract FetchSearchResult fetchResult()
-
getProfilers
public abstract Profilers getProfilers()
Return a handle over the profilers for the current search request, ornullif profiling is not enabled.
-
addReleasable
public void addReleasable(Releasable releasable, SearchContext.Lifetime lifetime)
Schedule the release of a resource. The time whenReleasable.close()will be called on this object is function of the providedSearchContext.Lifetime.
-
clearReleasables
public void clearReleasables(SearchContext.Lifetime lifetime)
-
hasOnlySuggest
public final boolean hasOnlySuggest()
- Returns:
- true if the request contains only suggest
-
smartNameFieldType
public abstract MappedFieldType smartNameFieldType(java.lang.String name)
Looks up the given field, but does not restrict to fields in the types set on this context.
-
getObjectMapper
public abstract ObjectMapper getObjectMapper(java.lang.String name)
-
getRelativeTimeInMillis
public abstract long getRelativeTimeInMillis()
Returns time in milliseconds that can be used for relative time calculations. WARN: This is not the epoch time.
-
queryCollectors
public abstract java.util.Map<java.lang.Class<?>,org.apache.lucene.search.Collector> queryCollectors()
Return a view of the additional query collectors that should be run for this context.
-
getQueryShardContext
public abstract QueryShardContext getQueryShardContext()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-