Thursday, April 14, 2011

Lucene search with complex query

Here's what I want to do, using pseudo-code:

lucene.Find((someField == "bar" || someField == "baz") && anotherField == "foo");

Or in English, "find all documents where someField is 'bar' or 'baz', and where anotherField is 'foo'".

How can I do a query like this with Lucene?

From stackoverflow
  • In Lucene query syntax:

    +(someField:bar someField:baz) +anotherField:foo
    

    The "+" means that the term is required, just like Google search syntax. The parentheses group terms to act like a single term. Without a "+" (or "-"), a term is optional; at least one of the terms has to match, and the more terms that match, the higher the score.

    Pass this string to the QueryParser to create a Query object. The query can then be passed to one of several search methods, depending on your needs.

    Judah Himango : Thanks! I'll give that a try. In the meantime, I'll mark this as the correct answer. I'll let you know if it works as expected.
    Judah Himango : Seems to work great. Thanks again.

0 comments:

Post a Comment