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
Ejemplo n.º 1
0
        public virtual void TestWeightedOR()
        {
            IDictionary <string, float> weights = new LinkedHashMap <string, float>();

            weights["field0"] = 5f;
            weights["field1"] = 10f;

            BooleanQuery expected = new BooleanQuery();
            BooleanQuery foo      = new BooleanQuery(true);
            Query        field0   = new TermQuery(new Term("field0", "foo"));

            field0.Boost = (5f);
            foo.Add(field0, Occur.SHOULD);
            Query field1 = new TermQuery(new Term("field1", "foo"));

            field1.Boost = (10f);
            foo.Add(field1, Occur.SHOULD);
            expected.Add(foo, Occur.SHOULD);

            BooleanQuery bar = new BooleanQuery(true);

            field0       = new TermQuery(new Term("field0", "bar"));
            field0.Boost = (5f);
            bar.Add(field0, Occur.SHOULD);
            field1       = new TermQuery(new Term("field1", "bar"));
            field1.Boost = (10f);
            bar.Add(field1, Occur.SHOULD);
            expected.Add(bar, Occur.SHOULD);

            Analyzer          analyzer = new MockAnalyzer(Random);
            SimpleQueryParser parser   = new SimpleQueryParser(analyzer, weights);

            assertEquals(expected, parser.Parse("foo|bar"));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// helper to parse a query with whitespace+lowercase analyzer across "field",
        /// with default operator of MUST
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private Query Parse(string text)
        {
            Analyzer          analyzer = new MockAnalyzer(Random);
            SimpleQueryParser parser   = new SimpleQueryParser(analyzer, "field");

            parser.DefaultOperator = Occur.MUST;
            return(parser.Parse(text));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// helper to parse a query with whitespace+lowercase analyzer across "field",
        /// with default operator of MUST
        /// </summary>
        /// <param name="text"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        private Query Parse(string text, Operator flags)
        {
            Analyzer          analyzer = new MockAnalyzer(Random);
            SimpleQueryParser parser   = new SimpleQueryParser(analyzer, new HashMap <string, float>()
            {
                { "field", 1f }
            }, flags);

            parser.DefaultOperator = Occur.MUST;
            return(parser.Parse(text));
        }
Ejemplo n.º 4
0
        public virtual void TestORImplicit()
        {
            BooleanQuery expected = new BooleanQuery();

            expected.Add(new TermQuery(new Term("field", "foo")), Occur.SHOULD);
            expected.Add(new TermQuery(new Term("field", "bar")), Occur.SHOULD);

            SimpleQueryParser parser = new SimpleQueryParser(new MockAnalyzer(Random), "field");

            assertEquals(expected, parser.Parse("foo bar"));
        }
Ejemplo n.º 5
0
        /** helper to parse a query with keyword analyzer across "field" */
        private Query ParseKeyword(string text, Operator flags)
        {
            Analyzer          analyzer = new MockAnalyzer(Random, MockTokenizer.KEYWORD, false);
            SimpleQueryParser parser   = new SimpleQueryParser(analyzer,
                                                               new HashMap <string, float>()
            {
                { "field", 1f }
            },
                                                               flags);

            return(parser.Parse(text));
        }
Ejemplo n.º 6
0
        public virtual void TestWeightedTerm()
        {
            IDictionary <string, float> weights = new Dictionary <string, float>();

            weights["field0"] = 5f;
            weights["field1"] = 10f;

            BooleanQuery expected = new BooleanQuery(true);
            Query        field0   = new TermQuery(new Term("field0", "foo"));

            field0.Boost = (5f);
            expected.Add(field0, BooleanClause.Occur.SHOULD);
            Query field1 = new TermQuery(new Term("field1", "foo"));

            field1.Boost = (10f);
            expected.Add(field1, BooleanClause.Occur.SHOULD);

            Analyzer          analyzer = new MockAnalyzer(Random());
            SimpleQueryParser parser   = new SimpleQueryParser(analyzer, weights);

            assertEquals(expected, parser.Parse("foo"));
        }
 /** helper to parse a query with keyword analyzer across "field" */
 private Query ParseKeyword(string text, int flags)
 {
     Analyzer analyzer = new MockAnalyzer(Random(), MockTokenizer.KEYWORD, false);
     SimpleQueryParser parser = new SimpleQueryParser(analyzer,
         new HashMap<string, float>() { { "field", 1f } },
         flags);
     return parser.Parse(text);
 }
 /// <summary>
 /// helper to parse a query with whitespace+lowercase analyzer across "field",
 /// with default operator of MUST
 /// </summary>
 /// <param name="text"></param>
 /// <param name="flags"></param>
 /// <returns></returns>
 private Query Parse(string text, int flags)
 {
     Analyzer analyzer = new MockAnalyzer(Random());
     SimpleQueryParser parser = new SimpleQueryParser(analyzer, new HashMap<string, float>() { { "field", 1f } }, flags);
     parser.DefaultOperator = BooleanClause.Occur.MUST;
     return parser.Parse(text);
 }
 /// <summary>
 /// helper to parse a query with whitespace+lowercase analyzer across "field",
 /// with default operator of MUST
 /// </summary>
 /// <param name="text"></param>
 /// <returns></returns>
 private Query Parse(string text)
 {
     Analyzer analyzer = new MockAnalyzer(Random());
     SimpleQueryParser parser = new SimpleQueryParser(analyzer, "field");
     parser.DefaultOperator = BooleanClause.Occur.MUST;
     return parser.Parse(text);
 }
        public virtual void TestWeightedTerm()
        {
            IDictionary<string, float> weights = new Dictionary<string, float>();
            weights["field0"] = 5f;
            weights["field1"] = 10f;

            BooleanQuery expected = new BooleanQuery(true);
            Query field0 = new TermQuery(new Term("field0", "foo"));
            field0.Boost = (5f);
            expected.Add(field0, BooleanClause.Occur.SHOULD);
            Query field1 = new TermQuery(new Term("field1", "foo"));
            field1.Boost = (10f);
            expected.Add(field1, BooleanClause.Occur.SHOULD);

            Analyzer analyzer = new MockAnalyzer(Random());
            SimpleQueryParser parser = new SimpleQueryParser(analyzer, weights);
            assertEquals(expected, parser.Parse("foo"));
        }
        public virtual void TestORImplicit()
        {
            BooleanQuery expected = new BooleanQuery();
            expected.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.SHOULD);
            expected.Add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.SHOULD);

            SimpleQueryParser parser = new SimpleQueryParser(new MockAnalyzer(Random()), "field");
            assertEquals(expected, parser.Parse("foo bar"));
        }