Ejemplo n.º 1
0
 public void testLeadingWildcardType() throws Exception {
   QueryParserWrapper qp = getParser(null);
   qp.setAllowLeadingWildcard(true);
   assertEquals(WildcardQuery.class, qp.parse("t*erm*").getClass());
   assertEquals(WildcardQuery.class, qp.parse("?term*").getClass());
   assertEquals(WildcardQuery.class, qp.parse("*term*").getClass());
 }
Ejemplo n.º 2
0
  public void testPositionIncrement() throws Exception {
    boolean dflt = StopFilter.getEnablePositionIncrementsDefault();
    StopFilter.setEnablePositionIncrementsDefault(true);
    try {
      QueryParserWrapper qp =
          new QueryParserWrapper("a", new StopAnalyzer(new String[] {"the", "in", "are", "this"}));
      qp.setEnablePositionIncrements(true);
      String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
      // 0 2 5 7 8
      int expectedPositions[] = {1, 3, 4, 6, 9};
      PhraseQuery pq = (PhraseQuery) qp.parse(qtxt);
      // System.out.println("Query text: "+qtxt);
      // System.out.println("Result: "+pq);
      Term t[] = pq.getTerms();
      int pos[] = pq.getPositions();
      for (int i = 0; i < t.length; i++) {
        // System.out.println(i+". "+t[i]+"  pos: "+pos[i]);
        assertEquals(
            "term " + i + " = " + t[i] + " has wrong term-position!", expectedPositions[i], pos[i]);
      }

    } finally {
      StopFilter.setEnablePositionIncrementsDefault(dflt);
    }
  }
Ejemplo n.º 3
0
  /** This test differs from TestPrecedenceQueryParser */
  public void testPrecedence() throws Exception {
    QueryParserWrapper qp = new QueryParserWrapper("field", new WhitespaceAnalyzer());
    Query query1 = qp.parse("A AND B OR C AND D");
    Query query2 = qp.parse("+A +B +C +D");

    assertEquals(query1, query2);
  }
Ejemplo n.º 4
0
 private void assertHits(int expected, String query, IndexSearcher is)
     throws ParseException, IOException {
   QueryParserWrapper qp = new QueryParserWrapper("date", new WhitespaceAnalyzer());
   qp.setLocale(Locale.ENGLISH);
   Query q = qp.parse(query);
   ScoreDoc[] hits = is.search(q, null, 1000).scoreDocs;
   assertEquals(expected, hits.length);
 }
Ejemplo n.º 5
0
 public void testMatchAllDocs() throws Exception {
   QueryParserWrapper qp = new QueryParserWrapper("field", new WhitespaceAnalyzer());
   assertEquals(new MatchAllDocsQuery(), qp.parse("*:*"));
   assertEquals(new MatchAllDocsQuery(), qp.parse("(*:*)"));
   BooleanQuery bq = (BooleanQuery) qp.parse("+*:* -*:*");
   assertTrue(bq.getClauses()[0].getQuery() instanceof MatchAllDocsQuery);
   assertTrue(bq.getClauses()[1].getQuery() instanceof MatchAllDocsQuery);
 }
Ejemplo n.º 6
0
 public void assertWildcardQueryEquals(String query, String result) throws Exception {
   QueryParserWrapper qp = getParser(null);
   Query q = qp.parse(query);
   String s = q.toString("field");
   if (!s.equals(result)) {
     fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
   }
 }
Ejemplo n.º 7
0
 public void testBooleanQuery() throws Exception {
   BooleanQuery.setMaxClauseCount(2);
   try {
     QueryParserWrapper qp = new QueryParserWrapper("field", new WhitespaceAnalyzer());
     qp.parse("one two three");
     fail("ParseException expected due to too many boolean clauses");
   } catch (ParseException expected) {
     // too many boolean clauses, so ParseException is expected
   }
 }
Ejemplo n.º 8
0
 public void assertWildcardQueryEquals(
     String query, boolean lowercase, String result, boolean allowLeadingWildcard)
     throws Exception {
   QueryParserWrapper qp = getParser(null);
   qp.setLowercaseExpandedTerms(lowercase);
   qp.setAllowLeadingWildcard(allowLeadingWildcard);
   Query q = qp.parse(query);
   String s = q.toString("field");
   if (!s.equals(result)) {
     fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
   }
 }
