Exemplo 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());
 }
Exemplo n.º 2
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);
  }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
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);
    }
  }
Exemplo n.º 5
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 + "/");
   }
 }
Exemplo n.º 6
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 + "/");
   }
 }
Exemplo n.º 7
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);
 }
Exemplo n.º 8
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
   }
 }
Exemplo n.º 9
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();
  }
Exemplo n.º 10
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);
 }
Exemplo n.º 11
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);
  }
Exemplo n.º 12
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 + "/");
   }
 }
Exemplo n.º 13
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})");
  }
Exemplo n.º 14
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);
 }
Exemplo n.º 15
0
 public Query getQueryAllowLeadingWildcard(String query, Analyzer a) throws Exception {
   QueryParserWrapper parser = getParser(a);
   parser.setAllowLeadingWildcard(true);
   return parser.parse(query);
 }