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 Chris Bamford <ch...@scalix.com> on 2008/07/03 15:38:57 UTC

Search question (newbie)

Hi,

Can someone point me in the right direction please?
How can I trap this situation correctly?  I receive user queries like 
this (quotes included):

    /from:"fred flintston*"/

Which produces a query string of

    /+from:fred  body:flintston/       (where /body/ is the default field)

What I want is:

/    +from:fred +from:flintston*/

In other words, I want quoted expressions to be treated as single units..
Thanks for any pointers,

- Chris


Re: Search question (newbie)

Posted by Chris Hostetter <ho...@fucit.org>.
: How can I trap this situation correctly?  I receive user queries like this
: (quotes included):
: 
:    /from:"fred flintston*"/
: 
: Which produces a query string of
: 
:    /+from:fred  body:flintston/       (where /body/ is the default field)

either you've left something out, or some aspect of your situation is 
broken ... passing the string you describe -- including the quotes -- to 
QueryParser will not create an optional clause for body:flinston ... it 
will create a PhraseQuery consisting of whatever Terms are produced by 
your analyzer for the (unquoted) string...
	 fred flinston*

...note that the "*" is included in the input passed to your analyzer, 
because it has no special meaning inside quotes.

: What I want is:
: 
: /    +from:fred +from:flintston*/

that would require both fred, and something starting with flinston, but it 
wouldn't require them to be close together -- you can achieve that by 
overriding QueryParser.getFieldQuery(String,String).  inspect the 
termText, if it contains something that looks prefix/wildcardish, then 
split the input to get what you want, and delegate to 
QueryParser.getPrefixQuery or getWildCardQuery and then combine up your 
parts into a BooleanQuery.

Or... if you want to take it one step further and require that the clauses 
are near eachother, you can construct a MultiPhraseQuery -- but you'll 
need to have access to an IndexReader in your QueryParser so you can 
expand the prefix/wildcard into it's Terms.


-Hoss


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


RE: Search question (newbie)

Posted by John Griffin <jg...@thebluezone.net>.
Chris,

Yes, when there are multiple types of queries involved (Term & Wildcard in
this case) that's how I handle it. A disadvantage of this is that the user
input must be somewhat restricted since you need to know what's coming.

An important thing to remember is that Wildcard, Prefix and one other I
can't remember at the moment (sorry :>() are not passed through an analyzer.
So, the QueryParser's analyzer has no effect on them.

John G.


-----Original Message-----
From: Chris Bamford [mailto:chris.bamford@scalix.com] 
Sent: Friday, July 04, 2008 6:10 AM
To: java-user@lucene.apache.org
Subject: Re: Search question (newbie)

John,

Thanks, I think I'm getting this now....  So you created your own 
BooleanQuery and parsed the string yourself, adding strings as  
TermQuerys etc., rather than using a QueryParser ?

Cheers,

- Chris

John Griffin wrote:
> Chris,
>
> I've had similar requirements in the past. First strip the quotes then
> create a BooleanQuery consisting of two separate queries.
>
> 1. TermQuery for the first term - Fred
> 2. PrefixQuery for the second term - Flintstone
>
> When you add each individual query to the BooleanQuery make sure the
> BooleanClause.Occur parameter is set to MUST (look at the BooleanQuery API
> docs). 
>
> Use the toString() method on the BooleanQuery after it's created to make
> sure you did it correctly.
>
> John G.
>
> -----Original Message-----
> From: Chris Bamford [mailto:chris.bamford@scalix.com] 
> Sent: Thursday, July 03, 2008 7:39 AM
> To: java-user@lucene.apache.org
> Subject: Search question (newbie)
>
> Hi,
>
> Can someone point me in the right direction please?
> How can I trap this situation correctly?  I receive user queries like 
> this (quotes included):
>
>     /from:"fred flintston*"/
>
> Which produces a query string of
>
>     /+from:fred  body:flintston/       (where /body/ is the default field)
>
> What I want is:
>
> /    +from:fred +from:flintston*/
>
> In other words, I want quoted expressions to be treated as single units..
> Thanks for any pointers,
>
> - Chris
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>   


-- 
------------------------------------------------------------------------
*Chris Bamford*
Senior Development Engineer 	<http://www.scalix.com>
------------------------------------------------------------------------
/Email / MSN/ 	chris.bamford@scalix.com
/Tel/ 	+44 (0)1344 381814 	  	/Skype/ 	c.bamford


---------------------------------------------------------------------
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: Search question (newbie)

Posted by Chris Bamford <ch...@scalix.com>.
John,

Thanks, I think I'm getting this now....  So you created your own 
BooleanQuery and parsed the string yourself, adding strings as  
TermQuerys etc., rather than using a QueryParser ?

Cheers,

- Chris

John Griffin wrote:
> Chris,
>
> I've had similar requirements in the past. First strip the quotes then
> create a BooleanQuery consisting of two separate queries.
>
> 1. TermQuery for the first term - Fred
> 2. PrefixQuery for the second term - Flintstone
>
> When you add each individual query to the BooleanQuery make sure the
> BooleanClause.Occur parameter is set to MUST (look at the BooleanQuery API
> docs). 
>
> Use the toString() method on the BooleanQuery after it's created to make
> sure you did it correctly.
>
> John G.
>
> -----Original Message-----
> From: Chris Bamford [mailto:chris.bamford@scalix.com] 
> Sent: Thursday, July 03, 2008 7:39 AM
> To: java-user@lucene.apache.org
> Subject: Search question (newbie)
>
> Hi,
>
> Can someone point me in the right direction please?
> How can I trap this situation correctly?  I receive user queries like 
> this (quotes included):
>
>     /from:"fred flintston*"/
>
> Which produces a query string of
>
>     /+from:fred  body:flintston/       (where /body/ is the default field)
>
> What I want is:
>
> /    +from:fred +from:flintston*/
>
> In other words, I want quoted expressions to be treated as single units..
> Thanks for any pointers,
>
> - Chris
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>   


-- 
------------------------------------------------------------------------
*Chris Bamford*
Senior Development Engineer 	<http://www.scalix.com>
------------------------------------------------------------------------
/Email / MSN/ 	chris.bamford@scalix.com
/Tel/ 	+44 (0)1344 381814 	  	/Skype/ 	c.bamford


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


RE: Search question (newbie)

Posted by John Griffin <jg...@thebluezone.net>.
Chris,

I've had similar requirements in the past. First strip the quotes then
create a BooleanQuery consisting of two separate queries.

1. TermQuery for the first term - Fred
2. PrefixQuery for the second term - Flintstone

When you add each individual query to the BooleanQuery make sure the
BooleanClause.Occur parameter is set to MUST (look at the BooleanQuery API
docs). 

Use the toString() method on the BooleanQuery after it's created to make
sure you did it correctly.

John G.

-----Original Message-----
From: Chris Bamford [mailto:chris.bamford@scalix.com] 
Sent: Thursday, July 03, 2008 7:39 AM
To: java-user@lucene.apache.org
Subject: Search question (newbie)

Hi,

Can someone point me in the right direction please?
How can I trap this situation correctly?  I receive user queries like 
this (quotes included):

    /from:"fred flintston*"/

Which produces a query string of

    /+from:fred  body:flintston/       (where /body/ is the default field)

What I want is:

/    +from:fred +from:flintston*/

In other words, I want quoted expressions to be treated as single units..
Thanks for any pointers,

- Chris



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