Class DefaultQueryParser

java.lang.Object
org.apache.lucene.util.QueryBuilder
org.apache.lucene.queryparser.classic.QueryParserBase
org.apache.lucene.queryparser.classic.QueryParser
org.apache.lucene.queryparser.classic.MultiFieldQueryParser
com.jalios.jcms.search.queryparser.DefaultQueryParser
All Implemented Interfaces:
org.apache.lucene.queryparser.classic.QueryParserConstants, org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration

public class DefaultQueryParser extends org.apache.lucene.queryparser.classic.MultiFieldQueryParser
Classic QueryParser with some added feature.

Automatic right truncation if enabled with property query.lucene.use-right-truncation.

Additionnal behavior can be configured through properties :

  • Disable wildcard query through property query.lucene.wildcard.enabled.
  • Disable fuzzy query through property query.lucene.fuzzy.enabled.
  • Use scoring boolean rewrite
    • for all multi-term query, right truncation PrefixQuery, through property query.lucene.scoring-boolean-rewrite.all.enabled.
    • for right truncation PrefixQuery only, through property query.lucene.scoring-boolean-rewrite.right-truncation.enabled.
    Warning, CPU intensive option disabled by default. (available since jcms-10.0.2 / JCMS-7005)
Since:
jcms-10.0
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.apache.lucene.queryparser.classic.QueryParser

    org.apache.lucene.queryparser.classic.QueryParser.Operator

    Nested classes/interfaces inherited from class org.apache.lucene.util.QueryBuilder

    org.apache.lucene.util.QueryBuilder.TermAndBoost
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static boolean
     
    static boolean
     
    static String
    A regular expression used to match words in user's search string for which right truncation will NOT be applied.
    static boolean
     

    Fields inherited from class org.apache.lucene.queryparser.classic.MultiFieldQueryParser

    boosts, fields

    Fields inherited from class org.apache.lucene.queryparser.classic.QueryParser

    DEFAULT_SPLIT_ON_WHITESPACE, jj_nt, token, token_source

    Fields inherited from class org.apache.lucene.queryparser.classic.QueryParserBase

    AND_OPERATOR, field, OR_OPERATOR

    Fields inherited from class org.apache.lucene.util.QueryBuilder

    analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements

    Fields inherited from interface org.apache.lucene.queryparser.classic.QueryParserConstants

    _ESCAPED_CHAR, _NUM_CHAR, _QUOTED_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, AND, BAREOPER, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, Range, RANGE_GOOP, RANGE_QUOTED, RANGE_TO, RANGEEX_END, RANGEEX_START, RANGEIN_END, RANGEIN_START, REGEXPTERM, RPAREN, STAR, TERM, tokenImage, WILDTERM
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultQueryParser(String[] fields, org.apache.lucene.analysis.Analyzer analyzer, Map<String,Float> boosts, ParseOptions options)
    Creates a DefaultQueryParser.
    DefaultQueryParser(String field, org.apache.lucene.analysis.Analyzer analyzer, ParseOptions options)
    Deprecated, for removal: This API element is subject to removal in a future version.
    since use of MultiFieldQueryParser with JCMS-11884
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    addMultiTermClauses(List<org.apache.lucene.search.BooleanClause> clauses, org.apache.lucene.search.Query q)
     
    static float
    Return the boost that should be applied to a field at query time for text search.
    protected org.apache.lucene.search.Query
    getFieldQuery(String field, String queryText, boolean quoted)
     
    protected org.apache.lucene.search.Query
    getFuzzyQuery(String field, String termStr, float minSimilarity)
    Disable fuzzy query.
    protected org.apache.lucene.search.Query
    getMultiTermPositionQuery(List<org.apache.lucene.search.Query> positionGroups)
    Combines the per-position field-disjunction groups above when a single getFieldQuery call is analyzed into more than one term.
    protected org.apache.lucene.search.Query
    getWildcardQuery(String field, String termStr)
    Disable wildcard query.
    protected org.apache.lucene.search.Query
    newTermQuery(org.apache.lucene.index.Term term, float boost)
     
    static org.apache.lucene.search.Query
    parse(String searchString, ParseOptions options, org.apache.lucene.analysis.Analyzer analyzer)
    Invoked by JCMS Lucene search engines to parse query.
    static String
    rewriteSearchString(String searchString, ParseOptions options)
    Compute the search string to use from the specified options

    Methods inherited from class org.apache.lucene.queryparser.classic.MultiFieldQueryParser

    getFieldQuery, getMultiFieldQuery, getPrefixQuery, getRangeQuery, getRegexpQuery, parse, parse, parse

    Methods inherited from class org.apache.lucene.queryparser.classic.QueryParser

    Clause, Conjunction, disable_tracing, enable_tracing, generateParseException, getNextToken, getSplitOnWhitespace, getToken, Modifiers, MultiTerm, Query, ReInit, ReInit, setAutoGeneratePhraseQueries, setSplitOnWhitespace, Term, TopLevelQuery, trace_enabled

    Methods inherited from class org.apache.lucene.queryparser.classic.QueryParserBase

    addClause, escape, getAllowLeadingWildcard, getAutoGeneratePhraseQueries, getBooleanQuery, getDateResolution, getDefaultOperator, getDeterminizeWorkLimit, getField, getFuzzyDistance, getFuzzyMinSim, getFuzzyPrefixLength, getLocale, getMultiTermRewriteMethod, getPhraseSlop, getTimeZone, init, newBooleanClause, newFieldQuery, newFuzzyQuery, newMatchAllDocsQuery, newPrefixQuery, newRangeQuery, newRegexpQuery, newWildcardQuery, parse, setAllowLeadingWildcard, setDateResolution, setDateResolution, setDefaultOperator, setDeterminizeWorkLimit, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setMultiTermRewriteMethod, setPhraseSlop, setTimeZone

    Methods inherited from class org.apache.lucene.util.QueryBuilder

    add, analyzeBoolean, analyzeGraphBoolean, analyzeGraphPhrase, analyzeMultiBoolean, analyzeMultiPhrase, analyzePhrase, analyzeTerm, createBooleanQuery, createBooleanQuery, createFieldQuery, createFieldQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, getAnalyzer, getAutoGenerateMultiTermSynonymsPhraseQuery, getEnableGraphQueries, getEnablePositionIncrements, newBooleanQuery, newGraphSynonymQuery, newMultiPhraseQueryBuilder, newSynonymQuery, setAnalyzer, setAutoGenerateMultiTermSynonymsPhraseQuery, setEnableGraphQueries, setEnablePositionIncrements

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration

    getAnalyzer, getEnablePositionIncrements, setEnablePositionIncrements
  • Field Details

    • LUCENE_WILDCARD_ENABLED

      public static boolean LUCENE_WILDCARD_ENABLED
    • LUCENE_FUZZY_ENABLED

      public static boolean LUCENE_FUZZY_ENABLED
    • USE_RIGHT_TRUNCATION

      public static boolean USE_RIGHT_TRUNCATION
    • RIGHT_TRUNCATION_EXCLUSION_REGEX

      public static String RIGHT_TRUNCATION_EXCLUSION_REGEX
      A regular expression used to match words in user's search string for which right truncation will NOT be applied.

      Default regex applies to :

      • punctuation...
      • words ending with punctuation (for example "said" and "world" in : "john said: hello world!")
      • numbers, as we want to match them precisely (it also prevents TooManyBoolean with big index)
  • Constructor Details

    • DefaultQueryParser

      @Deprecated(forRemoval=true) public DefaultQueryParser(String field, org.apache.lucene.analysis.Analyzer analyzer, ParseOptions options)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since use of MultiFieldQueryParser with JCMS-11884
      Builds a new DefaultQueryParser.
      Parameters:
      field - the default field for query terms.
      analyzer - used to find terms in the query text.
      options - parsing options
    • DefaultQueryParser

      public DefaultQueryParser(String[] fields, org.apache.lucene.analysis.Analyzer analyzer, Map<String,Float> boosts, ParseOptions options)
      Creates a DefaultQueryParser. Allows passing of a map with term to Boost, and the boost to apply to each term.

      It will, when parse(String query) is called, construct a query like this (assuming the query consists of two terms and you specify the two fields title and body): (title:term1 body:term1) (title:term2 body:term2)

      When setDefaultOperator(AND_OPERATOR) is set, the result will be: +(title:term1 body:term1) +(title:term2 body:term2)

      When you pass a boost (title=>5 body=>10) you can get +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)

      In other words, all the query's terms must appear, but it doesn't matter in what fields they appear.

      Parameters:
      fields - the default field for query terms.
      analyzer - used to find terms in the query text.
      options - parsing options
      Since:
      11.0.0
  • Method Details

    • getWildcardQuery

      protected org.apache.lucene.search.Query getWildcardQuery(String field, String termStr) throws org.apache.lucene.queryparser.classic.ParseException
      Disable wildcard query.
      Overrides:
      getWildcardQuery in class org.apache.lucene.queryparser.classic.MultiFieldQueryParser
      Throws:
      org.apache.lucene.queryparser.classic.ParseException
    • getFuzzyQuery

      protected org.apache.lucene.search.Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws org.apache.lucene.queryparser.classic.ParseException
      Disable fuzzy query.
      Overrides:
      getFuzzyQuery in class org.apache.lucene.queryparser.classic.MultiFieldQueryParser
      Throws:
      org.apache.lucene.queryparser.classic.ParseException
    • newTermQuery

      protected org.apache.lucene.search.Query newTermQuery(org.apache.lucene.index.Term term, float boost)
      Overrides:
      newTermQuery in class org.apache.lucene.util.QueryBuilder
    • parse

      public static org.apache.lucene.search.Query parse(String searchString, ParseOptions options, org.apache.lucene.analysis.Analyzer analyzer) throws org.apache.lucene.queryparser.classic.ParseException
      Invoked by JCMS Lucene search engines to parse query.
      Parameters:
      searchString - the string to search/parse
      options - parsing options
      analyzer - analyzer used to find terms in the query text
      Returns:
      a new Query instance
      Throws:
      org.apache.lucene.queryparser.classic.ParseException - if lucene query was invalid
      Since:
      jcms-10.0.0
    • rewriteSearchString

      public static String rewriteSearchString(String searchString, ParseOptions options)
      Compute the search string to use from the specified options
      Parameters:
      searchString - the string to search
      options - a SearchOptions instance holder of search parameters
      Returns:
      a String modified to be suitable for lucene search.
    • getFieldBoost

      public static float getFieldBoost(ParseOptions.Engine engine, String fieldName)
      Return the boost that should be applied to a field at query time for text search.

      Boost value are being read from property declaration :

      query.lucene.boost.{search-engine-name}.{field-name}: 2.0
      For example :
         query.lucene.boost.PUBLICATION.title: 2.0
         query.lucene.boost.CATEGORY.name: 1.2
         query.lucene.boost.PUBLICATION._allfields_: 1.0
       
      Parameters:
      engine - the search engine in which search is performed
      fieldName - the field name being search and for which a Query object is being built
      Returns:
      a float noting the boost value of the field to apply to the Query object
    • addMultiTermClauses

      protected void addMultiTermClauses(List<org.apache.lucene.search.BooleanClause> clauses, org.apache.lucene.search.Query q)
      Overrides:
      addMultiTermClauses in class org.apache.lucene.queryparser.classic.QueryParserBase
    • getFieldQuery

      protected org.apache.lucene.search.Query getFieldQuery(String field, String queryText, boolean quoted) throws org.apache.lucene.queryparser.classic.ParseException
      Overrides:
      getFieldQuery in class org.apache.lucene.queryparser.classic.MultiFieldQueryParser
      Throws:
      org.apache.lucene.queryparser.classic.ParseException
    • getMultiTermPositionQuery

      protected org.apache.lucene.search.Query getMultiTermPositionQuery(List<org.apache.lucene.search.Query> positionGroups) throws org.apache.lucene.queryparser.classic.ParseException
      Combines the per-position field-disjunction groups above when a single getFieldQuery call is analyzed into more than one term. Unlike getMultiFieldQuery (field alternatives for a single term, always SHOULD), this honors the default operator, since every term position must be satisfied under AND_OPERATOR.
      Throws:
      org.apache.lucene.queryparser.classic.ParseException