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 Hetan Shah <He...@Sun.COM> on 2004/07/27 01:11:28 UTC

Phrase Query

Hello,

Can someone on the mailing list send me a copy of sample code of how to 
implement the phrase query for my search. Regular Query is working fine, 
but the Phrase Query does not seem to work.

TIA,
-H


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


Re: Phrase Query

Posted by Daniel Naber <da...@t-online.de>.
On Thursday 29 July 2004 22:32, Hetan Shah wrote:

> All the Words:
> The Exact Phrase:
> Any of the Words:

Just use the query parser. For the "All" fields, set 
setOperator(DEFAULT_OPERATOR_AND) before you call parse(). For the phrase, 
just add quotes around the query before you feed it into QuerParser.

Regards
 Daniel


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


Re: Phrase Query

Posted by Hetan Shah <He...@Sun.COM>.
Hi Erik,

I am trying to create the query based on the field the user enters the 
keywords in.

I have these three field.

All the Words:
The Exact Phrase:
Any of the Words:

For All the words and Any of the words I am able to use Query. For The 
Exact Phrase I want to use the PhraseQuery. That is what I was showing 
in the code snippet. My mistake in not commenting out the line where I 
was per-pending and appending the quotation marks, which is taken care 
by  the PhraseQuery. So how would one go about creating the different 
queries and then do the actual search.

Any help and/or code snippet is highly appreciated.

Thanks.
-H

Erik Hatcher wrote:
> On Jul 27, 2004, at 11:42 AM, Hetan Shah wrote:
> 
>> Works for me.
>> Here is what I am striving to achieve.
>>
>> phraseString =     request.getParameter("phrase");
>> if (phraseString.length() > 0){
>>     phraseQueryString = "\""+phraseString+("\"");
>>     phraseQuery = true;
>>     queryString = phraseQueryString;
>> }
>>
>> if(phraseQuery){
>>     PhraseQuery pQuery = new PhraseQuery();
>>     pQuery.add(new Term("contents", phraseString));
>>     pQuery.setSlop(0);
>>     QueryParser qP = new QueryParser();
>>     query = qP.parse(phraseString);
>> }
> 
> 
> I don't understand what you're attempting in the above.  You don't make 
> a new Term and add it to a PhraseQuery instance with the term having 
> quotes around it - it is QueryParser that deals with quoted strings and 
> interpreting it into a PhraseQuery.
> 
> Are you using pQuery?  Or query?  You've got two Query instances there - 
> query is the one you probably want.
> 
>     Erik
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
> 


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


Re: Phrase Query

Posted by Hetan Shah <He...@Sun.COM>.
What I am trying to do is to create the query based on the field in 
which user enters the keywords.

I have the following fields.

All the Words:
The Exact Phrase:
Any of the Words:

For All the words and Any of the words I am able to use Query. For The 
Exact Phrase I want to use the PhraseQuery. That is what I was showing 
in the code snippet. My mistake in not commenting out the line where I 
was per-pending and appending the quotation marks, which is taken care 
by  the PhraseQuery. So how would one go about creating the different 
queries and then do the actual search.

Thanks.
-H

Erik Hatcher wrote:

> On Jul 27, 2004, at 11:42 AM, Hetan Shah wrote:
> 
>> Works for me.
>> Here is what I am striving to achieve.
>>
>> phraseString =     request.getParameter("phrase");
>> if (phraseString.length() > 0){
>>     phraseQueryString = "\""+phraseString+("\"");
>>     phraseQuery = true;
>>     queryString = phraseQueryString;
>> }
>>
>> if(phraseQuery){
>>     PhraseQuery pQuery = new PhraseQuery();
>>     pQuery.add(new Term("contents", phraseString));
>>     pQuery.setSlop(0);
>>     QueryParser qP = new QueryParser();
>>     query = qP.parse(phraseString);
>> }
> 
> 
> I don't understand what you're attempting in the above.  You don't make 
> a new Term and add it to a PhraseQuery instance with the term having 
> quotes around it - it is QueryParser that deals with quoted strings and 
> interpreting it into a PhraseQuery.
> 
> Are you using pQuery?  Or query?  You've got two Query instances there - 
> query is the one you probably want.
> 
>     Erik
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
> 


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


Re: Phrase Query

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Jul 27, 2004, at 11:42 AM, Hetan Shah wrote:
> Works for me.
> Here is what I am striving to achieve.
>
> phraseString = 	request.getParameter("phrase");
> if (phraseString.length() > 0){
> 	phraseQueryString = "\""+phraseString+("\"");
> 	phraseQuery = true;
> 	queryString = phraseQueryString;
> }
>
> if(phraseQuery){
> 	PhraseQuery pQuery = new PhraseQuery();
> 	pQuery.add(new Term("contents", phraseString));
> 	pQuery.setSlop(0);
> 	QueryParser qP = new QueryParser();
> 	query = qP.parse(phraseString);
> }

I don't understand what you're attempting in the above.  You don't make 
a new Term and add it to a PhraseQuery instance with the term having 
quotes around it - it is QueryParser that deals with quoted strings and 
interpreting it into a PhraseQuery.

Are you using pQuery?  Or query?  You've got two Query instances there 
- query is the one you probably want.

	Erik


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


Re: Phrase Query

Posted by Hetan Shah <He...@Sun.COM>.
Works for me.
Here is what I am striving to achieve.

phraseString = 	request.getParameter("phrase");
if (phraseString.length() > 0){
	phraseQueryString = "\""+phraseString+("\"");
	phraseQuery = true;
	queryString = phraseQueryString;
}

if(phraseQuery){
	PhraseQuery pQuery = new PhraseQuery();
	pQuery.add(new Term("contents", phraseString));
	pQuery.setSlop(0);
	QueryParser qP = new QueryParser();
	query = qP.parse(phraseString);
}

This is piece of the code, what I intend to do is if there is any 
keyword entered in the Exact Phrase field of the form I want to use the 
phrase query other wise use regular Query.

Please correct the code if you'll think it is not correct. I am still 
learning about search and Lucene in general.

thanks.
-H

Erik Hatcher wrote:


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


Re: Phrase Query

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
Let's turn it around.... could you send us your code that is not 
working?

Lucene's test cases show PhraseQuery in action, and working.

	Erik


On Jul 26, 2004, at 4:11 PM, Hetan Shah wrote:

> Hello,
>
> Can someone on the mailing list send me a copy of sample code of how 
> to implement the phrase query for my search. Regular Query is working 
> fine, but the Phrase Query does not seem to work.
>
> TIA,
> -H
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org


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