FileDocCategorySizeDatePackage
TestMultiFieldQueryParser.javaAPI DocApache Lucene 2.1.010914Wed Feb 14 10:46:34 GMT 2007org.apache.lucene.queryParser

TestMultiFieldQueryParser

public class TestMultiFieldQueryParser extends TestCase
Tests QueryParser.
author
Daniel Naber

Fields Summary
Constructors Summary
Methods Summary
public voidtestAnalyzerReturningNull()

    String[] fields = new String[] { "f1", "f2", "f3" };
    MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new AnalyzerReturningNull());
    Query q = parser.parse("bla AND blo");
    assertEquals("+(f2:bla f3:bla) +(f2:blo f3:blo)", q.toString());
    // the following queries are not affected as their terms are not analyzed anyway:
    q = parser.parse("bla*");
    assertEquals("f1:bla* f2:bla* f3:bla*", q.toString());
    q = parser.parse("bla~");
    assertEquals("f1:bla~0.5 f2:bla~0.5 f3:bla~0.5", q.toString());
    q = parser.parse("[a TO c]");
    assertEquals("f1:[a TO c] f2:[a TO c] f3:[a TO c]", q.toString());
  
public voidtestBoostsSimple()

      Map boosts = new HashMap();
      boosts.put("b", new Float(5));
      boosts.put("t", new Float(10));
      String[] fields = {"b", "t"};
      MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, new StandardAnalyzer(), boosts);
      
      
      //Check for simple
      Query q = mfqp.parse("one");
      assertEquals("b:one^5.0 t:one^10.0", q.toString());
      
      //Check for AND
      q = mfqp.parse("one AND two");
      assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0)", q.toString());
      
      //Check for OR
      q = mfqp.parse("one OR two");
      assertEquals("(b:one^5.0 t:one^10.0) (b:two^5.0 t:two^10.0)", q.toString());
      
      //Check for AND and a field
      q = mfqp.parse("one AND two AND foo:test");
      assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0) +foo:test", q.toString());
      
      q = mfqp.parse("one^3 AND two^4");
      assertEquals("+((b:one^5.0 t:one^10.0)^3.0) +((b:two^5.0 t:two^10.0)^4.0)", q.toString());
  
public voidtestSimple()

    String[] fields = {"b", "t"};
    MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, new StandardAnalyzer());
    
    Query q = mfqp.parse("one");
    assertEquals("b:one t:one", q.toString());
    
    q = mfqp.parse("one two");
    assertEquals("(b:one t:one) (b:two t:two)", q.toString());
    
    q = mfqp.parse("+one +two");
    assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());

    q = mfqp.parse("+one -two -three)");
    assertEquals("+(b:one t:one) -(b:two t:two) -(b:three t:three)", q.toString());
    
    q = mfqp.parse("one^2 two");
    assertEquals("((b:one t:one)^2.0) (b:two t:two)", q.toString());

    q = mfqp.parse("one~ two");
    assertEquals("(b:one~0.5 t:one~0.5) (b:two t:two)", q.toString());

    q = mfqp.parse("one~0.8 two^2");
    assertEquals("(b:one~0.8 t:one~0.8) ((b:two t:two)^2.0)", q.toString());

    q = mfqp.parse("one* two*");
    assertEquals("(b:one* t:one*) (b:two* t:two*)", q.toString());

    q = mfqp.parse("[a TO c] two");
    assertEquals("(b:[a TO c] t:[a TO c]) (b:two t:two)", q.toString());

    q = mfqp.parse("w?ldcard");
    assertEquals("b:w?ldcard t:w?ldcard", q.toString());

    q = mfqp.parse("\"foo bar\"");
    assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString());

    q = mfqp.parse("\"aa bb cc\" \"dd ee\"");
    assertEquals("(b:\"aa bb cc\" t:\"aa bb cc\") (b:\"dd ee\" t:\"dd ee\")", q.toString());

    q = mfqp.parse("\"foo bar\"~4");
    assertEquals("b:\"foo bar\"~4 t:\"foo bar\"~4", q.toString());

    // make sure that terms which have a field are not touched:
    q = mfqp.parse("one f:two");
    assertEquals("(b:one t:one) f:two", q.toString());

    // AND mode:
    mfqp.setDefaultOperator(QueryParser.AND_OPERATOR);
    q = mfqp.parse("one two");
    assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
    q = mfqp.parse("\"aa bb cc\" \"dd ee\"");
    assertEquals("+(b:\"aa bb cc\" t:\"aa bb cc\") +(b:\"dd ee\" t:\"dd ee\")", q.toString());

  
