Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class SimpleQueryParser

    SimpleQueryParser is used to parse human readable query syntax.

    The main idea behind this parser is that a person should be able to type whatever they want to represent a query, and this parser will do its best to interpret what to search for no matter how poorly composed the request may be. Tokens are considered to be any of a term, phrase, or subquery for the operations described below. Whitespace including ' ' '\n' '\r' and '\t' and certain operators may be used to delimit tokens ( ) + | " .

    Any errors in query syntax will be ignored and the parser will attempt to decipher what it can; however, this may mean odd or unexpected results.

    Query Operators

    • '+' specifies AND operation: token1+token2
    • '|' specifies OR operation: token1|token2
    • '-' negates a single token: -token0
    • '"' creates phrases of terms: "term1 term2 ..."
    • '*' at the end of terms specifies prefix query: term*
    • '~N' at the end of terms specifies fuzzy query: term~1
    • '~N' at the end of phrases specifies near query: "term1 term2"~5
    • '(' and ')' specifies precedence: token1 + (token2 | token3)

    The default operator is OR if no other operator is specified. For example, the following will ORtoken1 and token2 together: token1 token2

    Normal operator precedence will be simple order from right to left. For example, the following will evaluate token1 OR token2 first, then AND with token3:
    token1 | token2 + token3

    Escaping

    An individual term may contain any possible character with certain characters requiring escaping using a '\'. The following characters will need to be escaped in terms and phrases: + | " ( ) ' \

    The '-' operator is a special case. On individual terms (not phrases) the first character of a term that is - must be escaped; however, any '-' characters beyond the first character do not need to be escaped. For example:
    • -term1 -- Specifies NOT operation against term1
    • \-term1 -- Searches for the term -term1.
    • term-1 -- Searches for the term term-1.
    • term\-1 -- Searches for the term term-1.

    The '*' operator is a special case. On individual terms (not phrases) the last character of a term that is '*' must be escaped; however, any '*' characters before the last character do not need to be escaped:
    • term1* -- Searches for the prefix term1
    • term1\* -- Searches for the term term1*
    • term*1 -- Searches for the term term*1
    • term\*1 -- Searches for the term term*1

    Note that above examples consider the terms before text processing.
    Inheritance
    object
    QueryBuilder
    SimpleQueryParser
    Inherited Members
    QueryBuilder.CreateBooleanQuery(string, string)
    QueryBuilder.CreateBooleanQuery(string, string, Occur)
    QueryBuilder.CreatePhraseQuery(string, string)
    QueryBuilder.CreatePhraseQuery(string, string, int)
    QueryBuilder.CreateMinShouldMatchQuery(string, string, float)
    QueryBuilder.Analyzer
    QueryBuilder.EnablePositionIncrements
    QueryBuilder.CreateFieldQuery(Analyzer, Occur, string, string, bool, int)
    QueryBuilder.NewBooleanQuery(bool)
    QueryBuilder.NewTermQuery(Term)
    QueryBuilder.NewPhraseQuery()
    QueryBuilder.NewMultiPhraseQuery()
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Lucene.Net.QueryParsers.Simple
    Assembly: Lucene.Net.QueryParser.dll
    Syntax
    public class SimpleQueryParser : QueryBuilder

    Constructors

    SimpleQueryParser(Analyzer, IDictionary<string, float>)

    Creates a new parser searching over multiple fields with different weights.

    Declaration
    public SimpleQueryParser(Analyzer analyzer, IDictionary<string, float> weights)
    Parameters
    Type Name Description
    Analyzer analyzer
    IDictionary<string, float> weights

    SimpleQueryParser(Analyzer, IDictionary<string, float>, Operator)

    Creates a new parser with custom flags used to enable/disable certain features.

    Declaration
    public SimpleQueryParser(Analyzer analyzer, IDictionary<string, float> weights, Operator flags)
    Parameters
    Type Name Description
    Analyzer analyzer
    IDictionary<string, float> weights
    Operator flags

    SimpleQueryParser(Analyzer, string)

    Creates a new parser searching over a single field.

    Declaration
    public SimpleQueryParser(Analyzer analyzer, string field)
    Parameters
    Type Name Description
    Analyzer analyzer
    string field

    Fields

    m_flags

    flags to the parser (to turn features on/off)

    Declaration
    protected readonly Operator m_flags
    Field Value
    Type Description
    Operator

    m_weights

    Map of fields to query against with their weights

    Declaration
    protected readonly IDictionary<string, float> m_weights
    Field Value
    Type Description
    IDictionary<string, float>

    Properties

    DefaultOperator

    Gets or Sets the implicit operator setting, which will be either Lucene.Net.Search.Occur.SHOULD or Lucene.Net.Search.Occur.MUST.

    Declaration
    public virtual Occur DefaultOperator { get; set; }
    Property Value
    Type Description
    Occur

    Methods

    NewDefaultQuery(string)

    Factory method to generate a standard query (no phrase or prefix operators).

    Declaration
    protected virtual Query NewDefaultQuery(string text)
    Parameters
    Type Name Description
    string text
    Returns
    Type Description
    Query

    NewFuzzyQuery(string, int)

    Factory method to generate a fuzzy query.

    Declaration
    protected virtual Query NewFuzzyQuery(string text, int fuzziness)
    Parameters
    Type Name Description
    string text
    int fuzziness
    Returns
    Type Description
    Query

    NewPhraseQuery(string, int)

    Factory method to generate a phrase query with slop.

    Declaration
    protected virtual Query NewPhraseQuery(string text, int slop)
    Parameters
    Type Name Description
    string text
    int slop
    Returns
    Type Description
    Query

    NewPrefixQuery(string)

    Factory method to generate a prefix query.

    Declaration
    protected virtual Query NewPrefixQuery(string text)
    Parameters
    Type Name Description
    string text
    Returns
    Type Description
    Query

    Parse(string)

    Parses the query text and returns parsed query (or null if empty)

    Declaration
    public Query Parse(string queryText)
    Parameters
    Type Name Description
    string queryText
    Returns
    Type Description
    Query

    Simplify(BooleanQuery)

    Helper to simplify boolean queries with 0 or 1 clause

    Declaration
    protected virtual Query Simplify(BooleanQuery bq)
    Parameters
    Type Name Description
    BooleanQuery bq
    Returns
    Type Description
    Query
    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.