简体   繁体   中英

In Lucene.Net, How do I specify a match on any one of a list of terms for a specific field?

In Lucene.Net, I need to create a query to limit a search to a specific list of terms for a specific field.

For example, I need "AutoMake" to be "Ford" or "Chevy." So, as long as the value of "AutoMake" is one of those two, we're good.

Here's how I'm forming my BooleanQuery, but I don't think this is right:

var query1 = new TermQuery(new Term("AutoMake", "Ford"));
var query2 = new TermQuery(new Term("AutoMake", "Chevy"));

var thisFilter = new BooleanQuery();
thisFilter.Add(query1, BooleanClause.Occur.MUST);
thisFilter.Add(query2, BooleanClause.Occur.MUST);

The "MUST" there effectively "ANDs" the query, so "AutoMake" has to be both "Ford" and "Chevy," which is wrong. (I've done some reading on the BooleanClause "SHOULD," but I don't think that's right either.)

Essentially, I need a BooleanClause operator for "OR." I need a way to encapsulate two queries so that if one of them is true, then the entire query is true? (Because, I have multiple of these queries feeding into a larger query as clauses.)

Here's what would be awesome.

var query = new AnyOfTheseTermsQuery();
query.Add(new Term("AutoMake", "Ford"));
query.Add(new Term("AutoMake", "Chevy"));

That's what I'm looking for. Does this exist?

I think "SHOULD" is the correct operator for this type of query. It's probably worth your while setting up a small index to test this, but when I look at the documentation, it seems to be exactly what you're after.

Good luck,

Yes add these 2 terms to a BooleanQuery with a SHOULD clause. That will mean that the query will look for either of those terms. Then add that query to the parent BooleanQuery with a MUST clause, meaning that the sub query must match a document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM