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 Vishal Bathija <vi...@gmail.com> on 2006/04/19 04:00:07 UTC

using boolean operators with the PhraseQuery

Hi,
I am trying to find the number of hits for a phrase using the
PhraseQuery. I would like to know how I could seach for 2 phrases at
the same time using the boolean operators OR, AND. The code snippet
that I use to seach for one phrase is

String test ="avoids deadlock"
String[] phraseTerms = test.split( " ");
PhraseQuery query =new PhraseQuery();			
searcher = new IndexSearcher(rd);			
Term[] phrTerm=new Term[phraseTerms.length];
for(int u=0; u<phraseTerms.length;u++)
{
phrTerm[u]=new Term("contents",phraseTerms[u]);
query.add(phrTerm[u]);
}

Hits hits = searcher.search(query);	

How can I extend this to search for multiple phrases?

Regards
Vishal
--
Vishal Bathija
Graduate Student
Department of Computer Science & Systems Analysis
Miami University
Oxford,Ohio
Phone: (513)-461-9239

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


Re: using boolean operators with the PhraseQuery

Posted by Paul Elschot <pa...@xs4all.nl>.
On Monday 24 April 2006 16:24, Vishal Bathija wrote:
> How do I get the spans (getSpans()) if I use the SpanNearQuery as a
> clause (subquery) in a BooleanQuery

You can't normally. The Spans of the subqueries are used to compute the scores
of each matching document, via SpanScorer and BooleanScorer2.
Boolean queries do not have Spans.

Regards,
Paul Elschot

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


Re: using boolean operators with the PhraseQuery

Posted by Vishal Bathija <vi...@gmail.com>.
How do I get the spans (getSpans()) if I use the SpanNearQuery as a
clause (subquery) in a BooleanQuery

I currently have

SpanQuery [] clauses1 = new SpanQuery[phraseTerms1.length];
for(int count =0; count< phraseTerms1.length; count++)
{
clauses1[count]=phrase1[count];
}
SpanQuery [] clauses2 = new SpanQuery[phraseTerms2.length];
for(int count =0; count< phraseTerms2.length; count++)
{
clauses2[count]=phrase2[count];
}
SpanNearQuery near1 = new SpanNearQuery(clauses1, 0, true);
SpanNearQuery near2 = new SpanNearQuery(clauses2, 0, true);
						
I need the spans of either clause1 or clause2.

Vishal

On 4/22/06, Paul Elschot <pa...@xs4all.nl> wrote:
> On Friday 21 April 2006 21:11, Vishal Bathija wrote:
> > Hi,
> > I am trying to get the frequency of a phrase using the SpanNearQuery.
> > How can I use SpanNearQuery for boolean queries. The code I have is
> > for a single query. How can I extend this for multiple queries
>
> You can use SpanNearQuery as a clause (subquery) in a BooleanQuery
> or in a SpanOrQuery.
> See BooleanQuery.add() and the constructor of SpanOrQuery.
>
> Regards,
> Paul Elschot
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>


--
Vishal Bathija
Graduate Student
Department of Computer Science & Systems Analysis
Miami University
Oxford,Ohio
Phone: (513)-461-9239

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


Re: using boolean operators with the PhraseQuery

Posted by Paul Elschot <pa...@xs4all.nl>.
On Friday 21 April 2006 21:11, Vishal Bathija wrote:
> Hi,
> I am trying to get the frequency of a phrase using the SpanNearQuery.
> How can I use SpanNearQuery for boolean queries. The code I have is
> for a single query. How can I extend this for multiple queries

You can use SpanNearQuery as a clause (subquery) in a BooleanQuery
or in a SpanOrQuery.
See BooleanQuery.add() and the constructor of SpanOrQuery.

Regards,
Paul Elschot

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


Re: using boolean operators with the PhraseQuery

