コード例 #1
0
        public void TestSimple()
        {
            assertQueryEquals("term term term", null, "term term term");
            assertQueryEquals("türm term term", null, "türm term term");
            assertQueryEquals("ümlaut", null, "ümlaut");

            assertQueryEquals("a AND b", null, "+a +b");
            assertQueryEquals("(a AND b)", null, "+a +b");
            assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
            assertQueryEquals("a AND NOT b", null, "+a -b");
            assertQueryEquals("a AND -b", null, "+a -b");
            assertQueryEquals("a AND !b", null, "+a -b");
            assertQueryEquals("a && b", null, "+a +b");
            assertQueryEquals("a && ! b", null, "+a -b");

            assertQueryEquals("a OR b", null, "a b");
            assertQueryEquals("a || b", null, "a b");

            assertQueryEquals("+term -term term", null, "+term -term term");
            assertQueryEquals("foo:term AND field:anotherTerm", null,
                              "+foo:term +anotherterm");
            assertQueryEquals("term AND \"phrase phrase\"", null,
                              "+term +\"phrase phrase\"");
            assertQueryEquals("\"hello there\"", null, "\"hello there\"");
            assertTrue(GetQuery("a AND b", null) is BooleanQuery);
            assertTrue(GetQuery("hello", null) is TermQuery);
            assertTrue(GetQuery("\"hello there\"", null) is PhraseQuery);

            assertQueryEquals("germ term^2.0", null, "germ term^2.0");
            assertQueryEquals("(term)^2.0", null, "term^2.0");
            assertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
            assertQueryEquals("term^2.0", null, "term^2.0");
            assertQueryEquals("term^2", null, "term^2.0");
            assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");
            assertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");

            assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,
                              "+(foo bar) +(baz boo)");
            assertQueryEquals("((a OR b) AND NOT c) OR d", null, "(+(a b) -c) d");
            assertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null,
                              "+(apple \"steve jobs\") -(foo bar baz)");
            assertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null,
                              "+(title:dog title:cat) -author:\"bob dole\"");

            PrecedenceQueryParser qp = new PrecedenceQueryParser();

            qp.Analyzer = (new MockAnalyzer(Random));
            // make sure OR is the default:
            assertEquals(StandardQueryConfigHandler.Operator.OR, qp.DefaultOperator);
            qp.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
            assertEquals(StandardQueryConfigHandler.Operator.AND, qp.DefaultOperator);
            qp.DefaultOperator = (StandardQueryConfigHandler.Operator.OR);
            assertEquals(StandardQueryConfigHandler.Operator.OR, qp.DefaultOperator);

            assertQueryEquals("a OR !b", null, "a -b");
            assertQueryEquals("a OR ! b", null, "a -b");
            assertQueryEquals("a OR -b", null, "a -b");
        }
コード例 #2
0
        public void assertQueryEquals(PrecedenceQueryParser qp, String field, String query,
                                      String result)
        {
            Query  q = qp.Parse(query, field);
            String s = q.ToString(field);

            if (!s.Equals(result, StringComparison.Ordinal))
            {
                fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
                     + "/");
            }
        }
コード例 #3
0
        public Query getQueryDOA(String query, Analyzer a)
        {
            if (a == null)
            {
                a = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
            }
            PrecedenceQueryParser qp = new PrecedenceQueryParser();

            qp.Analyzer        = (a);
            qp.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
            return(qp.Parse(query, "field"));
        }
コード例 #4
0
        public void assertWildcardQueryEquals(String query, String result)
        {
            PrecedenceQueryParser qp = GetParser(null);
            Query  q = qp.Parse(query, "field");
            String s = q.ToString("field");

            if (!s.Equals(result, StringComparison.Ordinal))
            {
                fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"
                     + result + "/");
            }
        }
コード例 #5
0
        public PrecedenceQueryParser GetParser(Analyzer a)
        {
            if (a == null)
            {
                a = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
            }
            PrecedenceQueryParser qp = new PrecedenceQueryParser();

            qp.Analyzer        = (a);
            qp.DefaultOperator = (StandardQueryConfigHandler.Operator.OR);
            return(qp);
        }
コード例 #6
0
        public PrecedenceQueryParser GetParser(Analyzer a)
        {
            if (a == null)
            {
                a = new MockAnalyzer(Random(), MockTokenizer.SIMPLE, true);
            }
            PrecedenceQueryParser qp = new PrecedenceQueryParser();

            qp.Analyzer        = (a);
            qp.DefaultOperator = (StandardQueryConfigHandler.Operator.OR); // LUCENENET TODO: Change API back to the way it was..?
            return(qp);
        }
コード例 #7
0
 public void assertDateRangeQueryEquals(PrecedenceQueryParser qp, String field,
                                        String startDate, String endDate, DateTime endDateInclusive,
                                        DateTools.Resolution resolution)
 {
     assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate)
                       + " TO " + escapeDateString(endDate) + "]", "["
                       + getDate(startDate, resolution) + " TO "
                       + getDate(endDateInclusive, resolution) + "]");
     assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate)
                       + " TO " + escapeDateString(endDate) + "}", "{"
                       + getDate(startDate, resolution) + " TO "
                       + getDate(endDate, resolution) + "}");
 }
