This class is generated by JavaCC. The most important method is QueryParserBase.Parse(string). The syntax for query strings is as follows: A Query is a series of clauses. A clause may be prefixed by: a plus (+) or a minus (-) sign, indicating that the clause is required or prohibited respectively; or a term followed by a colon, indicating the field to be searched. This enables one to construct queries which search multiple fields. A clause may be either: a term, indicating all the documents that contain this term; or a nested query, enclosed in parentheses. Note that this may be used with a +/- prefix to require any of a set of terms. Thus, in BNF, the query grammar is: Query ::= ( Clause )* Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )

Examples of appropriately formatted queries can be found in the query syntax documentation.

In TermRangeQuerys, QueryParser tries to detect date values, e.g. date:[6/1/2005 TO 6/4/2005] produces a range query that searches for "date" fields between 2005-06-01 and 2005-06-04. Note that the format of the accepted input depends on the Locale. A Documents.DateTools.Resolution has to be set, if you want to use Documents.DateTools for date conversion.

The date resolution that shall be used for RangeQueries can be set using QueryParserBase.SetDateResolution(Documents.DateTools.Resolution) or QueryParserBase.SetDateResolution(string, Documents.DateTools.Resolution). The former sets the default date resolution for all fields, whereas the latter can be used to set field specific date resolutions. Field specific date resolutions take, if set, precedence over the default date resolution.

If you don't use Documents.DateTools in your index, you can create your own query parser that inherits QueryParser and overwrites QueryParserBase.GetRangeQuery(string, string, string, bool, bool) to use a different method for date conversion.

Note that QueryParser is not thread-safe.

NOTE: there is a new QueryParser in contrib, which matches the same syntax as this class, but is more modular, enabling substantial customization to how a query is created.

NOTE: You must specify the required LuceneVersion compatibility when creating QueryParser: As of 3.1, QueryParserBase.AutoGeneratePhraseQueries is false by default.
Inheritance: Lucene.Net.QueryParsers.Classic.QueryParserBase
 public virtual QueryParser GetParser(Analyzer a)
 {
     if (a == null) a = new MockAnalyzer(Random(), MockTokenizer.SIMPLE, true);
     QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, DefaultField, a);
     qp.DefaultOperator = (QueryParserBase.OR_OPERATOR);
     return qp;
 }
