lucene.net combine multiple filters and no search terms

限于喜欢 提交于 2019-12-04 22:47:00

问题


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.



来源:https://stackoverflow.com/questions/1307782/lucene-net-combine-multiple-filters-and-no-search-terms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!