Search in sources :

Example 16 with CommonQueryParserConfiguration

use of org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration in project lucene-solr by apache.

the class QueryParserTestBase method testDateRange.

public void testDateRange() throws Exception {
    String startDate = getLocalizedDate(2002, 1, 1);
    String endDate = getLocalizedDate(2002, 1, 4);
    // we use the default Locale/TZ since LuceneTestCase randomizes it
    Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
    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";
    Analyzer a = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
    CommonQueryParserConfiguration qp = getParserConfig(a);
    // set a field specific date resolution
    setDateResolution(qp, monthField, DateTools.Resolution.MONTH);
    // set default date resolution to MILLISECOND
    qp.setDateResolution(DateTools.Resolution.MILLISECOND);
    // set second field specific date resolution    
    setDateResolution(qp, 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);
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) CommonQueryParserConfiguration(org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)

Example 17 with CommonQueryParserConfiguration

use of org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration in project lucene-solr by apache.

the class QueryParserTestBase method testPhraseQueryToString.

public void testPhraseQueryToString() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET);
    CommonQueryParserConfiguration qp = getParserConfig(analyzer);
    qp.setEnablePositionIncrements(true);
    PhraseQuery q = (PhraseQuery) getQuery("\"this hi this is a test is\"", qp);
    assertEquals("field:\"? hi ? ? ? test\"", q.toString());
}
Also used : CommonQueryParserConfiguration(org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)

Example 18 with CommonQueryParserConfiguration

use of org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration in project lucene-solr by apache.

the class QueryParserTestBase method testRegexps.

public void testRegexps() throws Exception {
    CommonQueryParserConfiguration qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true));
    RegexpQuery q = new RegexpQuery(new Term("field", "[a-z][123]"));
    assertEquals(q, getQuery("/[a-z][123]/", qp));
    assertEquals(q, getQuery("/[A-Z][123]/", qp));
    assertEquals(new BoostQuery(q, 0.5f), getQuery("/[A-Z][123]/^0.5", qp));
    qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
    q.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
    assertTrue(getQuery("/[A-Z][123]/^0.5", qp) instanceof BoostQuery);
    assertTrue(((BoostQuery) getQuery("/[A-Z][123]/^0.5", qp)).getQuery() instanceof RegexpQuery);
    assertEquals(MultiTermQuery.SCORING_BOOLEAN_REWRITE, ((RegexpQuery) ((BoostQuery) getQuery("/[A-Z][123]/^0.5", qp)).getQuery()).getRewriteMethod());
    assertEquals(new BoostQuery(q, 0.5f), getQuery("/[A-Z][123]/^0.5", qp));
    qp.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
    Query escaped = new RegexpQuery(new Term("field", "[a-z]\\/[123]"));
    assertEquals(escaped, getQuery("/[a-z]\\/[123]/", qp));
    Query escaped2 = new RegexpQuery(new Term("field", "[a-z]\\*[123]"));
    assertEquals(escaped2, getQuery("/[a-z]\\*[123]/", qp));
    BooleanQuery.Builder complex = new BooleanQuery.Builder();
    complex.add(new RegexpQuery(new Term("field", "[a-z]\\/[123]")), Occur.MUST);
    complex.add(new TermQuery(new Term("path", "/etc/init.d/")), Occur.MUST);
    complex.add(new TermQuery(new Term("field", "/etc/init[.]d/lucene/")), Occur.SHOULD);
    assertEquals(complex.build(), getQuery("/[a-z]\\/[123]/ AND path:\"/etc/init.d/\" OR \"/etc\\/init\\[.\\]d/lucene/\" ", qp));
    Query re = new RegexpQuery(new Term("field", "http.*"));
    assertEquals(re, getQuery("field:/http.*/", qp));
    assertEquals(re, getQuery("/http.*/", qp));
    re = new RegexpQuery(new Term("field", "http~0.5"));
    assertEquals(re, getQuery("field:/http~0.5/", qp));
    assertEquals(re, getQuery("/http~0.5/", qp));
    re = new RegexpQuery(new Term("field", "boo"));
    assertEquals(re, getQuery("field:/boo/", qp));
    assertEquals(re, getQuery("/boo/", qp));
    assertEquals(new TermQuery(new Term("field", "/boo/")), getQuery("\"/boo/\"", qp));
    assertEquals(new TermQuery(new Term("field", "/boo/")), getQuery("\\/boo\\/", qp));
    BooleanQuery.Builder two = new BooleanQuery.Builder();
    two.add(new RegexpQuery(new Term("field", "foo")), Occur.SHOULD);
    two.add(new RegexpQuery(new Term("field", "bar")), Occur.SHOULD);
    assertEquals(two.build(), getQuery("field:/foo/ field:/bar/", qp));
    assertEquals(two.build(), getQuery("/foo/ /bar/", qp));
}
Also used : Term(org.apache.lucene.index.Term) CommonQueryParserConfiguration(org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)

Example 19 with CommonQueryParserConfiguration

use of org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration in project lucene-solr by apache.

the class QueryParserTestBase method testStopwords.

public void testStopwords() throws Exception {
    CharacterRunAutomaton stopSet = new CharacterRunAutomaton(new RegExp("the|foo").toAutomaton());
    CommonQueryParserConfiguration qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, stopSet));
    Query result = getQuery("field:the OR field:foo", qp);
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery || result instanceof MatchNoDocsQuery);
    if (result instanceof BooleanQuery) {
        assertEquals(0, ((BooleanQuery) result).clauses().size());
    }
    result = getQuery("field:woo OR field:the", qp);
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a TermQuery", result instanceof TermQuery);
    result = getQuery("(fieldX:xxxxx OR fieldy:xxxxxxxx)^2 AND (fieldx:the OR fieldy:foo)", qp);
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a BoostQuery", result instanceof BoostQuery);
    result = ((BoostQuery) result).getQuery();
    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);
}
Also used : RegExp(org.apache.lucene.util.automaton.RegExp) CharacterRunAutomaton(org.apache.lucene.util.automaton.CharacterRunAutomaton) CommonQueryParserConfiguration(org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)

Example 20 with CommonQueryParserConfiguration

use of org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration in project lucene-solr by apache.

the class QueryParserTestBase method testPrecedence.

/**
   * This test differs from TestPrecedenceQueryParser
   */
public void testPrecedence() throws Exception {
    CommonQueryParserConfiguration qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
    Query query1 = getQuery("A AND B OR C AND D", qp);
    Query query2 = getQuery("+A +B +C +D", qp);
    assertEquals(query1, query2);
}
Also used : CommonQueryParserConfiguration(org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)

Aggregations

CommonQueryParserConfiguration (org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration)21 Term (org.apache.lucene.index.Term)4 CharacterRunAutomaton (org.apache.lucene.util.automaton.CharacterRunAutomaton)3 RegExp (org.apache.lucene.util.automaton.RegExp)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 TestQueryParser (org.apache.lucene.queryparser.classic.TestQueryParser)1