示例#2
0
        protected CoreParser(string defaultField, Analyzer analyzer, QueryParser parser)
        {
            this.analyzer = analyzer;
            this.parser = parser;
            filterFactory = new FilterBuilderFactory();
            filterFactory.AddBuilder("RangeFilter", new RangeFilterBuilder());
            filterFactory.AddBuilder("NumericRangeFilter", new NumericRangeFilterBuilder());

            queryFactory = new QueryBuilderFactory();
            queryFactory.AddBuilder("TermQuery", new TermQueryBuilder());
            queryFactory.AddBuilder("TermsQuery", new TermsQueryBuilder(analyzer));
            queryFactory.AddBuilder("MatchAllDocsQuery", new MatchAllDocsQueryBuilder());
            queryFactory.AddBuilder("BooleanQuery", new BooleanQueryBuilder(queryFactory));
            queryFactory.AddBuilder("NumericRangeQuery", new NumericRangeQueryBuilder());
            queryFactory.AddBuilder("DisjunctionMaxQuery", new DisjunctionMaxQueryBuilder(queryFactory));
            if (parser != null)
            {
                queryFactory.AddBuilder("UserQuery", new UserInputQueryBuilder(parser));
            }
            else
            {
                queryFactory.AddBuilder("UserQuery", new UserInputQueryBuilder(defaultField, analyzer));
            }
            queryFactory.AddBuilder("FilteredQuery", new FilteredQueryBuilder(filterFactory, queryFactory));
            queryFactory.AddBuilder("ConstantScoreQuery", new ConstantScoreQueryBuilder(filterFactory));

            filterFactory.AddBuilder("CachedFilter", new CachedFilterBuilder(queryFactory,
                filterFactory, maxNumCachedFilters));

            SpanQueryBuilderFactory sqof = new SpanQueryBuilderFactory();

            SpanNearBuilder snb = new SpanNearBuilder(sqof);
            sqof.AddBuilder("SpanNear", snb);
            queryFactory.AddBuilder("SpanNear", snb);

            BoostingTermBuilder btb = new BoostingTermBuilder();
            sqof.AddBuilder("BoostingTermQuery", btb);
            queryFactory.AddBuilder("BoostingTermQuery", btb);

            SpanTermBuilder snt = new SpanTermBuilder();
            sqof.AddBuilder("SpanTerm", snt);
            queryFactory.AddBuilder("SpanTerm", snt);

            SpanOrBuilder sot = new SpanOrBuilder(sqof);
            sqof.AddBuilder("SpanOr", sot);
            queryFactory.AddBuilder("SpanOr", sot);

            SpanOrTermsBuilder sots = new SpanOrTermsBuilder(analyzer);
            sqof.AddBuilder("SpanOrTerms", sots);
            queryFactory.AddBuilder("SpanOrTerms", sots);

            SpanFirstBuilder sft = new SpanFirstBuilder(sqof);
            sqof.AddBuilder("SpanFirst", sft);
            queryFactory.AddBuilder("SpanFirst", sft);

            SpanNotBuilder snot = new SpanNotBuilder(sqof);
            sqof.AddBuilder("SpanNot", snot);
            queryFactory.AddBuilder("SpanNot", snot);
        }
        public virtual void TestMultiAnalyzer()
        {

            QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "", new MultiAnalyzer());

            // trivial, no multiple tokens:
            assertEquals("foo", qp.Parse("foo").toString());
            assertEquals("foo", qp.Parse("\"foo\"").toString());
            assertEquals("foo foobar", qp.Parse("foo foobar").toString());
            assertEquals("\"foo foobar\"", qp.Parse("\"foo foobar\"").toString());
            assertEquals("\"foo foobar blah\"", qp.Parse("\"foo foobar blah\"").toString());

            // two tokens at the same position:
            assertEquals("(multi multi2) foo", qp.Parse("multi foo").toString());
            assertEquals("foo (multi multi2)", qp.Parse("foo multi").toString());
            assertEquals("(multi multi2) (multi multi2)", qp.Parse("multi multi").toString());
            assertEquals("+(foo (multi multi2)) +(bar (multi multi2))",
                qp.Parse("+(foo multi) +(bar multi)").toString());
            assertEquals("+(foo (multi multi2)) field:\"bar (multi multi2)\"",
                qp.Parse("+(foo multi) field:\"bar multi\"").toString());

            // phrases:
            assertEquals("\"(multi multi2) foo\"", qp.Parse("\"multi foo\"").toString());
            assertEquals("\"foo (multi multi2)\"", qp.Parse("\"foo multi\"").toString());
            assertEquals("\"foo (multi multi2) foobar (multi multi2)\"",
                qp.Parse("\"foo multi foobar multi\"").toString());

            // fields:
            assertEquals("(field:multi field:multi2) field:foo", qp.Parse("field:multi field:foo").toString());
            assertEquals("field:\"(multi multi2) foo\"", qp.Parse("field:\"multi foo\"").toString());

            // three tokens at one position:
            assertEquals("triplemulti multi3 multi2", qp.Parse("triplemulti").toString());
            assertEquals("foo (triplemulti multi3 multi2) foobar",
                qp.Parse("foo triplemulti foobar").toString());

            // phrase with non-default slop:
            assertEquals("\"(multi multi2) foo\"~10", qp.Parse("\"multi foo\"~10").toString());

            // phrase with non-default boost:
            assertEquals("\"(multi multi2) foo\"^2.0", qp.Parse("\"multi foo\"^2").toString());

            // phrase after changing default slop
            qp.PhraseSlop=(99);
            assertEquals("\"(multi multi2) foo\"~99 bar",
                         qp.Parse("\"multi foo\" bar").toString());
            assertEquals("\"(multi multi2) foo\"~99 \"foo bar\"~2",
                         qp.Parse("\"multi foo\" \"foo bar\"~2").toString());
            qp.PhraseSlop=(0);

            // non-default operator:
            qp.DefaultOperator=(QueryParserBase.AND_OPERATOR);
            assertEquals("+(multi multi2) +foo", qp.Parse("multi foo").toString());

        }
        public virtual void TestPosIncrementAnalyzer()
        {
#pragma warning disable 612, 618
            QueryParser qp = new QueryParser(LuceneVersion.LUCENE_40, "", new PosIncrementAnalyzer());
#pragma warning restore 612, 618
            assertEquals("quick brown", qp.Parse("the quick brown").toString());
            assertEquals("quick brown fox", qp.Parse("the quick brown fox").toString());
        }
 /// <summary> 
 /// Parses a query, searching on the fields specified. Use this if you need
 /// to specify certain fields as required, and others as prohibited.
 /// <p/>
 /// Usage:
 /// <code>
 /// string[] query = {&quot;query1&quot;, &quot;query2&quot;, &quot;query3&quot;};
 /// string[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
 /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
 /// BooleanClause.Occur.MUST,
 /// BooleanClause.Occur.MUST_NOT};
 /// MultiFieldQueryParser.parse(query, fields, flags, analyzer);
 /// </code>
 /// <p/>
 /// The code above would construct a query:
 /// 
 /// <code>
 /// (filename:query1) +(contents:query2) -(description:query3)
 /// </code>
 /// 
 /// </summary>
 /// <param name="matchVersion">Lucene version to match; this is passed through to
 /// QueryParser.
 /// </param>
 /// <param name="queries">Queries string to parse
 /// </param>
 /// <param name="fields">Fields to search on
 /// </param>
 /// <param name="flags">Flags describing the fields
 /// </param>
 /// <param name="analyzer">Analyzer to use
 /// </param>
 /// <throws>  ParseException </throws>
 /// <summary>             if query parsing fails
 /// </summary>
 /// <throws>  IllegalArgumentException </throws>
 /// <summary>             if the length of the queries, fields, and flags array differ
 /// </summary>
 public static Query Parse(LuceneVersion matchVersion, string[] queries, string[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
 {
     if (!(queries.Length == fields.Length && queries.Length == flags.Length))
         throw new System.ArgumentException("queries, fields, and flags array have have different length");
     BooleanQuery bQuery = new BooleanQuery();
     for (int i = 0; i < fields.Length; i++)
     {
         QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
         Query q = qp.Parse(queries[i]);
         if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Count > 0))
         {
             bQuery.Add(q, flags[i]);
         }
     }
     return bQuery;
 }
 public virtual void TestCJKSynonymsPhrase()
 {
     MultiPhraseQuery expected = new MultiPhraseQuery();
     expected.Add(new Index.Term("field", "中"));
     expected.Add(new Index.Term[] { new Index.Term("field", "国"), new Index.Term("field", "國") });
     QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockCJKSynonymAnalyzer());
     qp.DefaultOperator = (QueryParserBase.Operator.AND);
     assertEquals(expected, qp.Parse("\"中国\""));
     expected.Boost = (2.0f);
     assertEquals(expected, qp.Parse("\"中国\"^2"));
     expected.Slop = (3);
     assertEquals(expected, qp.Parse("\"中国\"~3^2"));
 }
 public virtual void TestCJKSynonymsAND2()
 {
     BooleanQuery expected = new BooleanQuery();
     expected.Add(new TermQuery(new Index.Term("field", "中")), BooleanClause.Occur.MUST);
     BooleanQuery inner = new BooleanQuery(true);
     inner.Add(new TermQuery(new Index.Term("field", "国")), BooleanClause.Occur.SHOULD);
     inner.Add(new TermQuery(new Index.Term("field", "國")), BooleanClause.Occur.SHOULD);
     expected.Add(inner, BooleanClause.Occur.MUST);
     BooleanQuery inner2 = new BooleanQuery(true);
     inner2.Add(new TermQuery(new Index.Term("field", "国")), BooleanClause.Occur.SHOULD);
     inner2.Add(new TermQuery(new Index.Term("field", "國")), BooleanClause.Occur.SHOULD);
     expected.Add(inner2, BooleanClause.Occur.MUST);
     QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockCJKSynonymAnalyzer());
     qp.DefaultOperator = (QueryParserBase.Operator.AND);
     assertEquals(expected, qp.Parse("中国国"));
     expected.Boost = (2.0f);
     assertEquals(expected, qp.Parse("中国国^2"));
 }
 public virtual void TestSynonyms()
 {
     BooleanQuery expected = new BooleanQuery(true);
     expected.Add(new TermQuery(new Index.Term("field", "dogs")), BooleanClause.Occur.SHOULD);
     expected.Add(new TermQuery(new Index.Term("field", "dog")), BooleanClause.Occur.SHOULD);
     QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockSynonymAnalyzer());
     assertEquals(expected, qp.Parse("dogs"));
     assertEquals(expected, qp.Parse("\"dogs\""));
     qp.DefaultOperator = (QueryParserBase.Operator.AND);
     assertEquals(expected, qp.Parse("dogs"));
     assertEquals(expected, qp.Parse("\"dogs\""));
     expected.Boost = (2.0f);
     assertEquals(expected, qp.Parse("dogs^2"));
     assertEquals(expected, qp.Parse("\"dogs\"^2"));
 }
        public override void TestNewFieldQuery()
        {
            /** ordinary behavior, synonyms form uncoordinated boolean query */
            QueryParser dumb = new QueryParser(TEST_VERSION_CURRENT, "field",
                new Analyzer1());
            BooleanQuery expanded = new BooleanQuery(true);
            expanded.Add(new TermQuery(new Index.Term("field", "dogs")),
                BooleanClause.Occur.SHOULD);
            expanded.Add(new TermQuery(new Index.Term("field", "dog")),
                BooleanClause.Occur.SHOULD);
            assertEquals(expanded, dumb.Parse("\"dogs\""));
            /** even with the phrase operator the behavior is the same */
            assertEquals(expanded, dumb.Parse("dogs"));

            /**
             * custom behavior, the synonyms are expanded, unless you use quote operator
             */
            QueryParser smart = new SmartQueryParser();
            assertEquals(expanded, smart.Parse("dogs"));

            Query unexpanded = new TermQuery(new Index.Term("field", "dogs"));
            assertEquals(unexpanded, smart.Parse("\"dogs\""));
        }
        public virtual void TestMultiPhraseQueryParsing()
        {
            TokenAndPos[] INCR_0_QUERY_TOKENS_AND = new TokenAndPos[]
            {
                new TokenAndPos("a", 0),
                new TokenAndPos("1", 0),
                new TokenAndPos("b", 1),
                new TokenAndPos("1", 1),
                new TokenAndPos("c", 2)
            };

            QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new CannedAnalyzer(INCR_0_QUERY_TOKENS_AND));
            Query q = qp.Parse("\"this text is acually ignored\"");
            assertTrue("wrong query type!", q is MultiPhraseQuery);

            MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery();
            multiPhraseQuery.Add(new Term[] { new Term("field", "a"), new Term("field", "1") }, -1);
            multiPhraseQuery.Add(new Term[] { new Term("field", "b"), new Term("field", "1") }, 0);
            multiPhraseQuery.Add(new Term[] { new Term("field", "c") }, 1);

            assertEquals(multiPhraseQuery, q);
        }
示例#11
0
 /// <summary>
 /// This constructor has the disadvantage of not being able to change choice of default field name
 /// </summary>
 /// <param name="parser">thread un-safe query parser</param>
 public UserInputQueryBuilder(QueryParser parser)
 {
     this.unSafeParser = parser;
 }
示例#12
0
 /// <summary>
 /// Construct an XML parser that uses a single instance <see cref="QueryParser"/> for handling
 /// UserQuery tags - all parse operations are synchronised on this parser
 /// </summary>
 /// <param name="analyzer"></param>
 /// <param name="parser">A <see cref="QueryParser"/> which will be synchronized on during parse calls.</param>
 public CoreParser(Analyzer analyzer, QueryParser parser)
     : this(null, analyzer, parser)
 {
 }