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 Andre Rubin <an...@gmail.com> on 2008/08/25 17:32:31 UTC

MultiPhrase search

Hi all,

Let's say that I have in my index the value "One Two Three" for field 'A'.
I'm using a custom analyzer that is described in the forwarded message.

My Search query is built like this:

    QueryParser parser = new QueryParser(LABEL_FIELD, ANALYZER);
    Query query = parser.parse(LABEL_FIELD + ":" + searchQuery);

So when I search for

1) On*

I get a match with "One Two Three"

But when I search for

2) One Tw*

I get no matches. query.toString outputs:

3) label:One label:Tw*

which is obviously wrong. If I surround my search strng with ":

4) "One Tw*"

query.toString outputs what I was expecting:

5) label:One Tw*

but I still get no matches, and now even the first search (1) doesn't work.

Looking through the API I found this MultiPhraseQuery, but the doc was very
confusing. I tried it out but with no luck (I think I did it wrong). In any
case, is MultiPhraseQuery what I'm looking for? If it is, how should I use
the MultiPhraseQuery class?

Thanks,


Andre


---------- Forwarded message ----------
From: Andre Rubin <an...@gmail.com>
Date: Thu, Aug 21, 2008 at 2:21 AM
Subject: Re: Case Sensitivity
To: java-user@lucene.apache.org


Just to add to that, as I said before, in my case, I found more useful not
to use UN_Tokenized. Instead, I used Tokenized with a custom analyzer that
uses the KeywordTokenizer (entire input as only one token) with the
LowerCaseFilter: This way I get the best of both worlds.

public class KeywordLowerAnalyzer extends Analyzer {

    public KeywordLowerAnalyzer() {
    }


    public TokenStream tokenStream(String fieldName, Reader reader) {
        TokenStream result = new KeywordTokenizer(reader);
        result = new LowerCaseFilter(result);
        return result;
    }

}

Re: MultiPhrase search

Posted by Andre Rubin <an...@gmail.com>.
Thanks again Daniel,

It's working now. But for some reason, TermQuery is not working for me (i
think because I have special characters in the query). I replaced the
TermQuery with the query below and I got the results I was expecting.

Thanks!!!!

String escapedType = QueryParser.escape(type);
QueryParser parser = new QueryParser(TYPE_FIELD, ANALYZER);
Query tq = parser.parse(TYPE_FIELD + ":" + escapedType);


Andre

On Tue, Aug 26, 2008 at 10:19 AM, Daniel Naber <
lucenelist2007@danielnaber.de> wrote:

> On Dienstag, 26. August 2008, Andre Rubin wrote:
>
> > Now I was the one who didn't follow: How do I add a query to an existing
> > query?
>
> Something like this should work:
>
> BooleanQuery bq = new BooleanQuery();
> PrefixQuery pq = new PrefixQuery(...);
> bq.add(pq, BooleanClause.Occur.MUST);
> TermQuery tq = new TermQuery(new Term("type", "sequence"));
> bq.add(tq, BooleanClause.Occur.MUST);
>
> bq is now the query you can use for searching. BooleanClause.Occur.MUST
> basically means "AND". So unless you want your users to access the
> features of Lucene's QueryParser (e.g. AND, OR, NOT, fuzzy queries etc)
> there's no need to use it.
>
> 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: MultiPhrase search

Posted by Daniel Naber <lu...@danielnaber.de>.
On Dienstag, 26. August 2008, Andre Rubin wrote:

> Now I was the one who didn't follow: How do I add a query to an existing
> query?

Something like this should work:

BooleanQuery bq = new BooleanQuery();
PrefixQuery pq = new PrefixQuery(...);
bq.add(pq, BooleanClause.Occur.MUST);
TermQuery tq = new TermQuery(new Term("type", "sequence"));
bq.add(tq, BooleanClause.Occur.MUST);

bq is now the query you can use for searching. BooleanClause.Occur.MUST 
basically means "AND". So unless you want your users to access the 
features of Lucene's QueryParser (e.g. AND, OR, NOT, fuzzy queries etc) 
there's no need to use it.

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: MultiPhrase search

Posted by Andre Rubin <an...@gmail.com>.
Now I was the one who didn't follow: How do I add a query to an existing
query?

Let me be more clear on my use case:

