Lucene.net Fuzzy Phrase Search

跟風遠走 提交于 2019-12-05 22:29:10

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/.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!