FAQ
I'm new to Lucene. If I use

description = new TermQuery(new Term("description", "my string"));

I ask Lucene to consider "my string" as unique word, right ?

I actually need to consider each word, should I use PhraseQuery instead ? Or
is it correct ?

thanks

Search Discussions

  • Ahmet Arslan at Mar 21, 2011 at 4:55 pm

    description = new TermQuery(new
    Term("description", "my string"));

    I ask Lucene to consider "my string" as unique word, right? Correct.
    I actually need to consider each word, should I use
    PhraseQuery instead ?
    If description field is tokenized/analyzed during indexing you need to use PhraseQuery.

    May you can use QueryParser instead?




    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Patrick Diviacco at Mar 21, 2011 at 5:05 pm


    If description field is tokenized/analyzed during indexing you need to use
    PhraseQuery.
    Uhm yeah I'm using a WhitespaceAnalyzer. This is the code using for
    indexing:

    writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new
    IndexWriterConfig(org.apache.lucene.util.Version.LUCENE_40, new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)));
    ...
    document.add(new Field("description", flickrDoc.getDescription(),
    Field.Store.YES, Field.Index.ANALYZED));
    ...
    writer.addDocument(document);



    May you can use QueryParser instead?
    Why ? Can't I just use PhraseQuery ? What is QueryParser for?
  • Patrick Diviacco at Mar 21, 2011 at 5:40 pm
    One more thing: It is actually not clear to me how to use PhraseQuery... I
    thought I can just pass a phrase to it, but I see only add(Term) method...
    should I parse the string by myself to single terms ?


    On 21 March 2011 18:05, Patrick Diviacco wrote:

    If description field is tokenized/analyzed during indexing you need to use
    PhraseQuery.
    Uhm yeah I'm using a WhitespaceAnalyzer. This is the code using for
    indexing:

    writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new
    IndexWriterConfig(org.apache.lucene.util.Version.LUCENE_40, new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)));
    ...
    document.add(new Field("description", flickrDoc.getDescription(),
    Field.Store.YES, Field.Index.ANALYZED));
    ...
    writer.addDocument(document);



    May you can use QueryParser instead?
    Why ? Can't I just use PhraseQuery ? What is QueryParser for?
  • Ahmet Arslan at Mar 21, 2011 at 9:16 pm

    Date: Monday, March 21, 2011, 7:39 PM
    One more thing: It is actually not
    clear to me how to use PhraseQuery... I
    thought I can just pass a phrase to it, but I see only
    add(Term) method...
    should I parse the string by myself to single terms ?
    Yes, you need to do it.

    QueryParser transforms String into Query.

    http://lucene.apache.org/java/2_9_3/api/core/org/apache/lucene/queryParser/QueryParser.html




    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Patrick Diviacco at Mar 22, 2011 at 7:41 am
    OK, so I'm currently doing this:

    booleanQuery.add(new QueryParser(org.apache.lucene.util.Version.LUCENE_40,
    "tags", new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)).parse(phrase[i]);
    , BooleanClause.Occur.SHOULD);

    I just want to add single terms to my booleanQuery. if I pass a query to the
    add method, am I considering the single terms ?

    thanks


    On 21 March 2011 22:15, Ahmet Arslan wrote:

    Date: Monday, March 21, 2011, 7:39 PM
    One more thing: It is actually not
    clear to me how to use PhraseQuery... I
    thought I can just pass a phrase to it, but I see only
    add(Term) method...
    should I parse the string by myself to single terms ?
    Yes, you need to do it.

    QueryParser transforms String into Query.


    http://lucene.apache.org/java/2_9_3/api/core/org/apache/lucene/queryParser/QueryParser.html




    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Erick Erickson at Mar 22, 2011 at 1:28 pm
    A good habit to develop is to print out the toString() of the assembled
    queries, that'll get you going pretty quickly understanding what the
    query assembly is all about without having to wait for people to respond.

    But the short form is that phrase queries require all the terms to be
    adjacent, which may be what you want but may not.

    BooleanQuery is just asking for all the terms. You should probably
    normalize them.

    Parsing is something like
    Query query = QueryParser.parse("your text here");
    and the returned query is what you pass to the searcher. This latter
    will apply the analysis chains to your input and is probably perferred.

    Best
    Erick

    On Tue, Mar 22, 2011 at 3:41 AM, Patrick Diviacco
    wrote:
    OK, so I'm currently doing this:

    booleanQuery.add(new QueryParser(org.apache.lucene.util.Version.LUCENE_40,
    "tags", new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)).parse(phrase[i]);
    , BooleanClause.Occur.SHOULD);

    I just want to add single terms to my booleanQuery. if I pass a query to the
    add method, am I considering the single terms ?

    thanks


    On 21 March 2011 22:15, Ahmet Arslan wrote:

    Date: Monday, March 21, 2011, 7:39 PM
    One more thing: It is actually not
    clear to me how to use PhraseQuery... I
    thought I can just pass a phrase to it, but I see only
    add(Term) method...
    should I parse the string by myself to single terms ?
    Yes, you need to do it.

    QueryParser transforms String into Query.


    http://lucene.apache.org/java/2_9_3/api/core/org/apache/lucene/queryParser/QueryParser.html




    ---------------------------------------------------------------------
    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
  • Patrick Diviacco at Mar 23, 2011 at 6:51 am
    Your answer is quite clear, but my question is a bit more specific:
    as you seen from my snippet ( I copy it here again), I'm already using
    BooleanQuery and QueryParser.parse method.

    booleanQuery.add(new QueryParser(org.apache.lucene.util.Version.LUCENE_40,
    "tags", new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)).parse(phrase[i]);
    , BooleanClause.Occur.SHOULD);

    I'm however wondering if this is correct way to pass single terms to
    booleanQuery.

    QueryParser.parse method returns a SrndQuery object, which I directly
    passing to booleanQuery.add.

    Not sure if this is correct. Should I extract single terms instead from
    SrndQuery... or something like that, and invoke booleanQuery.add several
    times ?

    thanks




    On 22 March 2011 14:27, Erick Erickson wrote:

    A good habit to develop is to print out the toString() of the assembled
    queries, that'll get you going pretty quickly understanding what the
    query assembly is all about without having to wait for people to respond.

    But the short form is that phrase queries require all the terms to be
    adjacent, which may be what you want but may not.

    BooleanQuery is just asking for all the terms. You should probably
    normalize them.

    Parsing is something like
    Query query = QueryParser.parse("your text here");
    and the returned query is what you pass to the searcher. This latter
    will apply the analysis chains to your input and is probably perferred.

    Best
    Erick

    On Tue, Mar 22, 2011 at 3:41 AM, Patrick Diviacco
    wrote:
    OK, so I'm currently doing this:

    booleanQuery.add(new
    QueryParser(org.apache.lucene.util.Version.LUCENE_40,
    "tags", new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)).parse(phrase[i]);
    , BooleanClause.Occur.SHOULD);

    I just want to add single terms to my booleanQuery. if I pass a query to the
    add method, am I considering the single terms ?

    thanks


    On 21 March 2011 22:15, Ahmet Arslan wrote:

    Date: Monday, March 21, 2011, 7:39 PM
    One more thing: It is actually not
    clear to me how to use PhraseQuery... I
    thought I can just pass a phrase to it, but I see only
    add(Term) method...
    should I parse the string by myself to single terms ?
    Yes, you need to do it.

    QueryParser transforms String into Query.

    http://lucene.apache.org/java/2_9_3/api/core/org/apache/lucene/queryParser/QueryParser.html



    ---------------------------------------------------------------------
    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
  • Patrick Diviacco at Mar 23, 2011 at 7:34 am
    ups, I've just used toString as you suggested and this is what I got:

    *.*:*.* title:Flickrmeetup_01 description:Michael description:R.
    description:Ross tags:rochester tags:ny tags:usa tags:flickrmeetup
    tags:king76 tags:eos350d tags:canon50mmf14 tags:mikros tags:canon tags:ef
    tags:50mm tags:f14 tags:usm tags:canonef50mmf14 tags:canonef50mmf14usm

    I can see the tags field repeated multiple times, so it seems to me
    correctly parsed... correct ?


    On 23 March 2011 07:50, Patrick Diviacco wrote:

    Your answer is quite clear, but my question is a bit more specific:
    as you seen from my snippet ( I copy it here again), I'm already using
    BooleanQuery and QueryParser.parse method.

    booleanQuery.add(new QueryParser(org.apache.lucene.util.Version.LUCENE_40,
    "tags", new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)).parse(phrase[i]);
    , BooleanClause.Occur.SHOULD);

    I'm however wondering if this is correct way to pass single terms to
    booleanQuery.

    QueryParser.parse method returns a SrndQuery object, which I directly
    passing to booleanQuery.add.

    Not sure if this is correct. Should I extract single terms instead from
    SrndQuery... or something like that, and invoke booleanQuery.add several
    times ?

    thanks




    On 22 March 2011 14:27, Erick Erickson wrote:

    A good habit to develop is to print out the toString() of the assembled
    queries, that'll get you going pretty quickly understanding what the
    query assembly is all about without having to wait for people to respond.

    But the short form is that phrase queries require all the terms to be
    adjacent, which may be what you want but may not.

    BooleanQuery is just asking for all the terms. You should probably
    normalize them.

    Parsing is something like
    Query query = QueryParser.parse("your text here");
    and the returned query is what you pass to the searcher. This latter
    will apply the analysis chains to your input and is probably perferred.

    Best
    Erick

    On Tue, Mar 22, 2011 at 3:41 AM, Patrick Diviacco
    wrote:
    OK, so I'm currently doing this:

    booleanQuery.add(new
    QueryParser(org.apache.lucene.util.Version.LUCENE_40,
    "tags", new
    WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)).parse(phrase[i]);
    , BooleanClause.Occur.SHOULD);

    I just want to add single terms to my booleanQuery. if I pass a query to the
    add method, am I considering the single terms ?

    thanks


    On 21 March 2011 22:15, Ahmet Arslan wrote:

    Date: Monday, March 21, 2011, 7:39 PM
    One more thing: It is actually not
    clear to me how to use PhraseQuery... I
    thought I can just pass a phrase to it, but I see only
    add(Term) method...
    should I parse the string by myself to single terms ?
    Yes, you need to do it.

    QueryParser transforms String into Query.

    http://lucene.apache.org/java/2_9_3/api/core/org/apache/lucene/queryParser/QueryParser.html



    ---------------------------------------------------------------------
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupjava-user @
categorieslucene
postedMar 21, '11 at 4:01p
activeMar 23, '11 at 7:34a
posts9
users3
websitelucene.apache.org

People

Translate

site design / logo © 2022 Grokbase