Class SearchContext

java.lang.Object
org.opensearch.search.internal.SearchContext
All Implemented Interfaces:
Closeable, AutoCloseable, org.opensearch.common.lease.Releasable
Direct Known Subclasses:
FilteredSearchContext

@PublicApi(since="1.0.0") public abstract class SearchContext extends Object implements org.opensearch.common.lease.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.
Opensearch.api:
  • Field Details

    • DEFAULT_TERMINATE_AFTER

      public static final int DEFAULT_TERMINATE_AFTER
      See Also:
    • TRACK_TOTAL_HITS_ACCURATE

      public static final int TRACK_TOTAL_HITS_ACCURATE
      See Also:
    • TRACK_TOTAL_HITS_DISABLED

      public static final int TRACK_TOTAL_HITS_DISABLED
      See Also:
    • DEFAULT_TRACK_TOTAL_HITS_UP_TO

      public static final int DEFAULT_TRACK_TOTAL_HITS_UP_TO
      See Also:
    • NO_OP_BUCKET_COLLECTOR_PROCESSOR

      public static final BucketCollectorProcessor NO_OP_BUCKET_COLLECTOR_PROCESSOR
  • Constructor Details

    • SearchContext

      protected SearchContext()
  • Method Details

    • setTask

      public abstract void setTask(SearchShardTask task)
    • getTask

      public abstract SearchShardTask getTask()
    • isCancelled

      public abstract boolean isCancelled()
    • isSearchTimedOut

      public boolean isSearchTimedOut()
    • setSearchTimedOut

      public void setSearchTimedOut(boolean searchTimedOut)
    • close

      public final void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface org.opensearch.common.lease.Releasable
    • 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 from searcher()
    • 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 ShardSearchContextId id()
    • source

      public abstract 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()
    • scrollContext

      public abstract ScrollContext scrollContext()
    • aggregations

      public abstract SearchContextAggregations aggregations()
    • aggregations

      public abstract SearchContext aggregations(SearchContextAggregations aggregations)
    • addSearchExt

      public abstract void addSearchExt(SearchExtBuilder searchExtBuilder)
    • getSearchExt

      public abstract SearchExtBuilder getSearchExt(String name)
    • highlight

      public abstract SearchHighlightContext highlight()
    • highlight

      public abstract void highlight(SearchHighlightContext highlight)
    • hasInnerHits

      public boolean hasInnerHits()
    • innerHits

      public InnerHitsContext innerHits()
    • suggest

      public abstract SuggestionSearchContext suggest()
    • suggest

      public abstract void suggest(SuggestionSearchContext suggest)
    • rescore

      public abstract List<RescoreContext> rescore()
      Returns:
      list of all rescore contexts. empty if there aren't any.
    • addRescore

      public abstract void addRescore(RescoreContext rescore)
    • rescoreDocIds

      public final RescoreDocIds rescoreDocIds()
    • assignRescoreDocIds

      public final void assignRescoreDocIds(RescoreDocIds rescoreDocIds)
    • 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)
    • docValuesContext

      public abstract FetchDocValuesContext docValuesContext()
    • docValuesContext

      public abstract SearchContext docValuesContext(FetchDocValuesContext docValuesContext)
    • fetchFieldsContext

      public abstract FetchFieldsContext fetchFieldsContext()
      The context related to retrieving fields.
    • fetchFieldsContext

      public abstract SearchContext fetchFieldsContext(FetchFieldsContext fetchFieldsContext)
      Sets the context related to retrieving fields.
    • 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()
    • timeout

      public abstract org.opensearch.common.unit.TimeValue timeout()
    • timeout

      public abstract void timeout(org.opensearch.common.unit.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 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()
    • includeNamedQueriesScore

      public SearchContext includeNamedQueriesScore(boolean includeNamedQueriesScore)
      Determines whether named queries' scores should be included in the search results. By default, this is set to return false, indicating that scores from named queries are not included.
      Parameters:
      includeNamedQueriesScore - true to include scores from named queries, false otherwise.
    • includeNamedQueriesScore

      public boolean includeNamedQueriesScore()
      Checks if scores from named queries are included in the search results.
      Returns:
      true if scores from named queries are included, false otherwise.
    • trackTotalHitsUpTo

      public abstract SearchContext trackTotalHitsUpTo(int trackTotalHits)
    • trackTotalHitsUpTo

      public abstract int trackTotalHitsUpTo()
      Indicates the total number of hits to count accurately. Defaults to DEFAULT_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 List<String> groupStats()
    • groupStats

      public abstract void groupStats(List<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)
    • 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, or null if profiling is not enabled.
    • shouldUseConcurrentSearch

      public boolean shouldUseConcurrentSearch()
      Returns concurrent segment search status for the search context
    • asLocalBucketCountThresholds

      public LocalBucketCountThresholds asLocalBucketCountThresholds(TermsAggregator.BucketCountThresholds bucketCountThresholds)
      Returns local bucket count thresholds based on concurrent segment search status
    • addReleasable

      public void addReleasable(org.opensearch.common.lease.Releasable releasable)
      Adds a releasable that will be freed when this context is closed.
    • hasOnlySuggest

      public final boolean hasOnlySuggest()
      Returns:
      true if the request contains only suggest
    • fieldType

      public abstract MappedFieldType fieldType(String name)
      Given the full name of a field, returns its MappedFieldType.
    • getObjectMapper

      public abstract ObjectMapper getObjectMapper(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 and can be a cached time.
    • getRelativeTimeInMillis

      public long getRelativeTimeInMillis(boolean useCache)
      Returns time in milliseconds that can be used for relative time calculations. this method will fall back to getRelativeTimeInMillis() (which might be a cached time) if useCache was set to true else it will be just be a wrapper of System.nanoTime() converted to milliseconds.
      Parameters:
      useCache - to allow using cached time if true or forcing calling System.nanoTime() if false
      Returns:
      Returns time in milliseconds that can be used for relative time calculations.
    • queryCollectorManagers

      public abstract Map<Class<?>,org.apache.lucene.search.CollectorManager<? extends org.apache.lucene.search.Collector,ReduceableSearchResult>> queryCollectorManagers()
      Return a view of the additional query collector managers that should be run for this context.
    • getQueryShardContext

      public abstract QueryShardContext getQueryShardContext()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • readerContext

      public abstract ReaderContext readerContext()
    • partialOnShard

      public abstract InternalAggregation.ReduceContext partialOnShard()
    • setBucketCollectorProcessor

      public abstract void setBucketCollectorProcessor(BucketCollectorProcessor bucketCollectorProcessor)
    • bucketCollectorProcessor

      public abstract BucketCollectorProcessor bucketCollectorProcessor()
    • shouldUseTimeSeriesDescSortOptimization

      public abstract boolean shouldUseTimeSeriesDescSortOptimization()
    • getTargetMaxSliceCount

      public abstract int getTargetMaxSliceCount()
    • maxAggRewriteFilters

      public int maxAggRewriteFilters()