没有运算符的 Lucene 查询

发布于 2024-08-15 23:54:29 字数 277 浏览 5 评论 0原文

我正在尝试使用 Lucene 在数据库中搜索姓名。然而,有些名称包含“NOT”和“OR”等单词,甚至包含“-”减号。我仍然希望使用分析器分解名称中的不同标记,并作为术语的布尔组合进行搜索,但我不希望 Lucene 将任何“NOT”/“OR”术语解释为运算符(相反,我希望它们像普通术语一样被搜索)。

完成我所说的一种方法是在搜索查询上手动运行分析器,然后根据所有生成的标记手动构建布尔查询。这是最好的方法吗?我的印象是分析器被设计为与查询解析器结合使用,我觉得应该有一种内置的方法来完成我想要做的事情。有人知道最好的方法吗?

I am trying to use Lucene to search for names in a database. However, some of the names contain words like "NOT" and "OR" and even "-" minus symbols. I still want the different tokens inside the names to be broken up using an Analyzer and searched upon as a boolean combination of terms, but I do not want Lucene to interpret any of the "NOT"/"OR" terms as operators (instead I want them to be searched upon like normal terms).

One way to accomplish what I am talking about would be to manually run the Analyzer on the search query and then manually construct a boolean query based on all the resulting tokens. Is this the best way? I get the impression that analyzer's were designed to be used in conjunction with the query parser and I feel like there should be a built-in way to accomplish what I am trying to do. Anyone know the best way to do this?

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

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

发布评论

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

评论(1

ゃ人海孤独症 2024-08-22 23:54:29

您自己建议的从 TokenStream 构造 BooleanQuery 的方法是完全有意义的。 QueryParser API 实际上只是用于使用特定语法解析结构化查询 - 如果您不利用查询解析器语法,我认为没有理由使用 QueryParser 而不是手动构建的 BooleanQuery。

但是,如果您使用 StandardAnalyzer(或其他带有 StopFilter 的分析器)来索引字段,则“AND”、“NOT”和“OR”等单词将不会被索引,并且无法进行搜索。因此,在这种情况下,您可以使用正则表达式轻松地从查询中删除这些单词和运算符,例如“-”和“+”。不过,我更愿意推荐 BooleanQuery 方法。

Your own suggested approach of constructing a BooleanQuery from a TokenStream makes complete sense. The QueryParser API is really just intended for parsing structured queries using a specific syntax - if you are not leveraging the query parser syntax, I see no reason to use QueryParser over a manually constructed BooleanQuery.

However, if you are using a StandardAnalyzer (or another analyzer with a StopFilter) to index your fields, words like "AND", "NOT" and "OR" will not be indexed, and cannot be searched on. So in that case you could just as easily strip those words and operators like "-" and "+" from your queries using a regular expression. I would sooner recommend the BooleanQuery approach, however.

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