在 Lucene.Net 中,如何指定特定字段的术语列表中的任何一个匹配?
在 Lucene.Net 中,我需要创建一个查询来将搜索限制为特定字段的特定术语列表。
例如,我需要“AutoMake”为“Ford”或“Chevy”。因此,只要“AutoMake”的值是这两者之一,我们就可以了。
这是我形成 BooleanQuery 的方式,但我认为这是不对的:
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);
那里的“MUST”有效地“AND”查询,因此“AutoMake”必须两者“Ford”和“雪佛兰”,这是错误的。 (我已经阅读了一些有关 BooleanClause“SHOULD”的内容,但我认为这也不正确。)
本质上,我需要一个用于“OR”的 BooleanClause 运算符。我需要一种方法来封装两个查询,以便如果其中一个查询为真,则整个查询为真? (因为,我有多个这样的查询作为子句输入到一个更大的查询中。)
这就是很棒的。
var query = new AnyOfTheseTermsQuery();
query.Add(new Term("AutoMake", "Ford"));
query.Add(new Term("AutoMake", "Chevy"));
这就是我正在寻找的。这存在吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为“SHOULD”是此类查询的正确运算符。设置一个小索引来测试这一点可能是值得的,但是当我查看文档时,它似乎正是您所追求的。
祝你好运,
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,
是的,将这 2 个术语添加到带有 SHOULD 子句的 BooleanQuery 中。这意味着查询将查找这些术语中的任何一个。然后将该查询添加到带有 MUST 子句的父 BooleanQuery 中,这意味着子查询必须与文档匹配。
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.