Judah Gabriel Himango
Judah Gabriel Himango

Reputation: 60051

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?

Upvotes: 1

Views: 2174

Answers (1)

erickson
erickson

Reputation: 269857

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.

Upvotes: 5

Related Questions