1

In my application I have a Company, with the name field of This is a test, which is correctly being indexed by Lucene.Net. For reference, my MultiFieldQueryParser has its default operator set to QueryParser.Operator.AND.

My search passes when I search for this test~ and this tst~. However, my search fails when I attempt to search for this~ test~, thas~ test~, thas test~, and other variations.

This whole purpose is to allow the user to misspell their search a bit, so if the user searches for Jon Doe it will still show results for John Doe, allowing users to not remember exact spelling of things they entered in the database. Unfortunately, it seems like it is only allowing fuzzy searches on the last term in the search phrase. Am I doing something wrong, or do I need to use a whole separate Analyzer in order to do this?

1 Answer 1

5

I recently had to implement something similar on my project.

I ended up splitting-up phrase into multiple segments and constructing the query manually.

var input = "This is a test";

var fieldName = "yourField";
var minimumSimilarity = 0.5f;
var prefixLength = 3;
var query = new BooleanQuery();

var segments = input.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
foreach (var segment in segments)
{
    var term = new Term(fieldName, segment);
    var fuzzyQuery = new FuzzyQuery(term, minimumSimilarity, prefixLength);
    query.Add(fuzzyQuery, BooleanClause.Occur.SHOULD);
}

Very primitive, I know, but appears to work.

Note: this has only been tested against Lucene.net v2.3.1.3

3
  • 1
    Note: This works best if you use the WhitespaceAnalyzer at index-time, many other analyzers will lower-case your data, so a search for "This" wont match anything.
    – sisve
    Commented May 26, 2011 at 3:57
  • 1
    In my case there was no requirement for the search to be case-sensitive, so I generate search Terms using StandardAnalyzer.TokenStream(string fieldName, TextReader reader) Commented May 26, 2011 at 8:49
  • For Java, it will be query.add(new BooleanClause(fuzzyQuery, Occur.SHOULD)) on the last line
    – Duc Tran
    Commented Nov 20, 2013 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.