C# Class Lucene.Net.QueryParsers.Simple.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 OR token1 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: Lucene.Net.Util.QueryBuilder
显示文件 Open project: apache/lucenenet Class Usage Examples

Protected Properties

Property Type Description
flags Operator
weights float>.IDictionary

Public Methods

Method Description
Parse ( string queryText ) : Query

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

SimpleQueryParser ( Analyzer analyzer, float>.IDictionary weights ) : Lucene.Net.Analysis

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

SimpleQueryParser ( Analyzer analyzer, float>.IDictionary weights, Operator flags ) : Lucene.Net.Analysis

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

SimpleQueryParser ( Analyzer analyzer, string field ) : Lucene.Net.Analysis

Creates a new parser searching over a single field.

Protected Methods

Method Description
NewDefaultQuery ( string text ) : Query

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

NewFuzzyQuery ( string text, int fuzziness ) : Query

Factory method to generate a fuzzy query.

NewPhraseQuery ( string text, int slop ) : Query

Factory method to generate a phrase query with slop.

NewPrefixQuery ( string text ) : Query

Factory method to generate a prefix query.

Simplify ( Lucene.Net.Search.BooleanQuery bq ) : Query

Helper to simplify boolean queries with 0 or 1 clause

Private Methods

Method Description
BuildQueryTree ( State state, Query branch ) : void

buildQueryTree should be called after a term, phrase, or subquery is consumed to be added to our existing query tree this method will only add to the existing tree if the branch contained in state is not null

ConsumePhrase ( State state ) : void
ConsumeSubQuery ( State state ) : void
ConsumeToken ( State state ) : void
ParseFuzziness ( State state ) : int

Helper parsing fuzziness from parsing state

ParseSubQuery ( State state ) : void
TokenFinished ( State state ) : bool

Helper returning true if the state has reached the end of token.

Method Details

NewDefaultQuery() protected method

Factory method to generate a standard query (no phrase or prefix operators).
protected NewDefaultQuery ( string text ) : Query
text string
return Lucene.Net.Search.Query

NewFuzzyQuery() protected method

Factory method to generate a fuzzy query.
protected NewFuzzyQuery ( string text, int fuzziness ) : Query
text string
fuzziness int
return Lucene.Net.Search.Query

NewPhraseQuery() protected method

Factory method to generate a phrase query with slop.
protected NewPhraseQuery ( string text, int slop ) : Query
text string
slop int
return Lucene.Net.Search.Query

NewPrefixQuery() protected method

Factory method to generate a prefix query.
protected NewPrefixQuery ( string text ) : Query
text string
return Lucene.Net.Search.Query

Parse() public method

Parses the query text and returns parsed query (or null if empty)
public Parse ( string queryText ) : Query
queryText string
return Lucene.Net.Search.Query

SimpleQueryParser() public method

Creates a new parser searching over multiple fields with different weights.
public SimpleQueryParser ( Analyzer analyzer, float>.IDictionary weights ) : Lucene.Net.Analysis
analyzer Lucene.Net.Analysis.Analyzer
weights float>.IDictionary
return Lucene.Net.Analysis

SimpleQueryParser() public method

Creates a new parser with custom flags used to enable/disable certain features.
public SimpleQueryParser ( Analyzer analyzer, float>.IDictionary weights, Operator flags ) : Lucene.Net.Analysis
analyzer Lucene.Net.Analysis.Analyzer
weights float>.IDictionary
flags Operator
return Lucene.Net.Analysis

SimpleQueryParser() public method

Creates a new parser searching over a single field.
public SimpleQueryParser ( Analyzer analyzer, string field ) : Lucene.Net.Analysis
analyzer Lucene.Net.Analysis.Analyzer
field string
return Lucene.Net.Analysis

Simplify() protected method

Helper to simplify boolean queries with 0 or 1 clause
protected Simplify ( Lucene.Net.Search.BooleanQuery bq ) : Query
bq Lucene.Net.Search.BooleanQuery
return Lucene.Net.Search.Query

Property Details

flags protected_oe property

flags to the parser (to turn features on/off)
protected Operator flags
return Operator

weights protected_oe property

Map of fields to query against with their weights
protected IDictionary weights
return float>.IDictionary