Class QueryParser

    • Field Detail

      • AND_OPERATOR

        public static final QueryParser.Operator AND_OPERATOR
        Alternative form of QueryParser.Operator.AND
      • OR_OPERATOR

        public static final QueryParser.Operator OR_OPERATOR
        Alternative form of QueryParser.Operator.OR
      • token

        public Token token
        Current token.
      • jj_nt

        public Token jj_nt
        Next token.
    • Constructor Detail

      • QueryParser

        public QueryParser​(Version matchVersion,
                           String f,
                           Analyzer a)
        Constructs a query parser.
        Parameters:
        matchVersion - Lucene version to match. See above.
        f - the default field for query terms.
        a - used to find terms in the query text.
      • QueryParser

        protected QueryParser​(CharStream stream)
        Constructor with user supplied CharStream.
    • Method Detail

      • getAnalyzer

        public Analyzer getAnalyzer()
        Returns:
        Returns the analyzer.
      • getField

        public String getField()
        Returns:
        Returns the field.
      • setAutoGeneratePhraseQueries

        public final void setAutoGeneratePhraseQueries​(boolean value)
        Set to true if phrase queries will be automatically generated when the analyzer returns more than one term from whitespace delimited text. NOTE: this behavior may not be suitable for all languages.

        Set to false if phrase queries should only be generated when surrounded by double quotes.

      • getFuzzyMinSim

        public float getFuzzyMinSim()
        Get the minimal similarity for fuzzy queries.
      • setFuzzyMinSim

        public void setFuzzyMinSim​(float fuzzyMinSim)
        Set the minimum similarity for fuzzy queries. Default is 0.5f.
      • getFuzzyPrefixLength

        public int getFuzzyPrefixLength()
        Get the prefix length for fuzzy queries.
        Returns:
        Returns the fuzzyPrefixLength.
      • setFuzzyPrefixLength

        public void setFuzzyPrefixLength​(int fuzzyPrefixLength)
        Set the prefix length for fuzzy queries. Default is 0.
        Parameters:
        fuzzyPrefixLength - The fuzzyPrefixLength to set.
      • setPhraseSlop

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

        public int getPhraseSlop()
        Gets the default slop for phrases.
      • 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 enable)
        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.

      • setDefaultOperator

        public void setDefaultOperator​(QueryParser.Operator op)
        Sets the boolean operator of the QueryParser. In default mode (OR_OPERATOR) terms without any modifiers are considered optional: for example capital of Hungary is equal to capital OR of OR Hungary.
        In AND_OPERATOR mode terms are considered to be in conjunction: the above mentioned query is parsed as capital AND of AND Hungary
      • getDefaultOperator

        public QueryParser.Operator getDefaultOperator()
        Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
      • setLowercaseExpandedTerms

        public void setLowercaseExpandedTerms​(boolean lowercaseExpandedTerms)
        Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default is true.
      • setMultiTermRewriteMethod

        public void setMultiTermRewriteMethod​(MultiTermQuery.RewriteMethod method)
        By default QueryParser uses MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT when creating a PrefixQuery, WildcardQuery or RangeQuery. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of terms unduly influence score c) avoids any "TooManyBooleanClauses" exception. However, if your application really needs to use the old-fashioned BooleanQuery expansion rewriting and the above points are not relevant then use this to change the rewrite method.
      • setLocale

        public void setLocale​(Locale locale)
        Set locale used by date range parsing.
      • getLocale

        public Locale getLocale()
        Returns current locale, allowing access by subclasses.
      • setDateResolution

        public void setDateResolution​(DateTools.Resolution dateResolution)
        Sets the default date resolution used by RangeQueries for fields for which no specific date resolutions has been set. Field specific resolutions can be set with setDateResolution(String, DateTools.Resolution).
        Parameters:
        dateResolution - the default date resolution to set
      • setDateResolution

        public void setDateResolution​(String fieldName,
                                      DateTools.Resolution dateResolution)
        Sets the date resolution used by RangeQueries for a specific field.
        Parameters:
        fieldName - field for which the date resolution is to be set
        dateResolution - date resolution to set
      • getDateResolution

        public DateTools.Resolution getDateResolution​(String fieldName)
        Returns the date resolution that is used by RangeQueries for the given field. Returns null, if no default or field specific date resolution has been set for the given field.
      • setRangeCollator

        public void setRangeCollator​(Collator rc)
        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:
        rc - the collator to use when constructing RangeQuerys
      • getRangeCollator

        public Collator getRangeCollator()
        Returns:
        the collator used to determine index term inclusion in ranges for RangeQuerys.
      • newBooleanQuery

        protected BooleanQuery newBooleanQuery​(boolean disableCoord)
        Builds a new BooleanQuery instance
        Parameters:
        disableCoord - disable coord
        Returns:
        new BooleanQuery instance
      • newBooleanClause

        protected BooleanClause newBooleanClause​(Query q,
                                                 BooleanClause.Occur occur)
        Builds a new BooleanClause instance
        Parameters:
        q - sub query
        occur - how this clause should occur when matching documents
        Returns:
        new BooleanClause instance
      • newTermQuery

        protected Query newTermQuery​(Term term)
        Builds a new TermQuery instance
        Parameters:
        term - term
        Returns:
        new TermQuery instance
      • newPhraseQuery

        protected PhraseQuery newPhraseQuery()
        Builds a new PhraseQuery instance
        Returns:
        new PhraseQuery instance
      • newMultiPhraseQuery

        protected MultiPhraseQuery newMultiPhraseQuery()
        Builds a new MultiPhraseQuery instance
        Returns:
        new MultiPhraseQuery instance
      • newPrefixQuery

        protected Query newPrefixQuery​(Term prefix)
        Builds a new PrefixQuery instance
        Parameters:
        prefix - Prefix term
        Returns:
        new PrefixQuery instance
      • newFuzzyQuery

        protected Query newFuzzyQuery​(Term term,
                                      float minimumSimilarity,
                                      int prefixLength)
        Builds a new FuzzyQuery instance
        Parameters:
        term - Term
        minimumSimilarity - minimum similarity
        prefixLength - prefix length
        Returns:
        new FuzzyQuery Instance
      • newRangeQuery

        protected Query newRangeQuery​(String field,
                                      String part1,
                                      String part2,
                                      boolean inclusive)
        Builds a new TermRangeQuery instance
        Parameters:
        field - Field
        part1 - min
        part2 - max
        inclusive - true if range is inclusive
        Returns:
        new TermRangeQuery instance
      • newMatchAllDocsQuery

        protected Query newMatchAllDocsQuery()
        Builds a new MatchAllDocsQuery instance
        Returns:
        new MatchAllDocsQuery instance
      • newWildcardQuery

        protected Query newWildcardQuery​(Term t)
        Builds a new WildcardQuery instance
        Parameters:
        t - wildcard term
        Returns:
        new WildcardQuery instance
      • getBooleanQuery

        protected Query getBooleanQuery​(List<BooleanClause> clauses)
                                 throws ParseException
        Factory method for generating query, given a set of clauses. By default creates a boolean query composed of clauses passed in. Can be overridden by extending classes, to modify query being returned.
        Parameters:
        clauses - List that contains BooleanClause instances to join.
        Returns:
        Resulting Query object.
        Throws:
        ParseException - throw in overridden method to disallow
      • getBooleanQuery

        protected Query getBooleanQuery​(List<BooleanClause> clauses,
                                        boolean disableCoord)
                                 throws ParseException
        Factory method for generating query, given a set of clauses. By default creates a boolean query composed of clauses passed in. Can be overridden by extending classes, to modify query being returned.
        Parameters:
        clauses - List that contains BooleanClause instances to join.
        disableCoord - true if coord scoring should be disabled.
        Returns:
        Resulting Query object.
        Throws:
        ParseException - throw in overridden method to disallow
      • getWildcardQuery

        protected Query getWildcardQuery​(String field,
                                         String termStr)
                                  throws ParseException
        Factory method for generating a query. Called when parser parses an input term token that contains one or more wildcard characters (? and *), but is not a prefix term token (one that has just a single * character at the end)

        Depending on settings, prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.

        Can be overridden by extending classes, to provide custom handling for wildcard queries, which may be necessary due to missing analyzer calls.

        Parameters:
        field - Name of the field query will use.
        termStr - Term token that contains one or more wild card characters (? or *), but is not simple prefix term
        Returns:
        Resulting Query built for the term
        Throws:
        ParseException - throw in overridden method to disallow
      • getPrefixQuery

        protected Query getPrefixQuery​(String field,
                                       String termStr)
                                throws ParseException
        Factory method for generating a query (similar to getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term token that uses prefix notation; that is, contains a single '*' wildcard character as its last character. Since this is a special case of generic wildcard term, and such a query can be optimized easily, this usually results in a different query object.

        Depending on settings, a prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.

        Can be overridden by extending classes, to provide custom handling for wild card queries, which may be necessary due to missing analyzer calls.

        Parameters:
        field - Name of the field query will use.
        termStr - Term token to use for building term for the query (without trailing '*' character!)
        Returns:
        Resulting Query built for the term
        Throws:
        ParseException - throw in overridden method to disallow
      • getFuzzyQuery

        protected Query getFuzzyQuery​(String field,
                                      String termStr,
                                      float minSimilarity)
                               throws ParseException
        Factory method for generating a query (similar to getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term token that has the fuzzy suffix (~) appended.
        Parameters:
        field - Name of the field query will use.
        termStr - Term token to use for building term for the query
        Returns:
        Resulting Query built for the term
        Throws:
        ParseException - throw in overridden method to disallow
      • escape

        public static String escape​(String s)
        Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.
      • main

        public static void main​(String[] args)
                         throws Exception
        Command line tool to test QueryParser, using SimpleAnalyzer. Usage:
        java org.apache.lucene.queryParser.QueryParser <input>
        Throws:
        Exception
      • ReInit

        public void ReInit​(CharStream stream)
        Reinitialise.
      • getNextToken

        public final Token getNextToken()
        Get the next Token.
      • getToken

        public final Token getToken​(int index)
        Get the specific Token.
      • generateParseException

        public ParseException generateParseException()
        Generate ParseException.
      • enable_tracing

        public final void enable_tracing()
        Enable tracing.
      • disable_tracing

        public final void disable_tracing()
        Disable tracing.