Ejemplo n.º 9
0
 public void assertQueryEquals(QueryParserWrapper qp, String field, String query, String result)
     throws Exception {
   Query q = qp.parse(query);
   String s = q.toString(field);
   if (!s.equals(result)) {
     fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
   }
 }
Ejemplo n.º 10
0
  public void testDateRange() throws Exception {
    String startDate = getLocalizedDate(2002, 1, 1, false);
    String endDate = getLocalizedDate(2002, 1, 4, false);
    Calendar endDateExpected = new GregorianCalendar();
    endDateExpected.set(2002, 1, 4, 23, 59, 59);
    endDateExpected.set(Calendar.MILLISECOND, 999);
    final String defaultField = "default";
    final String monthField = "month";
    final String hourField = "hour";
    QueryParserWrapper qp = new QueryParserWrapper("field", new SimpleAnalyzer());

    // Don't set any date resolution and verify if DateField is used
    assertDateRangeQueryEquals(
        qp, defaultField, startDate, endDate, endDateExpected.getTime(), null);

    // set a field specific date resolution
    qp.setDateResolution(monthField, DateTools.Resolution.MONTH);

    // DateField should still be used for defaultField
    assertDateRangeQueryEquals(
        qp, defaultField, startDate, endDate, endDateExpected.getTime(), null);

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

    // set second field specific date resolution
    qp.setDateResolution(hourField, DateTools.Resolution.HOUR);

    // 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.getTime(),
        DateTools.Resolution.MILLISECOND);

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

    assertDateRangeQueryEquals(
        qp, hourField, startDate, endDate, endDateExpected.getTime(), DateTools.Resolution.HOUR);
  }
Ejemplo n.º 11
0
 public void testStopwords() throws Exception {
   QueryParserWrapper qp =
       new QueryParserWrapper("a", new StopAnalyzer(new String[] {"the", "foo"}));
   Query result = qp.parse("a:the OR a:foo");
   assertNotNull("result is null and it shouldn't be", result);
   assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
   assertTrue(
       ((BooleanQuery) result).clauses().size() + " does not equal: " + 0,
       ((BooleanQuery) result).clauses().size() == 0);
   result = qp.parse("a:woo OR a:the");
   assertNotNull("result is null and it shouldn't be", result);
   assertTrue("result is not a TermQuery", result instanceof TermQuery);
   result = qp.parse("(fieldX:xxxxx OR fieldy:xxxxxxxx)^2 AND (fieldx:the OR fieldy:foo)");
   assertNotNull("result is null and it shouldn't be", result);
   assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
   System.out.println("Result: " + result);
   assertTrue(
       ((BooleanQuery) result).clauses().size() + " does not equal: " + 2,
       ((BooleanQuery) result).clauses().size() == 2);
 }
Ejemplo n.º 12
0
  public void testRange() throws Exception {
    assertQueryEquals("[ a TO z]", null, "[a TO z]");
    assertEquals(
        MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT,
        ((TermRangeQuery) getQuery("[ a TO z]", null)).getRewriteMethod());

    QueryParserWrapper qp = new QueryParserWrapper("field", new SimpleAnalyzer());

    qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    assertEquals(
        MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE,
        ((TermRangeQuery) qp.parse("[ a TO z]")).getRewriteMethod());

    assertQueryEquals("[ a TO z ]", null, "[a TO z]");
    assertQueryEquals("{ a TO z}", null, "{a TO z}");
    assertQueryEquals("{ a TO z }", null, "{a TO z}");
    assertQueryEquals("{ a TO z }^2.0", null, "{a TO z}^2.0");
    assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
    assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
    assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
    assertQueryEquals("gack ( bar blar { a TO z}) ", null, "gack (bar blar {a TO z})");
  }
Ejemplo n.º 13
0
  public void testFarsiRangeCollating() throws Exception {

    RAMDirectory ramDir = new RAMDirectory();
    IndexWriter iw =
        new IndexWriter(ramDir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
    Document doc = new Document();
    doc.add(new Field("content", "\u0633\u0627\u0628", Field.Store.YES, Field.Index.UN_TOKENIZED));
    iw.addDocument(doc);
    iw.close();
    IndexSearcher is = new IndexSearcher(ramDir);

    QueryParserWrapper qp = new QueryParserWrapper("content", new WhitespaceAnalyzer());

    // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
    // RuleBasedCollator. However, the Arabic Locale seems to order the Farsi
    // characters properly.
    Collator c = Collator.getInstance(new Locale("ar"));
    qp.setRangeCollator(c);

    // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
    // orders the U+0698 character before the U+0633 character, so the single
    // index Term below should NOT be returned by a ConstantScoreRangeQuery
    // with a Farsi Collator (or an Arabic one for the case when Farsi is not
    // supported).

    // Test ConstantScoreRangeQuery
    qp.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
    ScoreDoc[] result = is.search(qp.parse("[ \u062F TO \u0698 ]"), null, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);

    result = is.search(qp.parse("[ \u0633 TO \u0638 ]"), null, 1000).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);

    // Test RangeQuery
    qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    result = is.search(qp.parse("[ \u062F TO \u0698 ]"), null, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);

    result = is.search(qp.parse("[ \u0633 TO \u0638 ]"), null, 1000).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);

    is.close();
  }
