lucene.net combine multiple filters and no search

2020-06-16 09:12发布

问题:

How can I do a Filter across multiple fields in Lucene.Net? On one field I simply do:

TermQuery tool = new TermQuery(new Term("Tool", "Nail"));
Filter f = new QueryFilter(tool);

If I now wanted to add a nail length to the filter, how can I do that?

Also, I want the user to be a able to do a search with no search term (i.e. by just choosing a category) how can I do that?

回答1:

I think you're asking two questions...

Question 1: Adding an additional filter

Remember, QueryFilter accepts any query (not just TermQuery). Therefore, you can create a BooleanQuery of the criteria you wish to filter on.

TermQuery toolQuery = new TermQuery(new Term("Tool", "Nail"));
TermQuery nailLengthQuery = new TermQuery(new Term("NailLength", "3 inches"));

BooleanQuery filterQuery = new BooleanQuery();
filterQuery.add(toolQuery, BooleanClause.Occur.MUST);
filterQuery.add(nailLengthQuery, BooleanClause.Occur.MUST);

Filter f = new QueryFilter(filterQuery);

Question 2: Searching without a search term

If the user provides no search term, you can search using a MatchAllDocsQuery query.