Lucene.net 模糊短语搜索

发布于 2024-09-29 14:28:50 字数 141 浏览 4 评论 0原文

我自己已经尝试了相当长的一段时间,并在网络上到处寻找 - 但一直无法找到任何通过 Lucene.NET 2.9.2 进行模糊短语搜索的示例。 (C#)

是否能够建议如何详细执行此操作和/或提供一些示例代码 - 我将非常感谢任何帮助,因为我完全陷入困境?

I have tried this myself for a considerable period and looked everywhere around the net - but have been unable to find ANY examples of Fuzzy Phrase searching via Lucene.NET 2.9.2. ( C# )

Is something able to advise how to do this in detail and/or provide some example code - I would seriously seriously appreciate any help as I am totally stuck ?

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

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

发布评论

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

评论(1

那伤。 2024-10-06 14:28:50

我假设您已经运行 Lucene 并创建了一个包含某些字段的搜索索引。因此,让我们进一步假设:

var fields = ... // a string[] of the field names you wish to search in
var version = Version.LUCENE_29; // your Lucene version
var queryString = "some string to search for";

一旦您拥有了所有这些,您就可以继续在多个字段上定义搜索查询,如下所示:

var analyzer = LuceneIndexProvider.CreateAnalyzer();
var query = new MultiFieldQueryParser(version, fields, analyzer).Parse(queryString);

也许您已经做到了这一点,只是缺少模糊部分。我只是简单地为 queryString 中的每个单词添加波浪线 ~ 来告诉 Lucene 对 queryString 中的所有单词进行模糊搜索:

if (fuzzy && !string.IsNullOrEmpty(queryString)) {
    // first escape the queryString so that e.g. ~ will be escaped
    queryString = QueryParser.Escape(queryString);
    // now split, add ~ and join the queryString back together
    queryString = string.Join("~ ",
        queryString.Split(' ', StringSplitOptions.RemoveEmptyEntries)) + "~";
    // now queryString will be "some~ string~ to~ search~ for~"
}

这里的关键点是 Lucene 使用模糊仅搜索以 ~ 结尾的术语。发现了这个和一些更有用的信息
http://scatteredcode.wordpress.com/2011/05/26/performing-a-fuzzy-search-with-multiple-terms-through-multiple-lucene-net-document-fields/

I assume that you have Lucene running and created a search index with some fields in it. So let's assume further that:

var fields = ... // a string[] of the field names you wish to search in
var version = Version.LUCENE_29; // your Lucene version
var queryString = "some string to search for";

Once you have all of these you can go ahead and define a search query on multiple fields like this:

var analyzer = LuceneIndexProvider.CreateAnalyzer();
var query = new MultiFieldQueryParser(version, fields, analyzer).Parse(queryString);

Maybe you already got that far and are only missing the fuzzy part. I simply add a tilde ~ to every word in the queryString to tell Lucene to do a fuzzy search for all words in the queryString:

if (fuzzy && !string.IsNullOrEmpty(queryString)) {
    // first escape the queryString so that e.g. ~ will be escaped
    queryString = QueryParser.Escape(queryString);
    // now split, add ~ and join the queryString back together
    queryString = string.Join("~ ",
        queryString.Split(' ', StringSplitOptions.RemoveEmptyEntries)) + "~";
    // now queryString will be "some~ string~ to~ search~ for~"
}

The key point here is that Lucene uses fuzzy search only for terms that end with a ~. That and some more helpful info was found on
http://scatteredcode.wordpress.com/2011/05/26/performing-a-fuzzy-search-with-multiple-terms-through-multiple-lucene-net-document-fields/.

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