Posted by Vishal Bathija <vi...@gmail.com>.
Hi,
I am trying to get the frequency of a phrase using the SpanNearQuery.
How can I use SpanNearQuery for boolean queries. The code I have is
for a single query. How can I extend this for multiple queries

SpanTermQuery[] phrase = new SpanTermQuery[phraseTerms.length];	
for(int termCount=0; termCount<phraseTerms.length ;termCount++)
{

phrase[termCount] = new SpanTermQuery(new Term("contents", phraseTerm
termCount]));
}
SpanQuery [] clauses = new SpanQuery[phraseTerms.length];

for(int count =0; count<phraseTerms.length ; count++)
{
	clauses[count]=phrase[count];	
}
SpanNearQuery near = new SpanNearQuery(clauses, 0, true);
Spans spans = near.getSpans(rd);		




Regards
Vishal


On 4/19/06, Daniel Naber <lu...@danielnaber.de> wrote:
> On Mittwoch 19 April 2006 07:25, Vishal Bathija wrote:
>
> > Query= contents:"provides distribution" contents:"supports
> > distribution"
>
> Why are there two spaces between those words?
>
> > I am not sure why it returns 0, when I have both phrases present in the
> > docs.
>
> See
> http://wiki.apache.org/jakarta-lucene/LuceneFAQ#head-3558e5121806fb4fce80fc022d889484a9248b71
>
> Regards
>  Daniel
>
> --
> http://www.danielnaber.de
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>


--
Vishal Bathija
Graduate Student
Department of Computer Science & Systems Analysis
Miami University
Oxford,Ohio
Phone: (513)-461-9239

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


Re: using boolean operators with the PhraseQuery

Posted by Daniel Naber <lu...@danielnaber.de>.
On Mittwoch 19 April 2006 07:25, Vishal Bathija wrote:

> Query= contents:"provides  distribution" contents:"supports
>  distribution"

Why are there two spaces between those words?

> I am not sure why it returns 0, when I have both phrases present in the
> docs.

See
http://wiki.apache.org/jakarta-lucene/LuceneFAQ#head-3558e5121806fb4fce80fc022d889484a9248b71

Regards
 Daniel

-- 
http://www.danielnaber.de

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


Re: using boolean operators with the PhraseQuery

Posted by Vishal Bathija <vi...@gmail.com>.
I tried using the boolean query to perform an OR  as below
BooleanQuery b1 = new BooleanQuery();
 b1.add(query,BooleanClause .Occur .SHOULD  );
 b1.add(query2,BooleanClause .Occur .SHOULD );
Hits hits = searcher.search(b1);		


System.out.println("Query= "+b1.toString() );

gave me
Query= contents:"provides  distribution" contents:"supports  distribution"

This returns 0 hits.

I am not sure why it returns 0, when I have both phrases present in the docs.


Vishal
On 4/19/06, Chris Hostetter <ho...@fucit.org> wrote:
>
> : The code above just adds the terms of phrase2 following the
> : terms  for phrase1.
> : Can you give me an example building a BooleanQuery OR  for the
> : newTerm1 and newTerm2.
>
> At no point does your code use a BooleanQuery ... have you looked at the
> javadocs for the BooleanQuery class?
>
>
>
> -Hoss
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>


--
Vishal Bathija
Graduate Student
Department of Computer Science & Systems Analysis
Miami University
Oxford,Ohio
Phone: (513)-461-9239

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


Re: using boolean operators with the PhraseQuery

Posted by Chris Hostetter <ho...@fucit.org>.
: The code above just adds the terms of phrase2 following the
: terms  for phrase1.
: Can you give me an example building a BooleanQuery OR  for the
: newTerm1 and newTerm2.

At no point does your code use a BooleanQuery ... have you looked at the
javadocs for the BooleanQuery class?



-Hoss


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


Re: using boolean operators with the PhraseQuery

Posted by Vishal Bathija <vi...@gmail.com>.
I am not sure if I understand you.  Do I add the terms for the second
phrase immediately after I add the terms for the first phrase. When do
i wrap the PhraseQuery I construct into a BooleanQuery.

