You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by bogdan71 <bo...@gmail.com> on 2008/09/04 10:46:45 UTC

QueryParser vs. BooleanQuery

  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


Re: QueryParser vs. BooleanQuery

Posted by 叶双明 <ye...@gmail.com>.
Indeed, StandardAnalyzer removing the pluses, so analyse 'c++' to 'c'.
QueryParser include Term that been analysed.
And BooleanQuery include Term that hasn't been analysed.
I think this is the difference between they.



2008/9/4 Ian Lea <ia...@gmail.com>

> Have a look at the index with Luke to see what has actually been
> indexed. StandardAnalyzer may well be removing the pluses, or you may
> need to escape them.  And watch out for case - Visual != visual in
> term query land.
>
>
> --
> Ian.
>
>
> On Thu, Sep 4, 2008 at 9:46 AM, bogdan71 <bo...@gmail.com> wrote:
> >
> >  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
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

Re: QueryParser vs. BooleanQuery

Posted by Ian Lea <ia...@gmail.com>.
Have a look at the index with Luke to see what has actually been
indexed. StandardAnalyzer may well be removing the pluses, or you may
need to escape them.  And watch out for case - Visual != visual in
term query land.


--
Ian.


On Thu, Sep 4, 2008 at 9:46 AM, bogdan71 <bo...@gmail.com> wrote:
>
>  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
>
>

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