public void testStopwords() throws Exception {
    AqpQueryParser qp = getParser();
    qp.setAnalyzer(
        new StopAnalyzer(
            TEST_VERSION_CURRENT, StopFilter.makeStopSet(TEST_VERSION_CURRENT, "the", "foo")));

    Query result = qp.parse("a:the OR a:foo", "a");
    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", "a");
    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)", "a");

    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
    if (VERBOSE) System.out.println("Result: " + result);
    assertTrue(
        ((BooleanQuery) result).clauses().size() + " does not equal: " + 2,
        ((BooleanQuery) result).clauses().size() == 2);
  }
  public void testBoost() throws Exception {
    StandardAnalyzer oneStopAnalyzer =
        new StandardAnalyzer(TEST_VERSION_CURRENT, Collections.singleton("on"));
    AqpQueryParser qp = getParser();
    qp.setAnalyzer(oneStopAnalyzer);

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

    AqpQueryParser qp2 = AqpStandardLuceneParser.init();
    qp2.setAnalyzer(new StandardAnalyzer(TEST_VERSION_CURRENT));

    q = qp2.parse("the^3", "field");
    // "the" is a stop word so the result is an empty query:
    assertNotNull(q);
    assertEquals("", q.toString());
    assertEquals(1.0f, q.getBoost(), 0.01f);
  }
 public void testLeadingWildcardType() throws Exception {
   AqpQueryParser qp = getParser(null);
   qp.setAllowLeadingWildcard(true);
   assertEquals(WildcardQuery.class, qp.parse("t*erm*", "field").getClass());
   assertEquals(WildcardQuery.class, qp.parse("?term*", "field").getClass());
   assertEquals(WildcardQuery.class, qp.parse("*term*", "field").getClass());
 }
  public void testMatchAllDocs() throws Exception {
    AqpQueryParser qp = getParser();
    qp.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));

    assertEquals(new MatchAllDocsQuery(), qp.parse("*:*", "field"));
    assertEquals(new MatchAllDocsQuery(), qp.parse("(*:*)", "field"));
    BooleanQuery bq = (BooleanQuery) qp.parse("+*:* -*:*", "field");
    assertTrue(bq.getClauses()[0].getQuery() instanceof MatchAllDocsQuery);
    assertTrue(bq.getClauses()[1].getQuery() instanceof MatchAllDocsQuery);
  }
  public void testFarsiRangeCollating() throws Exception {
    Directory ramDir = newDirectory();
    IndexWriter iw =
        new IndexWriter(
            ramDir,
            newIndexWriterConfig(
                TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)));
    Document doc = new Document();
    doc.add(newField("content", "\u0633\u0627\u0628", Field.Store.YES, Field.Index.NOT_ANALYZED));
    iw.addDocument(doc);
    iw.close();
    IndexSearcher is = new IndexSearcher(ramDir, true);

    AqpQueryParser qp = getParser();
    qp.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));

    // 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 ]", "content"), null, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);

    result = is.search(qp.parse("[ \u0633 TO \u0638 ]", "content"), 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 ]", "content"), null, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);

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

    is.close();
    ramDir.close();
  }
  /** This test differs from TestPrecedenceQueryParser */
  public void testPrecedence() throws Exception {
    AqpQueryParser qp1 = getParser();
    qp1.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));

    AqpQueryParser qp2 = getParser();
    qp2.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));

    // TODO: to achieve standard lucene behaviour (no operator precedence)
    // modify the GroupQueryNodeProcessor to recognize our new BooleanQN classes
    // then do:
    QueryNodeProcessorPipeline processor = (QueryNodeProcessorPipeline) qp1.getQueryNodeProcessor();
    processor.add(new GroupQueryNodeProcessor());

    Query query1 = qp1.parse("A AND B OR C AND D", "field");
    Query query2 = qp2.parse("+A +B +C +D", "field");

    assertEquals(query1, query2);
  }
  public void testBooleanQuery() throws Exception {
    BooleanQuery.setMaxClauseCount(2);
    try {
      AqpQueryParser qp = getParser();
      qp.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));

      qp.parse("one two three", "field");
      fail("ParseException expected due to too many boolean clauses");
    } catch (QueryNodeException expected) {
      // too many boolean clauses, so ParseException is expected
    }
  }
  public void testConstantScoreAutoRewrite() throws Exception {
    AqpQueryParser qp = AqpStandardLuceneParser.init(getGrammarName());
    qp.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));

    Query q = qp.parse("foo*bar", "field");
    assertTrue(q instanceof WildcardQuery);
    assertEquals(
        MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT,
        ((MultiTermQuery) q).getRewriteMethod());

    q = qp.parse("foo*", "field");
    assertTrue(q instanceof PrefixQuery);
    assertEquals(
        MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT,
        ((MultiTermQuery) q).getRewriteMethod());

    q = qp.parse("[a TO z]", "field");
    assertTrue(q instanceof TermRangeQuery);
    assertEquals(
        MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT,
        ((MultiTermQuery) q).getRewriteMethod());
  }
  public void testPositionIncrement() throws Exception {
    AqpQueryParser qp = getParser();
    qp.setAnalyzer(
        new StopAnalyzer(
            TEST_VERSION_CURRENT,
            StopFilter.makeStopSet(TEST_VERSION_CURRENT, "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, "a");
    // 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]);
    }
  }
  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());

    AqpQueryParser qp = getParser();

    qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    assertEquals(
        MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE,
        ((TermRangeQuery) qp.parse("[ a TO z]", "field")).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}");

    // the original expected value was: gack (bar blar {a TO z})
    assertQueryEquals("gack ( bar blar { a TO z}) ", null, "gack bar blar {a TO z}");
  }