org.apache.lucene.queryParser.standard

## Class StandardQueryParser

- java.lang.Object
- - org.apache.lucene.queryParser.core.QueryParserHelper
  - - org.apache.lucene.queryParser.standard.StandardQueryParser

-     public class StandardQueryParser
      extends QueryParserHelper

  This class is a helper that enables users to easily use the Lucene query parser.

  To construct a Query object from a query string, use the `parse(String, String)` method:

  StandardQueryParser queryParserHelper = new StandardQueryParser();
  Query query = queryParserHelper.parse("a AND b", "defaultField");

  To change any configuration before parsing the query string do, for example:

  // the query config handler returned by `StandardQueryParser` is a `StandardQueryConfigHandler`
  queryParserHelper.getQueryConfigHandler().setAnalyzer(new WhitespaceAnalyzer());

  The syntax for query strings is as follows (copied from the old QueryParser javadoc):

  A Query is a series of clauses. A clause may be prefixed by:
  - a plus (`+`) or a minus (`-`) sign, indicating that the clause is required or prohibited respectively; or
  - a term followed by a colon, indicating the field to be searched. This enables one to construct queries which search multiple fields.
  A clause may be either:
  - a term, indicating all the documents that contain this term; or
  - a nested query, enclosed in parentheses. Note that this may be used with a `+`/`-` prefix to require any of a set of terms.
  Thus, in BNF, the query grammar is:

         Query  ::= ( Clause )*
         Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )

  Examples of appropriately formatted queries can be found in the query syntax documentation.

  The text parser used by this helper is a `StandardSyntaxParser`.

  The query node processor used by this helper is a `StandardQueryNodeProcessorPipeline`.

  The builder used by this helper is a `StandardQueryTreeBuilder`.

  See Also:
    `StandardQueryParser`, `StandardQueryConfigHandler`, `StandardSyntaxParser`, `StandardQueryNodeProcessorPipeline`, `StandardQueryTreeBuilder`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `StandardQueryParser()` Constructs a `StandardQueryParser` object.
    `StandardQueryParser(org.apache.lucene.analysis.Analyzer analyzer)` Constructs a `StandardQueryParser` object and sets an `Analyzer` to it.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `getAllowLeadingWildcard()`
    `org.apache.lucene.analysis.Analyzer` | `getAnalyzer()`
    `DefaultOperatorAttribute.Operator` | `getDefaultOperator()` Gets implicit operator setting, which will be either `DefaultOperatorAttribute.Operator.AND` or `DefaultOperatorAttribute.Operator.OR`.
    `boolean` | `getEnablePositionIncrements()`
    `float` | `getFuzzyMinSim()` Get the minimal similarity for fuzzy queries.
    `int` | `getFuzzyPrefixLength()` Get the prefix length for fuzzy queries.
    `java.util.Locale` | `getLocale()` Returns current locale, allowing access by subclasses.
    `boolean` | `getLowercaseExpandedTerms()`
    `org.apache.lucene.search.MultiTermQuery.RewriteMethod` | `getMultiTermRewriteMethod()`
    `int` | `getPhraseSlop()` Gets the default slop for phrases.
    `java.text.Collator` | `getRangeCollator()`
    `org.apache.lucene.search.Query` | `parse(java.lang.String query, java.lang.String defaultField)` Overrides `QueryParserHelper.parse(String, String)` so it casts the return object to `Query`.
    `void` | `setAllowLeadingWildcard(boolean allowLeadingWildcard)` Set to `true` to allow leading wildcard characters.
    `void` | `setAnalyzer(org.apache.lucene.analysis.Analyzer analyzer)`
    `void` | `setDateResolution(org.apache.lucene.document.DateTools.Resolution dateResolution)`
    `void` | `setDateResolution(java.util.Map<java.lang.CharSequence,org.apache.lucene.document.DateTools.Resolution> dateRes)`
    `void` | `setDefaultOperator(DefaultOperatorAttribute.Operator operator)` Sets the boolean operator of the QueryParser.
    `void` | `setDefaultPhraseSlop(int defaultPhraseSlop)` Sets the default slop for phrases.
    `void` | `setEnablePositionIncrements(boolean enabled)` Set to `true` to enable position increments in result query.
    `void` | `setFieldsBoost(java.util.Map<java.lang.CharSequence,java.lang.Float> boosts)`
    `void` | `setFuzzyMinSim(float fuzzyMinSim)` Set the minimum similarity for fuzzy queries.
    `void` | `setFuzzyPrefixLength(int fuzzyPrefixLength)` Set the prefix length for fuzzy queries.
    `void` | `setLocale(java.util.Locale locale)` Set locale used by date range parsing.
    `void` | `setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)` Set to `true` to allow leading wildcard characters.
    `void` | `setMultiFields(java.lang.CharSequence[] fields)`
    `void` | `setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod method)` By default, it uses `MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT` when creating a prefix, wildcard and range queries.
    `void` | `setRangeCollator(java.text.Collator collator)` Sets the collator used to determine index term inclusion in ranges for RangeQuerys.
    `java.lang.String` | `toString()`

    - ### Methods inherited from class org.apache.lucene.queryParser.core.QueryParserHelper

      `getQueryBuilder, getQueryConfigHandler, getQueryNodeProcessor, getSyntaxParser, setQueryBuilder, setQueryConfigHandler, setQueryNodeProcessor, setSyntaxParser`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### StandardQueryParser

          public StandardQueryParser()

      Constructs a `StandardQueryParser` object.

    - #### StandardQueryParser

          public StandardQueryParser(org.apache.lucene.analysis.Analyzer analyzer)

      Constructs a `StandardQueryParser` object and sets an `Analyzer` to it. The same as:
      StandardQueryParser qp = new StandardQueryParser(); qp.getQueryConfigHandler().setAnalyzer(analyzer);
      Parameters:
        `analyzer` - the analyzer to be used by this query parser helper

  - ### Method Detail

    - #### toString

          public java.lang.String toString()

      Overrides:
        `toString` in class `java.lang.Object`

    - #### parse

          public org.apache.lucene.search.Query parse(java.lang.String query,
                                                      java.lang.String defaultField)
                                               throws QueryNodeException

      Overrides `QueryParserHelper.parse(String, String)` so it casts the return object to `Query`. For more reference about this method, check `QueryParserHelper.parse(String, String)`.
      Overrides:
        `parse` in class `QueryParserHelper`
      Parameters:
        `query` - the query string
        `defaultField` - the default field used by the text parser
      Returns:
        the object built from the query
      Throws:
        `QueryNodeException` - if something wrong happens along the three phases

    - #### getDefaultOperator

          public DefaultOperatorAttribute.Operator getDefaultOperator()

      Gets implicit operator setting, which will be either `DefaultOperatorAttribute.Operator.AND` or `DefaultOperatorAttribute.Operator.OR`.

    - #### setRangeCollator

          public void setRangeCollator(java.text.Collator collator)

      Sets the collator used to determine index term inclusion in ranges for RangeQuerys.

      WARNING: Setting the rangeCollator to a non-null collator using this method will cause every single index Term in the Field referenced by lowerTerm and/or upperTerm to be examined. Depending on the number of index Terms in this Field, the operation could be very slow.
      Parameters:
        `collator` - the collator to use when constructing `RangeQueryNode`s

    - #### getRangeCollator

          public java.text.Collator getRangeCollator()

      Returns:
        the collator used to determine index term inclusion in ranges for RangeQuerys.

    - #### setDefaultOperator

          public void setDefaultOperator(DefaultOperatorAttribute.Operator operator)

      Sets the boolean operator of the QueryParser. In default mode ( `DefaultOperatorAttribute.Operator.OR`) terms without any modifiers are considered optional: for example `capital of Hungary` is equal to `capital OR of OR Hungary`.
      In `DefaultOperatorAttribute.Operator.AND` mode terms are considered to be in conjunction: the above mentioned query is parsed as `capital AND of AND Hungary`

    - #### setLowercaseExpandedTerms

          public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)

      Set to `true` to allow leading wildcard characters.

      When set, `*` or `?` are allowed as the first character of a PrefixQuery and WildcardQuery. Note that this can produce very slow queries on big indexes.

      Default: false.

    - #### getLowercaseExpandedTerms

          public boolean getLowercaseExpandedTerms()

      See Also:
        `setLowercaseExpandedTerms(boolean)`

    - #### setAllowLeadingWildcard

          public void setAllowLeadingWildcard(boolean allowLeadingWildcard)

      Set to `true` to allow leading wildcard characters.

      When set, `*` or `?` are allowed as the first character of a PrefixQuery and WildcardQuery. Note that this can produce very slow queries on big indexes.

      Default: false.

    - #### setEnablePositionIncrements

          public void setEnablePositionIncrements(boolean enabled)

      Set to `true` to enable position increments in result query.

      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.

      Default: false.

    - #### getEnablePositionIncrements

          public boolean getEnablePositionIncrements()

      See Also:
        `setEnablePositionIncrements(boolean)`

    - #### setMultiTermRewriteMethod

          public void setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod method)

      By default, it uses `MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT` when creating a prefix, wildcard and range queries. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of terms unduly influence score c) avoids any `TooManyListenersException` exception. However, if your application really needs to use the old-fashioned boolean queries expansion rewriting and the above points are not relevant then use this change the rewrite method.

    - #### getMultiTermRewriteMethod

          public org.apache.lucene.search.MultiTermQuery.RewriteMethod getMultiTermRewriteMethod()

      See Also:
        `setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)`

    - #### setMultiFields

          public void setMultiFields(java.lang.CharSequence[] fields)

    - #### setFuzzyPrefixLength

          public void setFuzzyPrefixLength(int fuzzyPrefixLength)

      Set the prefix length for fuzzy queries. Default is 0.
      Parameters:
        `fuzzyPrefixLength` - The fuzzyPrefixLength to set.

    - #### setLocale

          public void setLocale(java.util.Locale locale)

      Set locale used by date range parsing.

    - #### getLocale

          public java.util.Locale getLocale()

      Returns current locale, allowing access by subclasses.

    - #### setDefaultPhraseSlop

          public void setDefaultPhraseSlop(int defaultPhraseSlop)

      Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.

    - #### setAnalyzer

          public void setAnalyzer(org.apache.lucene.analysis.Analyzer analyzer)

    - #### getAnalyzer

          public org.apache.lucene.analysis.Analyzer getAnalyzer()

    - #### getAllowLeadingWildcard

          public boolean getAllowLeadingWildcard()

      See Also:
        `setAllowLeadingWildcard(boolean)`

    - #### getFuzzyMinSim

          public float getFuzzyMinSim()

      Get the minimal similarity for fuzzy queries.

    - #### getFuzzyPrefixLength

          public int getFuzzyPrefixLength()

      Get the prefix length for fuzzy queries.
      Returns:
        Returns the fuzzyPrefixLength.

    - #### getPhraseSlop

          public int getPhraseSlop()

      Gets the default slop for phrases.

    - #### setFuzzyMinSim

          public void setFuzzyMinSim(float fuzzyMinSim)

      Set the minimum similarity for fuzzy queries. Default is defined on `FuzzyQuery.defaultMinSimilarity`.

    - #### setFieldsBoost

          public void setFieldsBoost(java.util.Map<java.lang.CharSequence,java.lang.Float> boosts)

    - #### setDateResolution

          public void setDateResolution(org.apache.lucene.document.DateTools.Resolution dateResolution)

    - #### setDateResolution

          public void setDateResolution(java.util.Map<java.lang.CharSequence,org.apache.lucene.document.DateTools.Resolution> dateRes)

Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.