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 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 static AqpQueryParser init(Analyzer a) throws Exception {
      AqpQueryParser p = AqpStandardLuceneParser.init();

      ((QueryNodeProcessorPipeline) p.getQueryNodeProcessor())
          .add(new QPTestParserQueryNodeProcessor());
      p.setAnalyzer(a);
      return p;
    }
  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 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 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 testDateRange() throws Exception {
    String startDate = getLocalizedDate(2002, 1, 1);
    String endDate = getLocalizedDate(2002, 1, 4);
    Calendar endDateExpected = new GregorianCalendar();
    endDateExpected.clear();
    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";
    AqpQueryParser qp = getParser();

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

    Map<CharSequence, DateTools.Resolution> dateRes =
        new HashMap<CharSequence, DateTools.Resolution>();

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

    // 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
    dateRes.put(hourField, DateTools.Resolution.HOUR);
    qp.setDateResolution(dateRes);

    // 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);
  }
  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();
  }
  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}");
  }
  /** 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 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(TEST_VERSION_CURRENT), "t�rm term term");
    assertQueryEquals("�mlaut", new WhitespaceAnalyzer(TEST_VERSION_CURRENT), "�mlaut");

    // XXX: not allowed, TODO???
    // 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 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("((a OR b) 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\"");

    AqpQueryParser qp = getParser();
    qp.setDefaultOperator(Operator.OR);
    assertQueryMatch(qp, "title:(+a -b c)", "text", "+title:a -title:b title:c");

    qp.setDefaultOperator(Operator.AND);
    assertQueryMatch(qp, "title:(+a -b c)", "text", "+title:a -title:b +title:c");
  }