Class QueryBuilder

  • Direct Known Subclasses:
    QueryParserBase, SimpleQueryParser

    public class QueryBuilder
    extends java.lang.Object
    Creates queries from the Analyzer chain.

    Example usage:

       QueryBuilder builder = new QueryBuilder(analyzer);
       Query a = builder.createBooleanQuery("body", "just a test");
       Query b = builder.createPhraseQuery("body", "another test");
       Query c = builder.createMinShouldMatchQuery("body", "another test", 0.5f);
     

    This can also be used as a subclass for query parsers to make it easier to interact with the analysis chain. Factory methods such as newTermQuery are provided so that the generated queries can be customized.

    • Constructor Detail

      • QueryBuilder

        public QueryBuilder​(Analyzer analyzer)
        Creates a new QueryBuilder using the given analyzer.
    • Method Detail

      • createBooleanQuery

        public Query createBooleanQuery​(java.lang.String field,
                                        java.lang.String queryText)
        Creates a boolean query from the query text.

        This is equivalent to createBooleanQuery(field, queryText, Occur.SHOULD)

        Parameters:
        field - field name
        queryText - text to be passed to the analyzer
        Returns:
        TermQuery or BooleanQuery, based on the analysis of queryText
      • createBooleanQuery

        public Query createBooleanQuery​(java.lang.String field,
                                        java.lang.String queryText,
                                        BooleanClause.Occur operator)
        Creates a boolean query from the query text.

        Parameters:
        field - field name
        queryText - text to be passed to the analyzer
        operator - operator used for clauses between analyzer tokens.
        Returns:
        TermQuery or BooleanQuery, based on the analysis of queryText
      • createPhraseQuery

        public Query createPhraseQuery​(java.lang.String field,
                                       java.lang.String queryText)
        Creates a phrase query from the query text.

        This is equivalent to createPhraseQuery(field, queryText, 0)

        Parameters:
        field - field name
        queryText - text to be passed to the analyzer
        Returns:
        TermQuery, BooleanQuery, PhraseQuery, or MultiPhraseQuery, based on the analysis of queryText
      • createPhraseQuery

        public Query createPhraseQuery​(java.lang.String field,
                                       java.lang.String queryText,
                                       int phraseSlop)
        Creates a phrase query from the query text.

        Parameters:
        field - field name
        queryText - text to be passed to the analyzer
        phraseSlop - number of other words permitted between words in query phrase
        Returns:
        TermQuery, BooleanQuery, PhraseQuery, or MultiPhraseQuery, based on the analysis of queryText
      • createMinShouldMatchQuery

        public Query createMinShouldMatchQuery​(java.lang.String field,
                                               java.lang.String queryText,
                                               float fraction)
        Creates a minimum-should-match query from the query text.

        Parameters:
        field - field name
        queryText - text to be passed to the analyzer
        fraction - of query terms [0..1] that should match
        Returns:
        TermQuery or BooleanQuery, based on the analysis of queryText
      • setAnalyzer

        public void setAnalyzer​(Analyzer analyzer)
        Sets the analyzer used to tokenize text.
      • 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: true.