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 Sylvain <sy...@gmail.com> on 2009/02/01 11:33:08 UTC

Set a field as required in a MultiFieldQueryParser

Hello everybody,

I have a search app in which the user can specify in which category the
documents he's searching are. So all my indexed documents have a
"category" field as well as other fields such as title, description,
etc. So when the user enters his query, only the documents that are in
the given category and which match a part of the query must be returned.

So I tried to use a MultiFieldQueryParser with SHOULD clauses on the
title, description, etc and a MUST clause on the category field. It
works pretty well but the problem is that _all_ the documents in the
searched category are returned, even if they don't have any similarity
with the search string (except for the category of course).

I think that's because of the MUST field which takes precedence on the
SHOULD fields or something like that ? I saw in the BooleanQuery the
method setMinimumNumberShouldMatch(). I think I should use this with my
MultiFieldQueryParser to set a minimum of 1 or 2 fields, but this method
only exists for BooleanQuery instances so I can't use it... I thought
about using directly a BooleanQuery but I want to use the FrenchAnalyzer
for the request.

Any idea on how to achieve this ?

Thanks for your help !

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


Re: Set a field as required in a MultiFieldQueryParser

Posted by Sylvain <sy...@gmail.com>.
What I wanted to do was to set at least one "SHOULD" field as required in a
MFQP. Because of the MUST field I had, even if no SHOULD field matched the
query, all the documents which had the corresponding MUST field were
returned. I found a very easy solution to solve my problem ::

BooleanQuery myQuery = (BooleanQuery)myMFQP.parse(queries, fields,
booleanClauses, analyzer)
myQuery.setMinimumNumbersShouldMatch(1)
mySearcher.search(myQuery)

And it worked ! Now my results must have at least one "SHOULD" field that
matches the query.

Thanks for your explanations !

2009/2/1 Erick Erickson <er...@gmail.com>

> I think query.toString() is your friend here. I'm having a hard
> time figuring out, from your description, what you actually want,
> so maybe some examples would help too.
>
> If your users enter terms for each field, then it seems to me that
> you'd want MUST between clauses (in which case MultiFieldQueryParser
> seems unnecesary). That is, if your form is somethinng like
> title: <user enters term>
> description: <user enters another term>
> category: <user enters category>
>
> If you have them enter just a single term, that is your form looks
> something like
> word:<user enters term to find in title OR description OR etc>
> category: <user enters category>
>
>  and want to form a query like
> (title:term OR description:term OR etc:term) AND category:cat, it's not
> clear from the docs how you would create this in a *single* MFQP. But
> you could easily use MFQP to form the (a OR b OR c) clause, add that
> to a BoleanQuery with MUST, and then create your simple query on
> category and add *that* as a second MUST clause in your BooleanQuery
> and use that for your search.
>
> HTH
> Erick
>
>
> On Sun, Feb 1, 2009 at 5:33 AM, Sylvain <sy...@gmail.com>
> wrote:
>
> > Hello everybody,
> >
> > I have a search app in which the user can specify in which category the
> > documents he's searching are. So all my indexed documents have a
> > "category" field as well as other fields such as title, description,
> > etc. So when the user enters his query, only the documents that are in
> > the given category and which match a part of the query must be returned.
> >
> > So I tried to use a MultiFieldQueryParser with SHOULD clauses on the
> > title, description, etc and a MUST clause on the category field. It
> > works pretty well but the problem is that _all_ the documents in the
> > searched category are returned, even if they don't have any similarity
> > with the search string (except for the category of course).
> >
> > I think that's because of the MUST field which takes precedence on the
> > SHOULD fields or something like that ? I saw in the BooleanQuery the
> > method setMinimumNumberShouldMatch(). I think I should use this with my
> > MultiFieldQueryParser to set a minimum of 1 or 2 fields, but this method
> > only exists for BooleanQuery instances so I can't use it... I thought
> > about using directly a BooleanQuery but I want to use the FrenchAnalyzer
> > for the request.
> >
> > Any idea on how to achieve this ?
> >
> > Thanks for your help !
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> > For additional commands, e-mail: java-user-help@lucene.apache.org
> >
> >
>

Re: Set a field as required in a MultiFieldQueryParser

Posted by Erick Erickson <er...@gmail.com>.
I think query.toString() is your friend here. I'm having a hard
time figuring out, from your description, what you actually want,
so maybe some examples would help too.

If your users enter terms for each field, then it seems to me that
you'd want MUST between clauses (in which case MultiFieldQueryParser
seems unnecesary). That is, if your form is somethinng like
title: <user enters term>
description: <user enters another term>
category: <user enters category>

If you have them enter just a single term, that is your form looks
something like
word:<user enters term to find in title OR description OR etc>
category: <user enters category>

 and want to form a query like
(title:term OR description:term OR etc:term) AND category:cat, it's not
clear from the docs how you would create this in a *single* MFQP. But
you could easily use MFQP to form the (a OR b OR c) clause, add that
to a BoleanQuery with MUST, and then create your simple query on
category and add *that* as a second MUST clause in your BooleanQuery
and use that for your search.

HTH
Erick


On Sun, Feb 1, 2009 at 5:33 AM, Sylvain <sy...@gmail.com> wrote:

> Hello everybody,
>
> I have a search app in which the user can specify in which category the
> documents he's searching are. So all my indexed documents have a
> "category" field as well as other fields such as title, description,
> etc. So when the user enters his query, only the documents that are in
> the given category and which match a part of the query must be returned.
>
> So I tried to use a MultiFieldQueryParser with SHOULD clauses on the
> title, description, etc and a MUST clause on the category field. It
> works pretty well but the problem is that _all_ the documents in the
> searched category are returned, even if they don't have any similarity
> with the search string (except for the category of course).
>
> I think that's because of the MUST field which takes precedence on the
> SHOULD fields or something like that ? I saw in the BooleanQuery the
> method setMinimumNumberShouldMatch(). I think I should use this with my
> MultiFieldQueryParser to set a minimum of 1 or 2 fields, but this method
> only exists for BooleanQuery instances so I can't use it... I thought
> about using directly a BooleanQuery but I want to use the FrenchAnalyzer
> for the request.
>
> Any idea on how to achieve this ?
>
> Thanks for your help !
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>