lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bogdan71 <bogdan.pi...@gmail.com>
Subject QueryParser vs. BooleanQuery
Date Thu, 04 Sep 2008 08:46:45 GMT

  Hello,

  I am experiencing a strange behaviour when trying to query the same thing
via
BooleanQuery vs. via the know-it-all QueryParser class. Precisely, the index
contains
the document:
   "12,Visual C++,4.2" with the field layout: ID,name,version(thus, "12" is
the ID field, "Visual C++"
is the name field and "4.2" is the version field). 
  The search string is "Visual C++" for the name field.

  The following test, using QueryParser, goes fine:

public final void testUsingQueryParser()
	{
		IndexSearcher recordSearcher;
		Query q;
		QueryParser parser = new QueryParser("name", new StandardAnalyzer());
		try
		{
		       q = parser.parse("name:visual +name:c++");
		    
		        Directory directory =
FSDirectory.getDirectory(<some_path_to_index>); 
			recordSearcher = new IndexSearcher(directory);	
			
			Hits h = recordSearcher.search(q);
			
			assertEquals(1, h.length());
			assertEquals(12, Integer.parseInt(h.doc(0).get("ID")));
		}
		catch(Exception exn)
		{
			fail("Exception occurred.");
		}
	}

  But this one, using a BooleanQuery, fails.

public final void testUsingTermQuery()
	{
		IndexSearcher recordSearcher;
		BooleanQuery bq = new BooleanQuery();
		
		bq.add(new TermQuery(new Term("name", "visual")),
BooleanClause.Occur.SHOULD);
		bq.add(new TermQuery(new Term("name", "c++")), BooleanClause.Occur.MUST);
		
		try
		{   
		        Directory directory =
FSDirectory.getDirectory(<some_path_to_index>); 
			recordSearcher = new IndexSearcher(directory);	
			
			Hits h = recordSearcher.search(bq);
			
			assertEquals(1, h.length());   // fails, saying it expects 0 !!!
			assertEquals(12, Integer.parseInt(h.doc(0).get("ID")));
		}
		catch(Exception exn)
		{
			fail("Eexception occurred.");
		}	
	}

   Rewriting the BooleanQuery and taking toString() yields the same String
given to QueryParser.parse() in the first test. I am using Lucene 2.3.0. Can
somebody explain the difference ?
-- 
View this message in context: http://www.nabble.com/QueryParser-vs.-BooleanQuery-tp19306087p19306087.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message