Lucene.Net BooleanClause 问题

发布于 2024-09-14 13:27:02 字数 670 浏览 3 评论 0原文

我在使用 Lucene.Net 和 BooleanQuery 时遇到问题。这是我的代码:

 BooleanQuery query = new BooleanQuery();

 String[] types = searchTypes.Split(',');

 foreach (string t in types)
      query.Add(new TermQuery(new Term("document type", t.ToLower())), BooleanClause.Occur.SHOULD);

这基本上应该是一个 OR 语句,它遍历具有某种类型的文档,它可以单独工作。但是,我也有这个查询:

 Query documentTitleQuery = new WildcardQuery(new Term("title", "*" + documentTitle.ToLower() + "*"));
 query.Add(documentTitleQuery, BooleanClause.Occur.MUST);

Which 搜索标题中的单词。这两个查询都可以自行查找。当它们一起使用时,Lucene 似乎将 documentTitleQuery 视为 OR。因此,两个查询一起应该返回特定类型的文档并在标题中包含特定单词,但它返回标题中具有特定单词的所有类型。

I'm having an issue with Lucene.Net and a BooleanQuery. This is my code:

 BooleanQuery query = new BooleanQuery();

 String[] types = searchTypes.Split(',');

 foreach (string t in types)
      query.Add(new TermQuery(new Term("document type", t.ToLower())), BooleanClause.Occur.SHOULD);

This should basically be an OR statement going through documents that have a certain type, which works on its own. However, I also have this query:

 Query documentTitleQuery = new WildcardQuery(new Term("title", "*" + documentTitle.ToLower() + "*"));
 query.Add(documentTitleQuery, BooleanClause.Occur.MUST);

Which searches for words in a title. Both of these queries work find on their own. When they are used together, it seems Lucene is treating the documentTitleQuery as an OR. So both queries together should return documents of a specific type AND contain specific words in the title, but it is returning all types that have specific words in the title.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-09-21 13:27:02

使用另一层布尔查询对两者进行分组:

BooleanQuery topQuery = new BooleanQuery();
...
BooleanQuery query1 = new BooleanQuery();
...
BooleanQuery query2 = new BooleanQuery();
...
topQuery.add(query1, BooleanClause.Occur.MUST);
topQuery.add(query2, BooleanClause.Occur.MUST);

Use one more layer of Boolean query to group both:

BooleanQuery topQuery = new BooleanQuery();
...
BooleanQuery query1 = new BooleanQuery();
...
BooleanQuery query2 = new BooleanQuery();
...
topQuery.add(query1, BooleanClause.Occur.MUST);
topQuery.add(query2, BooleanClause.Occur.MUST);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文