コード例 #8
0
        public void TestPrecedence()
        {
            PrecedenceQueryParser parser = GetParser(new MockAnalyzer(Random, MockTokenizer.WHITESPACE, false));
            Query query1 = parser.Parse("A AND B OR C AND D", "field");
            Query query2 = parser.Parse("(A AND B) OR (C AND D)", "field");

            assertEquals(query1, query2);

            query1 = parser.Parse("A OR B C", "field");
            query2 = parser.Parse("(A B) C", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A AND B C", "field");
            query2 = parser.Parse("(+A +B) C", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A AND NOT B", "field");
            query2 = parser.Parse("+A -B", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A OR NOT B", "field");
            query2 = parser.Parse("A -B", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A OR NOT B AND C", "field");
            query2 = parser.Parse("A (-B +C)", "field");
            assertEquals(query1, query2);

            parser.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
            query1 = parser.Parse("A AND B OR C AND D", "field");
            query2 = parser.Parse("(A AND B) OR (C AND D)", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A AND B C", "field");
            query2 = parser.Parse("(A B) C", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A AND B C", "field");
            query2 = parser.Parse("(+A +B) C", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A AND NOT B", "field");
            query2 = parser.Parse("+A -B", "field");
            assertEquals(query1, query2);

            query1 = parser.Parse("A AND NOT B OR C", "field");
            query2 = parser.Parse("(+A -B) OR C", "field");
            assertEquals(query1, query2);
        }
コード例 #9
0
        public void assertWildcardQueryEquals(String query, bool lowercase,
                                              String result)
        {
            PrecedenceQueryParser qp = GetParser(null);

            qp.LowercaseExpandedTerms = (lowercase);
            Query  q = qp.Parse(query, "field");
            String s = q.ToString("field");

            if (!s.equals(result))
            {
                fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"
                     + result + "/");
            }
        }
コード例 #10
0
        public void TestDateRange()
        {
            String startDate = getLocalizedDate(2002, 1, 1, false);
            String endDate   = getLocalizedDate(2002, 1, 4, false);
            // we use the default Locale/TZ since LuceneTestCase randomizes it
            //Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
            //endDateExpected.set(2002, 1, 4, 23, 59, 59);
            //endDateExpected.set(Calendar.MILLISECOND, 999);
            DateTime endDateExpected = new DateTime(2002, 1, 4, 23, 59, 59, 999, new GregorianCalendar());


            String defaultField      = "default";
            String monthField        = "month";
            String hourField         = "hour";
            PrecedenceQueryParser qp = new PrecedenceQueryParser(new MockAnalyzer(Random()));

            // LUCENENET TODO: Can we eliminate this nullable??
            IDictionary <string, DateTools.Resolution?> fieldMap = new HashMap <string, DateTools.Resolution?>();

            // set a field specific date resolution
            fieldMap.Put(monthField, DateTools.Resolution.MONTH);
#pragma warning disable 612, 618
            qp.SetDateResolution(fieldMap);
#pragma warning restore 612, 618

            // set default date resolution to MILLISECOND
            qp.SetDateResolution(DateTools.Resolution.MILLISECOND);

            // set second field specific date resolution
            fieldMap.Put(hourField, DateTools.Resolution.HOUR);
#pragma warning disable 612, 618
            qp.SetDateResolution(fieldMap);
#pragma warning restore 612, 618

            // for this field no field specific date resolution has been set,
            // so verify if the default resolution is used
            assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
                                       endDateExpected, DateTools.Resolution.MILLISECOND);

            // verify if field specific date resolutions are used for these two fields
            assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
                                       endDateExpected, DateTools.Resolution.MONTH);

            assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
                                       endDateExpected, DateTools.Resolution.HOUR);
        }
コード例 #11
0
        public void TestBoost()
        {
            CharacterRunAutomaton stopSet = new CharacterRunAutomaton(BasicAutomata.MakeString("on"));
            Analyzer oneStopAnalyzer      = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true, stopSet);

            PrecedenceQueryParser qp = new PrecedenceQueryParser();

            qp.Analyzer = (oneStopAnalyzer);
            Query q = qp.Parse("on^1.0", "field");

            assertNotNull(q);
            q = qp.Parse("\"hello\"^2.0", "field");
            assertNotNull(q);
            assertEquals(q.Boost, (float)2.0, (float)0.5);
            q = qp.Parse("hello^2.0", "field");
            assertNotNull(q);
            assertEquals(q.Boost, (float)2.0, (float)0.5);
            q = qp.Parse("\"on\"^1.0", "field");
            assertNotNull(q);

            q = GetParser(new MockAnalyzer(Random, MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET)).Parse("the^3",
                                                                                                                       "field");
            assertNotNull(q);
        }