I have two documents:
1)
label:One Two Three
type:sequence

2)
label:One Two FOUR
type:other


I want to be able to make the same kind of search as you described earlier
using PrefixQuery, but I want to filter the results using the other field.
Like:
label:One Tw*
type:sequence

And I only want to get document 1 as result.

At first glance, it seems that your answer may fit, but I'm not sure what
you meant by adding another query to an existing query.

Thanks again,


Andre

On Tue, Aug 26, 2008 at 9:34 AM, Daniel Naber <lucenelist2007@danielnaber.de
> wrote:

> On Dienstag, 26. August 2008, Andre Rubin wrote:
>
> > I just have one more use case. I want the same prefix search as before,
> > plus another match in another field.
>
> Not sure if I'm following you, but you can create your own BooleanQuery
> programmatically, and then add the original PrefixQuery and any other
> query, no matter if it's another BooleanQuery or if comes out of the
> QueryParser or is also built programmatically.
>
> 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: MultiPhrase search

Posted by Daniel Naber <lu...@danielnaber.de>.
On Dienstag, 26. August 2008, Andre Rubin wrote:

> I just have one more use case. I want the same prefix search as before,
> plus another match in another field.

Not sure if I'm following you, but you can create your own BooleanQuery 
programmatically, and then add the original PrefixQuery and any other 
query, no matter if it's another BooleanQuery or if comes out of the 
QueryParser or is also built programmatically.

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: MultiPhrase search

Posted by Andre Rubin <an...@gmail.com>.
That worked great! Thanks Daniel.

I just have one more use case. I want the same prefix search as before, plus
another match in another field.

I was using MultiFieldQueryParser.parse(), but then I have the same problem
with the "One Tw*" query, cause MultiFieldQueryParser.parse() returns a
BooleanQuery.

Thanks again,


Andre

On Tue, Aug 26, 2008 at 2:37 AM, Daniel Naber <lucenelist2007@danielnaber.de
> wrote:

> On Dienstag, 26. August 2008, Andre Rubin wrote:
>
> > For some reason, the TermQuery is not returning any results, even when
> > querying for a single word (like on*).
>
> Sorry, I meant PrefixQuery. Also, do not add the "*" to the search string
> when creating the PrefixQuery.
>
> 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: MultiPhrase search

Posted by Daniel Naber <lu...@danielnaber.de>.
On Dienstag, 26. August 2008, Andre Rubin wrote:

> For some reason, the TermQuery is not returning any results, even when
> querying for a single word (like on*).

Sorry, I meant PrefixQuery. Also, do not add the "*" to the search string 
when creating the PrefixQuery.

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: MultiPhrase search

Posted by Andre Rubin <an...@gmail.com>.
For some reason, the TermQuery is not returning any results, even when
querying for a single word (like on*).

query = new TermQuery(new Term(LABEL_FIELD, searchString));

On 8/25/08, Daniel Naber <lu...@danielnaber.de> wrote:
> On Montag, 25. August 2008, Andre Rubin wrote:
>
>>  I tried it out but with no luck (I think I did it wrong). In any
>> case, is MultiPhraseQuery what I'm looking for? If it is, how should I
>> use the MultiPhraseQuery class?
>
> No, you won't need it. If you know that the field is not really tokenized
> (no matter whether by using UN_TOKENIZED or KeywordAnalyzer) you should
> create a TermQuery directly, without using the QueryParser. The
> QueryParser interprets AND, OR, and NOT, and a space is just a default
> for "OR", so using KeywordAnalyzer won't have the desired effect.
>
> 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
>
>

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


Re: MultiPhrase search

Posted by Daniel Naber <lu...@danielnaber.de>.
On Montag, 25. August 2008, Andre Rubin wrote:

>  I tried it out but with no luck (I think I did it wrong). In any
> case, is MultiPhraseQuery what I'm looking for? If it is, how should I
> use the MultiPhraseQuery class?

No, you won't need it. If you know that the field is not really tokenized 
(no matter whether by using UN_TOKENIZED or KeywordAnalyzer) you should 
create a TermQuery directly, without using the QueryParser. The 
QueryParser interprets AND, OR, and NOT, and a space is just a default 
for "OR", so using KeywordAnalyzer won't have the desired effect.

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