Class SearchExecutionContext


public class SearchExecutionContext extends QueryRewriteContext
The context used to execute a search request on a shard. It provides access to required information like mapping definitions and document data. This context is used in several components of search execution, including building queries and fetching hits.
  • Constructor Details

  • Method Details

    • setTypes

      public void setTypes(String... types)
    • getTypes

      public String[] getTypes()
    • getSearchSimilarity

      public getSearchSimilarity()
      The similarity to use in searches, which takes into account per-field configuration.
    • getDefaultSimilarity

      public getDefaultSimilarity()
      The default similarity configured in the index settings.
    • defaultFields

      public List<String> defaultFields()
    • queryStringLenient

      public boolean queryStringLenient()
    • queryStringAnalyzeWildcard

      public boolean queryStringAnalyzeWildcard()
    • queryStringAllowLeadingWildcard

      public boolean queryStringAllowLeadingWildcard()
    • bitsetFilter

      public bitsetFilter( filter)
    • allowExpensiveQueries

      public boolean allowExpensiveQueries()
    • getForField

      public <IFD extends IndexFieldData<?>> IFD getForField(MappedFieldType fieldType)
    • addNamedQuery

      public void addNamedQuery(String name, query)
    • copyNamedQueries

      public Map<String,> copyNamedQueries()
    • parseDocument

      public ParsedDocument parseDocument(SourceToParse source) throws MapperParsingException
      Parse a document with current mapping.
    • hasNested

      public boolean hasNested()
    • hasMappings

      public boolean hasMappings()
    • nestedMappings

      public List<NestedObjectMapper> nestedMappings()
    • getMatchingFieldNames

      public Set<String> getMatchingFieldNames(String pattern)
      Returns the names of all mapped fields that match a given pattern All names returned by this method are guaranteed to resolve to a MappedFieldType if passed to getFieldType(String)
      pattern - the field name pattern
    • getFieldType

      public MappedFieldType getFieldType(String name)
      Returns the MappedFieldType for the provided field name. If the field is not mapped, the behaviour depends on the index.query.parse.allow_unmapped_fields setting, which defaults to true. In case unmapped fields are allowed, null is returned when the field is not mapped. In case unmapped fields are not allowed, either an exception is thrown or the field is automatically mapped as a text field.
      QueryShardException - if unmapped fields are not allowed and automatically mapping unmapped fields as text is disabled.
      See Also:
    • isFieldMapped

      public boolean isFieldMapped(String name)
      Returns true if the field identified by the provided name is mapped, false otherwise
    • getObjectMapper

      @Nullable public ObjectMapper getObjectMapper(String name)
      name - name of the object
      can be null e.g. if field is root of a composite runtime field
    • isMetadataField

      public boolean isMetadataField(String field)
    • sourcePath

      public Set<String> sourcePath(String fullName)
    • isSourceEnabled

      public boolean isSourceEnabled()
    • buildAnonymousFieldType

      public MappedFieldType buildAnonymousFieldType(String type)
      Given a type (eg. long, string, ...), returns an anonymous field type that can be used for search operations. Generally used to handle unmapped fields in the context of sorting.
    • getIndexAnalyzers

      public IndexAnalyzers getIndexAnalyzers()
    • getIndexAnalyzer

      public org.apache.lucene.analysis.Analyzer getIndexAnalyzer(Function<String,NamedAnalyzer> unindexedFieldAnalyzer)
      Return the index-time analyzer for the current index
      unindexedFieldAnalyzer - a function that builds an analyzer for unindexed fields
    • getValuesSourceRegistry

      public ValuesSourceRegistry getValuesSourceRegistry()
    • setAllowUnmappedFields

      public void setAllowUnmappedFields(boolean allowUnmappedFields)
    • setMapUnmappedFieldAsString

      public void setMapUnmappedFieldAsString(boolean mapUnmappedFieldAsString)
    • setAllowedFields

      public void setAllowedFields(Predicate<String> allowedFields)
    • queryTypes

      public Collection<String> queryTypes()
      Returns the narrowed down explicit types, or, if not set, all types.
    • containsBrokenAnalysis

      public boolean containsBrokenAnalysis(String field)
      Does the index analyzer for this field have token filters that may produce backwards offsets in term vectors
    • lookup

      public SearchLookup lookup()
      Get the lookup to use during the search.
    • nestedScope

      public NestedScope nestedScope()
    • indexVersionCreated

      public Version indexVersionCreated()
    • indexMatches

      public boolean indexMatches(String pattern)
      Given an index pattern, checks whether it matches against the current shard. The pattern may represent a fully qualified index name if the search targets remote shards.
    • indexSortedOnField

      public boolean indexSortedOnField(String field)
    • toQuery

      public ParsedQuery toQuery(QueryBuilder queryBuilder)
    • index

      public Index index()
    • compile

      public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context)
      Compile script using script service
    • freezeContext

      public final void freezeContext()
      if this method is called the query context will throw exception if methods are accessed that could yield different results across executions like getClient()
    • disableCache

      public void disableCache()
      Marks this context as not cacheable. This method fails if freezeContext() is called before on this context.
    • failIfFrozen

      protected final void failIfFrozen()
      This method fails if freezeContext() is called before on this context. This is used to seal. This methods and all methods that call it should be final to ensure that setting the request as not cacheable and the freezing behaviour of this class cannot be bypassed. This is important so we can trust when this class says a request can be cached.
    • registerAsyncAction

      public void registerAsyncAction(BiConsumer<Client,ActionListener<?>> asyncAction)
      Description copied from class: QueryRewriteContext
      Registers an async action that must be executed before the next rewrite round in order to make progress. This should be used if a rewriteabel needs to fetch some external resources in order to be executed ie. a document from an index.
      registerAsyncAction in class QueryRewriteContext
    • executeAsyncActions

      public void executeAsyncActions(ActionListener<Object> listener)
      Description copied from class: QueryRewriteContext
      Executes all registered async actions and notifies the listener once it's done. The value that is passed to the listener is always null. The list of registered actions is cleared once this method returns.
      executeAsyncActions in class QueryRewriteContext
    • isCacheable

      public final boolean isCacheable()
      Returns true iff the result of the processed search request is cacheable. Otherwise false
    • getShardId

      public int getShardId()
      Returns the shard ID this context was created for.
    • getShardRequestIndex

      public int getShardRequestIndex()
      Returns the shard request ordinal that is used by the main search request to reference this shard.
    • nowInMillis

      public final long nowInMillis()
      Description copied from class: QueryRewriteContext
      Returns the time in milliseconds that is shared across all resources involved. Even across shards and nodes.
      nowInMillis in class QueryRewriteContext
    • getClient

      public Client getClient()
    • parseInnerQueryBuilder

      public QueryBuilder parseInnerQueryBuilder(XContentParser parser) throws IOException
    • convertToSearchExecutionContext

      public final SearchExecutionContext convertToSearchExecutionContext()
      Description copied from class: QueryRewriteContext
      Returns an instance of SearchExecutionContext if available of null otherwise
      convertToSearchExecutionContext in class QueryRewriteContext
    • getIndexSettings

      public IndexSettings getIndexSettings()
      Returns the index settings for this context. This might return null if the context has not index scope.
    • getType

      public String getType()
    • getIndexReader

      public org.apache.lucene.index.IndexReader getIndexReader()
      Return the current IndexReader, or null if no index reader is available, for instance if this rewrite context is used to index queries (percolation).
    • searcher

      public searcher()
      Return the current IndexSearcher, or null if no index reader is available, for instance if this rewrite context is used to index queries (percolation).
    • getFullyQualifiedIndex

      public Index getFullyQualifiedIndex()
      Returns the fully qualified index including a remote cluster alias if applicable, and the index uuid
    • mappingCacheKey

      public MappingLookup.CacheKey mappingCacheKey()
      Cache key for current mapping.
    • getNestedParent

      public String getNestedParent(String nestedPath)
      Given a nested object path, returns the path to its nested parent In particular, if a nested field `foo` contains an object field `bar.baz`, then calling this method with `` will return the path `foo`, skipping over the object-but-not-nested ``
    • getImmediateChildMappers

      public List<String> getImmediateChildMappers(String path)
    • getNestedDocuments

      public NestedDocuments getNestedDocuments()