Lucene.net combines several filters without search queries

How can I filter by multiple fields in Lucene.Net? In one field, I just 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 this?

In addition, I want the user to be able to search without a search query (i.e. just by selecting a category), how can I do this?

+7
full-text-search lucene
source share
1 answer

I think you are asking two questions ...

Question 1: Adding an Additional Filter

Remember, QueryFilter accepts any query (and not just TermQuery ). This way you can create the BooleanQuery criteria you want to filter.

 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: Search without a search query

If the user does not provide a search query, you can search using the MatchAllDocsQuery query.

+12
source share

All Articles