Ejemplo n.º 14
0
  public void testBoost() throws Exception {
    StandardAnalyzer oneStopAnalyzer = new StandardAnalyzer(new String[] {"on"});
    QueryParserWrapper qp = new QueryParserWrapper("field", oneStopAnalyzer);
    Query q = qp.parse("on^1.0");
    assertNotNull(q);
    q = qp.parse("\"hello\"^2.0");
    assertNotNull(q);
    assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
    q = qp.parse("hello^2.0");
    assertNotNull(q);
    assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
    q = qp.parse("\"on\"^1.0");
    assertNotNull(q);

    QueryParserWrapper qp2 = new QueryParserWrapper("field", new StandardAnalyzer());
    q = qp2.parse("the^3");
    // "the" is a stop word so the result is an empty query:
    assertNotNull(q);
    assertEquals("", q.toString());
    assertEquals(1.0f, q.getBoost(), 0.01f);
  }
Ejemplo n.º 15
0
 public QueryParserWrapper getParser(Analyzer a) throws Exception {
   if (a == null) a = new SimpleAnalyzer();
   QueryParserWrapper qp = new QueryParserWrapper("field", a);
   qp.setDefaultOperator(QueryParserWrapper.OR_OPERATOR);
   return qp;
 }
Ejemplo n.º 16
0
  public void testSimple() throws Exception {
    assertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
    assertQueryEquals("term term term", null, "term term term");
    assertQueryEquals("t�rm term term", new WhitespaceAnalyzer(), "t�rm term term");
    assertQueryEquals("�mlaut", new WhitespaceAnalyzer(), "�mlaut");

    assertQueryEquals("\"\"", new KeywordAnalyzer(), "");
    assertQueryEquals("foo:\"\"", new KeywordAnalyzer(), "foo:");

    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("a OR !b", null, "a -b");

    assertQueryEquals("a OR ! b", null, "a -b");

    assertQueryEquals("a OR -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) instanceof BooleanQuery);
    assertTrue(getQuery("hello", null) instanceof TermQuery);
    assertTrue(getQuery("\"hello there\"", null) instanceof 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\"");

    QueryParserWrapper qp = new QueryParserWrapper("field", new StandardAnalyzer());
    // make sure OR is the default:
    assertEquals(QueryParserWrapper.OR_OPERATOR, qp.getDefaultOperator());
    qp.setDefaultOperator(QueryParserWrapper.AND_OPERATOR);
    assertEquals(QueryParserWrapper.AND_OPERATOR, qp.getDefaultOperator());
    qp.setDefaultOperator(QueryParserWrapper.OR_OPERATOR);
    assertEquals(QueryParserWrapper.OR_OPERATOR, qp.getDefaultOperator());
  }
Ejemplo n.º 17
0
 public Query getQueryDOA(String query, Analyzer a) throws Exception {
   if (a == null) a = new SimpleAnalyzer();
   QueryParserWrapper qp = new QueryParserWrapper("field", a);
   qp.setDefaultOperator(QueryParserWrapper.AND_OPERATOR);
   return qp.parse(query);
 }
Ejemplo n.º 18
0
 public Query getQueryAllowLeadingWildcard(String query, Analyzer a) throws Exception {
   QueryParserWrapper parser = getParser(a);
   parser.setAllowLeadingWildcard(true);
   return parser.parse(query);
 }
Ejemplo n.º 19
0
 public void assertEscapedQueryEquals(String query, Analyzer a, String result) throws Exception {
   String escapedQuery = QueryParserWrapper.escape(query);
   if (!escapedQuery.equals(result)) {
     fail("Query /" + query + "/ yielded /" + escapedQuery + "/, expecting /" + result + "/");
   }
 }