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 implementsRefCounted
since in some situations like inSearchService
a 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 class
SearchContext.Lifetime
The life time of an object that is used during search execution.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_TERMINATE_AFTER
-
Constructor Summary
Constructors Modifier Constructor Description protected
SearchContext()
-
Method Summary
Modifier and Type Method Description abstract void
accessed(long accessTime)
void
addReleasable(Releasable releasable, SearchContext.Lifetime lifetime)
Schedule the release of a resource.abstract void
addRescore(RescoreContext rescore)
abstract void
addSearchExt(SearchExtBuilder searchExtBuilder)
abstract SearchContextAggregations
aggregations()
abstract SearchContext
aggregations(SearchContextAggregations aggregations)
abstract org.apache.lucene.search.Query
aliasFilter()
protected void
alreadyClosed()
abstract BigArrays
bigArrays()
abstract BitsetFilterCache
bitsetFilterCache()
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.void
clearReleasables(SearchContext.Lifetime lifetime)
void
close()
protected void
closeInternal()
abstract CollapseContext
collapse()
abstract SearchContext
collapse(CollapseContext collapse)
abstract DfsSearchResult
dfsResult()
abstract int[]
docIdsToLoad()
abstract SearchContext
docIdsToLoad(int[] docIdsToLoad, int docsIdsToLoadFrom, int docsIdsToLoadSize)
abstract int
docIdsToLoadFrom()
abstract int
docIdsToLoadSize()
protected abstract void
doClose()
abstract DocValueFieldsContext
docValueFieldsContext()
abstract SearchContext
docValueFieldsContext(DocValueFieldsContext docValueFieldsContext)
abstract boolean
explain()
abstract void
explain(boolean explain)
abstract FetchPhase
fetchPhase()
abstract FetchSearchResult
fetchResult()
abstract FetchSourceContext
fetchSourceContext()
abstract SearchContext
fetchSourceContext(FetchSourceContext fetchSourceContext)
abstract int
from()
abstract SearchContext
from(int from)
abstract <IFD extends IndexFieldData<?>>
IFDgetForField(MappedFieldType fieldType)
abstract ObjectMapper
getObjectMapper(java.lang.String name)
abstract long
getOriginNanoTime()
abstract Profilers
getProfilers()
Return a handle over the profilers for the current search request, ornull
if profiling is not enabled.abstract QueryShardContext
getQueryShardContext()
abstract SearchExtBuilder
getSearchExt(java.lang.String name)
abstract SearchTask
getTask()
abstract java.util.List<java.lang.String>
groupStats()
abstract void
groupStats(java.util.List<java.lang.String> groupStats)
abstract boolean
hasFetchSourceContext()
boolean
hasOnlySuggest()
abstract boolean
hasScriptFields()
abstract boolean
hasStoredFields()
abstract boolean
hasStoredFieldsContext()
abstract SearchContextHighlight
highlight()
abstract void
highlight(SearchContextHighlight highlight)
abstract long
id()
abstract IndexShard
indexShard()
InnerHitsContext
innerHits()
abstract boolean
isCancelled()
abstract long
keepAlive()
abstract void
keepAlive(long keepAlive)
abstract long
lastAccessTime()
SearchLookup
lookup()
abstract boolean
lowLevelCancellation()
Indicates if the current index should perform frequent low level search cancellation check.abstract MapperService
mapperService()
abstract java.lang.Float
minimumScore()
abstract SearchContext
minimumScore(float minimumScore)
abstract int
numberOfShards()
abstract ParsedQuery
parsedPostFilter()
abstract SearchContext
parsedPostFilter(ParsedQuery postFilter)
abstract ParsedQuery
parsedQuery()
abstract SearchContext
parsedQuery(ParsedQuery query)
abstract void
preProcess(boolean rewrite)
Should be called before executing the main query and after all other parameters have been set.abstract org.apache.lucene.search.Query
query()
The query to execute, might be rewritten.abstract float
queryBoost()
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 QuerySearchResult
queryResult()
abstract ShardSearchRequest
request()
abstract java.util.List<RescoreContext>
rescore()
abstract ScriptFieldsContext
scriptFields()
abstract ScrollContext
scrollContext()
abstract SearchContext
scrollContext(ScrollContext scroll)
abstract org.apache.lucene.search.FieldDoc
searchAfter()
abstract SearchContext
searchAfter(org.apache.lucene.search.FieldDoc searchAfter)
abstract ContextIndexSearcher
searcher()
abstract SearchType
searchType()
abstract boolean
seqNoAndPrimaryTerm()
indicates whether the sequence number and primary term of the last modification to each hit should be returnedabstract void
seqNoAndPrimaryTerm(boolean seqNoAndPrimaryTerm)
controls whether the sequence number and primary term of the last modification to each hit should be returnedabstract void
setTask(SearchTask task)
abstract SearchShardTarget
shardTarget()
abstract SimilarityService
similarityService()
abstract int
size()
abstract SearchContext
size(int size)
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.abstract SortAndFormats
sort()
abstract SearchContext
sort(SortAndFormats sort)
abstract java.lang.String
source()
abstract boolean
sourceRequested()
A shortcut function to see whether there is a fetchSourceContext and it says the source is requested.abstract StoredFieldsContext
storedFieldsContext()
abstract SearchContext
storedFieldsContext(StoredFieldsContext storedFieldsContext)
abstract boolean
storedFieldsRequested()
A shortcut function to see whether there is a storedFieldsContext and it says the fields are requested.abstract SuggestionSearchContext
suggest()
abstract void
suggest(SuggestionSearchContext suggest)
abstract int
terminateAfter()
abstract void
terminateAfter(int terminateAfter)
abstract org.apache.lucene.util.Counter
timeEstimateCounter()
abstract TimeValue
timeout()
abstract void
timeout(TimeValue timeout)
java.lang.String
toString()
abstract boolean
trackScores()
abstract SearchContext
trackScores(boolean trackScores)
abstract boolean
trackTotalHits()
Indicates if the total hit count for the query should be tracked.abstract SearchContext
trackTotalHits(boolean trackTotalHits)
abstract boolean
version()
abstract void
version(boolean version)
-
-
-
Field Detail
-
DEFAULT_TERMINATE_AFTER
public static final int DEFAULT_TERMINATE_AFTER
- 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceReleasable
-
closeInternal
protected final void closeInternal()
- Specified by:
closeInternal
in classAbstractRefCounted
-
alreadyClosed
protected void alreadyClosed()
- Overrides:
alreadyClosed
in 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()
-
trackTotalHits
public abstract SearchContext trackTotalHits(boolean trackTotalHits)
-
trackTotalHits
public abstract boolean trackTotalHits()
Indicates if the total hit count for the query should be tracked. Defaults totrue
-
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, ornull
if 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)
-
timeEstimateCounter
public abstract org.apache.lucene.util.Counter timeEstimateCounter()
-
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:
toString
in classjava.lang.Object
-
-