public virtual void TestSynonyms() { BooleanQuery expected = new BooleanQuery(true); expected.Add(new TermQuery(new Index.Term("field", "dogs")), Occur.SHOULD); expected.Add(new TermQuery(new Index.Term("field", "dog")), Occur.SHOULD); QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockSynonymAnalyzer()); assertEquals(expected, qp.Parse("dogs")); assertEquals(expected, qp.Parse("\"dogs\"")); qp.DefaultOperator = (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 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); }
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()); }
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 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()); }
/// <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 = {"query1", "query2", "query3"}; /// string[] fields = {"filename", "contents", "description"}; /// 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); }