Search for fuzzy phrases Lucene.net

I tried this myself for a considerable period of time and looked around the net, but could not find ANY examples of fuzzy phrase searches through Lucene.NET 2.9.2. (WITH#)

Can anyone advise how to do this in detail and / or provide some sample code - I will seriously seriously appreciate any help since I am completely stuck?

+3
source share
1 answer

I assume that Lucene is working for you, and created a search index with some fields in it. Therefore, suppose 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"; 

After you have all this, you can continue the search query in several fields:

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

You may already have gone so far and you are missing a fuzzy part. I simply add a ~ tilde to each word in queryString to tell Lucene about a fuzzy search for all words in queryString:

 if (fuzzy && !string.IsNullOrEmpty(queryString)) { // first escape the queryString so that eg ~ 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 searches only for terms that end in ~ . This and other useful information was found at http://scatteredcode.wordpress.com/2011/05/26/performing-a-fuzzy-search-with-multiple-terms-through-multiple-lucene-net-document-fields / .

+2
source

Source: https://habr.com/ru/post/972818/


All Articles