For instance
String newTerm1= "avoids deadlock";
String newTerm2= "reduces cost";
PhraseQuery query =new PhraseQuery();	

String[] phraseTerms1 = newTerm1.split( " ");
Term[] phrTerm1=new Term[phraseTerms1.length];

for(int u=0; u<phraseTerms1.length;u++)
{
							phrTerm1[u]=new Term("contents",phraseTerms1[u]);
	query.add(phrTerm1[u]);
}

String[] phraseTerms2 = newTerm2.split( " ");			
Term[] phrTerm2=new Term[phraseTerms2.length];

for(int u=0; u<phraseTerms2.length;u++)
{
							phrTerm2[u]=new Term("contents",phraseTerms2[u]);
	query.add(phrTerm2[u]);
						
}
Hits hits = searcher.search(query);		
									The code above just adds the terms of phrase2 following the
terms  for phrase1.
Can you give me an example building a BooleanQuery OR  for the
newTerm1 and newTerm2.

Thanks again
Vishal



On 4/18/06, Erik Hatcher <er...@ehatchersolutions.com> wrote:
> Wrap the PhraseQuery's inside a BooleanQuery to achieve AND/OR.
>
>        Erik
>
>
> On Apr 18, 2006, at 10:00 PM, Vishal Bathija wrote:
>
> > Hi,
> > I am trying to find the number of hits for a phrase using the
> > PhraseQuery. I would like to know how I could seach for 2 phrases at
> > the same time using the boolean operators OR, AND. The code snippet
> > that I use to seach for one phrase is
> >
> > String test ="avoids deadlock"
> > String[] phraseTerms = test.split( " ");
> > PhraseQuery query =new PhraseQuery();
> > searcher = new IndexSearcher(rd);
> > Term[] phrTerm=new Term[phraseTerms.length];
> > for(int u=0; u<phraseTerms.length;u++)
> > {
> > phrTerm[u]=new Term("contents",phraseTerms[u]);
> > query.add(phrTerm[u]);
> > }
> >
> > Hits hits = searcher.search(query);
> >
> > How can I extend this to search for multiple phrases?
> >
> > Regards
> > Vishal
> > --
> > Vishal Bathija
> > Graduate Student
> > Department of Computer Science & Systems Analysis
> > Miami University
> > Oxford,Ohio
> > Phone: (513)-461-9239
> >
> > ---------------------------------------------------------------------
> > 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
>
>


--
Vishal Bathija
Graduate Student
Department of Computer Science & Systems Analysis
Miami University
Oxford,Ohio
Phone: (513)-461-9239

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


Re: using boolean operators with the PhraseQuery

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
Wrap the PhraseQuery's inside a BooleanQuery to achieve AND/OR.

	Erik


On Apr 18, 2006, at 10:00 PM, Vishal Bathija wrote:

> Hi,
> I am trying to find the number of hits for a phrase using the
> PhraseQuery. I would like to know how I could seach for 2 phrases at
> the same time using the boolean operators OR, AND. The code snippet
> that I use to seach for one phrase is
>
> String test ="avoids deadlock"
> String[] phraseTerms = test.split( " ");
> PhraseQuery query =new PhraseQuery();			
> searcher = new IndexSearcher(rd);			
> Term[] phrTerm=new Term[phraseTerms.length];
> for(int u=0; u<phraseTerms.length;u++)
> {
> phrTerm[u]=new Term("contents",phraseTerms[u]);
> query.add(phrTerm[u]);
> }
>
> Hits hits = searcher.search(query);	
>
> How can I extend this to search for multiple phrases?
>
> Regards
> Vishal
> --
> Vishal Bathija
> Graduate Student
> Department of Computer Science & Systems Analysis
> Miami University
> Oxford,Ohio
> Phone: (513)-461-9239
>
> ---------------------------------------------------------------------
> 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