aneuryzm
aneuryzm

Reputation: 64864

Lucene: build a query by tokenizing a string and pass

I need to extract single terms from a string to build a query using BooleanQuery. I'm using QueryParser.parse() method for it, this is my code snippet:

booleanQuery.add(
    new QueryParser(
            org.apache.lucene.util.Version.LUCENE_40, 
            "tags", 
            new WhitespaceAnalyzer(org.apache.lucene.util.Version.LUCENE_40)
    ).parse("tag1 tag2 tag3"), 
    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 pass to booleanQuery.add() method.

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

Update: printed query

*.*:*.* 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

Upvotes: 2

Views: 1333

Answers (1)

Yuval F
Yuval F

Reputation: 20621

I believe you should extract the tokens, wrap each one in a Term, then create a TermQuery for it, then add the TermQuery to the BooleanQuery. SrndQuery is abstract anyway, so I guess your current code would create an instance of a subclass, which is probably not what you mean to do. You may want to create your own custom QueryParser for this.

Upvotes: 1

Related Questions