public voidtestStaticMethod1()

    String[] fields = {"b", "t"};
    String[] queries = {"one", "two"};
    Query q = MultiFieldQueryParser.parse(queries, fields, new StandardAnalyzer());
    assertEquals("b:one t:two", q.toString());

    String[] queries2 = {"+one", "+two"};
    q = MultiFieldQueryParser.parse(queries2, fields, new StandardAnalyzer());
    assertEquals("(+b:one) (+t:two)", q.toString());

    String[] queries3 = {"one", "+two"};
    q = MultiFieldQueryParser.parse(queries3, fields, new StandardAnalyzer());
    assertEquals("b:one (+t:two)", q.toString());

    String[] queries4 = {"one +more", "+two"};
    q = MultiFieldQueryParser.parse(queries4, fields, new StandardAnalyzer());
    assertEquals("(b:one +b:more) (+t:two)", q.toString());

    String[] queries5 = {"blah"};
    try {
      q = MultiFieldQueryParser.parse(queries5, fields, new StandardAnalyzer());
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception, array length differs
    }
  
public voidtestStaticMethod2()

    String[] fields = {"b", "t"};
    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
    Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
    assertEquals("+b:one -t:one", q.toString());

    q = MultiFieldQueryParser.parse("one two", fields, flags, new StandardAnalyzer());
    assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

    try {
      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
      q = MultiFieldQueryParser.parse("blah", fields, flags2, new StandardAnalyzer());
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception, array length differs
    }
  
public voidtestStaticMethod2Old()

    String[] fields = {"b", "t"};
    //int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
      BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
      MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());

    Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());//, fields, flags, new StandardAnalyzer());
    assertEquals("+b:one -t:one", q.toString());

    q = MultiFieldQueryParser.parse("one two", fields, flags, new StandardAnalyzer());
    assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

    try {
      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
      q = MultiFieldQueryParser.parse("blah", fields, flags2, new StandardAnalyzer());
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception, array length differs
    }
  
public voidtestStaticMethod3()

    String[] queries = {"one", "two", "three"};
    String[] fields = {"f1", "f2", "f3"};
    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,
        BooleanClause.Occur.MUST_NOT, BooleanClause.Occur.SHOULD};
    Query q = MultiFieldQueryParser.parse(queries, fields, flags, new StandardAnalyzer());
    assertEquals("+f1:one -f2:two f3:three", q.toString());

    try {
      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
      q = MultiFieldQueryParser.parse(queries, fields, flags2, new StandardAnalyzer());
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception, array length differs
    }
  
public voidtestStaticMethod3Old()

    String[] queries = {"one", "two"};
    String[] fields = {"b", "t"};
      BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
    Query q = MultiFieldQueryParser.parse(queries, fields, flags, new StandardAnalyzer());
    assertEquals("+b:one -t:two", q.toString());

    try {
      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
      q = MultiFieldQueryParser.parse(queries, fields, flags2, new StandardAnalyzer());
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception, array length differs
    }
  
public voidtestStopWordSearching()

    Analyzer analyzer = new StandardAnalyzer();
    Directory ramDir = new RAMDirectory();
    IndexWriter iw =  new IndexWriter(ramDir, analyzer, true);
    Document doc = new Document();
    doc.add(new Field("body", "blah the footest blah", Field.Store.NO, Field.Index.TOKENIZED));
    iw.addDocument(doc);
    iw.close();
    
    MultiFieldQueryParser mfqp = 
      new MultiFieldQueryParser(new String[] {"body"}, analyzer);
    mfqp.setDefaultOperator(QueryParser.Operator.AND);
    Query q = mfqp.parse("the footest");
    IndexSearcher is = new IndexSearcher(ramDir);
    Hits hits = is.search(q);
    assertEquals(1, hits.length());
    is.close();