How do I perform an AND search in Lucene.net when multiple words are used in a search?

梦想与她 提交于 2019-12-03 14:38:06

A. If you require all words to be in a document but don't require the words to be consecutive and in the order you specify: The query

+big +red

matches

* the big red fox jumped
* the red big fox jumped
* the big fast red fox jumped

but does not match

* the small red fox jumped

B. If you want to match a phrase (i.e. all words required; the words have to be consecutive and in the order specified) instead: The query

+"big red"

matches

* the big red fox jumped

but does not match

* the red big fox jumped
* the big fast red fox jumped
* the small red fox jumped

What do you get when your query is var query = parser.Parse("+big +abcdefg +test1234"); That should cause the parser to require all terms to be present in matching documents. Another possibility is to construct the query programmatically.

BooleanQuery query = new BooleanQuery();
query.add(new BooleanClause(new TermQuery(new Term("field", "big"))), Occur.MUST);
query.add(new BooleanClause(new TermQuery(new Term("field", "abcdefg"))), Occur.MUST);
query.add(new BooleanClause(new TermQuery(new Term("field", "test1234"))), Occur.MUST);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!