Class QueryStringQueryBuilder

All Implemented Interfaces:
NamedWriteable, Writeable, org.elasticsearch.common.xcontent.ToXContent, org.elasticsearch.common.xcontent.ToXContentObject, QueryBuilder, Rewriteable<QueryBuilder>

public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQueryBuilder>
A query that parses a query string and runs it. There are two modes that this operates. The first, when no field is added (using field(String), will run the query once and non prefixed fields will use the defaultField(String) set. The second, when one or more fields are added (using field(String)), will run the parsed query against the provided fields, and combine them using Dismax.
  • Field Details

  • Constructor Details

    • QueryStringQueryBuilder

      public QueryStringQueryBuilder(String queryString)
    • QueryStringQueryBuilder

      public QueryStringQueryBuilder(StreamInput in) throws IOException
      Read from a stream.
      Throws:
      IOException
  • Method Details

    • doWriteTo

      protected void doWriteTo(StreamOutput out) throws IOException
      Specified by:
      doWriteTo in class AbstractQueryBuilder<QueryStringQueryBuilder>
      Throws:
      IOException
    • queryString

      public String queryString()
    • defaultField

      public QueryStringQueryBuilder defaultField(String defaultField)
      The default field to run against when no prefix field is specified. Only relevant when not explicitly adding fields the query string will run against.
    • defaultField

      public String defaultField()
    • field

      public QueryStringQueryBuilder field(String field)
      Adds a field to run the query string against. The field will be associated with the default boost of AbstractQueryBuilder.DEFAULT_BOOST. Use field(String, float) to set a specific boost for the field.
    • field

      public QueryStringQueryBuilder field(String field, float boost)
      Adds a field to run the query string against with a specific boost.
    • fields

      public QueryStringQueryBuilder fields(Map<String,Float> fields)
      Add several fields to run the query against with a specific boost.
    • fields

      public Map<String,Float> fields()
      Returns the fields including their respective boosts to run the query against.
    • type

      Parameters:
      type - Sets how multiple fields should be combined to build textual part queries.
    • tieBreaker

      public QueryStringQueryBuilder tieBreaker(float tieBreaker)
      When more than one field is used with the query string, and combined queries are using dis max, control the tie breaker for it.
    • tieBreaker

      public Float tieBreaker()
    • defaultOperator

      public QueryStringQueryBuilder defaultOperator(Operator defaultOperator)
      Sets the boolean operator of the query parser used to parse the query string.

      In default mode (Operator.OR) terms without any modifiers are considered optional: for example capital of Hungary is equal to capital OR of OR Hungary.

      In Operator.AND mode terms are considered to be in conjunction: the above mentioned query is parsed as capital AND of AND Hungary

    • defaultOperator

      public Operator defaultOperator()
    • analyzer

      public QueryStringQueryBuilder analyzer(String analyzer)
      The optional analyzer used to analyze the query string. Note, if a field has search analyzer defined for it, then it will be used automatically. Defaults to the smart search analyzer.
    • analyzer

      public String analyzer()
      The optional analyzer used to analyze the query string. Note, if a field has search analyzer defined for it, then it will be used automatically. Defaults to the smart search analyzer.
    • quoteAnalyzer

      public String quoteAnalyzer()
      The optional analyzer used to analyze the query string for phrase searches. Note, if a field has search (quote) analyzer defined for it, then it will be used automatically. Defaults to the smart search analyzer.
    • quoteAnalyzer

      public QueryStringQueryBuilder quoteAnalyzer(String quoteAnalyzer)
      The optional analyzer used to analyze the query string for phrase searches. Note, if a field has search (quote) analyzer defined for it, then it will be used automatically. Defaults to the smart search analyzer.
    • maxDeterminizedStates

      public QueryStringQueryBuilder maxDeterminizedStates(int maxDeterminizedStates)
      Protects against too-difficult regular expression queries.
    • maxDeterminizedStates

      public int maxDeterminizedStates()
    • allowLeadingWildcard

      public QueryStringQueryBuilder allowLeadingWildcard(Boolean allowLeadingWildcard)
      Should leading wildcards be allowed or not. Defaults to true.
    • allowLeadingWildcard

      public Boolean allowLeadingWildcard()
    • enablePositionIncrements

      public QueryStringQueryBuilder enablePositionIncrements(boolean enablePositionIncrements)
      Set to true to enable position increments in result query. Defaults to true.

      When set, result phrase and multi-phrase queries will be aware of position increments. Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token.

    • enablePositionIncrements

      public boolean enablePositionIncrements()
    • fuzziness

      public QueryStringQueryBuilder fuzziness(Fuzziness fuzziness)
      Set the edit distance for fuzzy queries. Default is "AUTO".
    • fuzziness

      public Fuzziness fuzziness()
    • fuzzyPrefixLength

      public QueryStringQueryBuilder fuzzyPrefixLength(int fuzzyPrefixLength)
      Set the minimum prefix length for fuzzy queries. Default is 1.
    • fuzzyPrefixLength

      public int fuzzyPrefixLength()
    • fuzzyMaxExpansions

      public QueryStringQueryBuilder fuzzyMaxExpansions(int fuzzyMaxExpansions)
    • fuzzyMaxExpansions

      public int fuzzyMaxExpansions()
    • fuzzyRewrite

      public QueryStringQueryBuilder fuzzyRewrite(String fuzzyRewrite)
    • fuzzyRewrite

      public String fuzzyRewrite()
    • phraseSlop

      public QueryStringQueryBuilder phraseSlop(int phraseSlop)
      Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.
    • phraseSlop

      public int phraseSlop()
    • rewrite

      public QueryStringQueryBuilder rewrite(String rewrite)
    • analyzeWildcard

      public QueryStringQueryBuilder analyzeWildcard(Boolean analyzeWildcard)
      Set to true to enable analysis on wildcard and prefix queries.
    • analyzeWildcard

      public Boolean analyzeWildcard()
    • rewrite

      public String rewrite()
    • minimumShouldMatch

      public QueryStringQueryBuilder minimumShouldMatch(String minimumShouldMatch)
    • minimumShouldMatch

      public String minimumShouldMatch()
    • quoteFieldSuffix

      public QueryStringQueryBuilder quoteFieldSuffix(String quoteFieldSuffix)
      An optional field name suffix to automatically try and add to the field searched when using quoted text.
    • quoteFieldSuffix

      public String quoteFieldSuffix()
    • lenient

      public QueryStringQueryBuilder lenient(Boolean lenient)
      Sets the query string parser to be lenient when parsing field values, defaults to the index setting and if not set, defaults to false.
    • lenient

      public Boolean lenient()
    • timeZone

      public QueryStringQueryBuilder timeZone(String timeZone)
      In case of date field, we can adjust the from/to fields using a timezone
    • timeZone

      public QueryStringQueryBuilder timeZone(ZoneId timeZone)
    • timeZone

      public ZoneId timeZone()
    • escape

      public QueryStringQueryBuilder escape(boolean escape)
      Set to true to enable escaping of the query string
    • escape

      public boolean escape()
    • autoGenerateSynonymsPhraseQuery

      public QueryStringQueryBuilder autoGenerateSynonymsPhraseQuery(boolean value)
    • autoGenerateSynonymsPhraseQuery

      public boolean autoGenerateSynonymsPhraseQuery()
      Whether phrase queries should be automatically generated for multi terms synonyms. Defaults to true.
    • fuzzyTranspositions

      public boolean fuzzyTranspositions()
    • fuzzyTranspositions

      public QueryStringQueryBuilder fuzzyTranspositions(boolean fuzzyTranspositions)
      Sets whether transpositions are supported in fuzzy queries.

      The default metric used by fuzzy queries to determine a match is the Damerau-Levenshtein distance formula which supports transpositions. Setting transposition to false will switch to classic Levenshtein distance.
      If not set, Damerau-Levenshtein distance metric will be used.

    • doXContent

      protected void doXContent(org.elasticsearch.common.xcontent.XContentBuilder builder, org.elasticsearch.common.xcontent.ToXContent.Params params) throws IOException
      Specified by:
      doXContent in class AbstractQueryBuilder<QueryStringQueryBuilder>
      Throws:
      IOException
    • fromXContent

      public static QueryStringQueryBuilder fromXContent(org.elasticsearch.common.xcontent.XContentParser parser) throws IOException
      Throws:
      IOException
    • getWriteableName

      public String getWriteableName()
      Description copied from interface: NamedWriteable
      Returns the name of the writeable object
    • doEquals

      protected boolean doEquals(QueryStringQueryBuilder other)
      Description copied from class: AbstractQueryBuilder
      Indicates whether some other QueryBuilder object of the same type is "equal to" this one.
      Specified by:
      doEquals in class AbstractQueryBuilder<QueryStringQueryBuilder>
    • doHashCode

      protected int doHashCode()
      Specified by:
      doHashCode in class AbstractQueryBuilder<QueryStringQueryBuilder>
    • doToQuery

      protected org.apache.lucene.search.Query doToQuery(SearchExecutionContext context) throws IOException
      Specified by:
      doToQuery in class AbstractQueryBuilder<QueryStringQueryBuilder>
      Throws:
      IOException