November Happy Hour will be moved to Thursday December 5th.

Episerver find - how to do fuzzy search

Vote:
 

Hi,

I am trying to implement fuzzy search using episerver find.

I have a page that contains the word "Salmon". If i type the word "Salmin" or "Salmo" i get 0 results. If i type the word "Salmon" i get 10 results which is correct.

But i wonder why i do not get any results if i spell lets say just one character wrong.

Below you can see how i query using episerver find and none of the result sets contains any hits when searching for "Salmin":

                var currentSubsite = _contentLoader.Get(new ContentReference(id));
                var unifiedSearch = SearchClient.Instance.UnifiedSearchFor(term).Filter(x =>
                    !x.MatchTypeHierarchy(typeof (PageData)) | ((PageData) x).Ancestors().Match(currentSubsite.ContentLink.ID.ToString()));

                result = unifiedSearch.GetResult(); // Filter(x => x.SearchText.Fuzzy(term)) - MoreLike(term)

                var unifiedSearchMoreLike = SearchClient.Instance.UnifiedSearchFor(term).MoreLike(term);
                var moreLikeResult = unifiedSearchMoreLike.MoreLike(term).GetResult();

                var unifiedSearchFuzzy = SearchClient.Instance.UnifiedSearchFor(term).Filter(x => x.SearchText.Fuzzy(term));
                var fuzzyResult = unifiedSearchFuzzy.GetResult();

                var forResult = SearchClient.Instance.Search().For(term).GetContentResult();

                var unifiedSearchApplyBestBets = SearchClient.Instance.UnifiedSearchFor(term).ApplyBestBets(3);
                var applyBestBetsResult = unifiedSearchApplyBestBets.GetResult();

And some Fuzzy extensions:

    public static class FilterExtensions
    {
        public static DelegateFilterBuilder Fuzzy(this string value, string almost)
        {
            return new DelegateFilterBuilder(field => new QueryFilter(new FuzzyQuery(field, almost)));
        }

        public static DelegateFilterBuilder Fuzzy(this string value, string almost, double minSimilarity)
        {
            return new DelegateFilterBuilder(field => new QueryFilter(new FuzzyQuery(field, almost) { MinSimilarity = minSimilarity }));
        }
    }

In Episerver find I have seen terms like "ApplyBestBets", "MoreLike", "For" and there is also a "FuzzyQuery". To honest i am a bit confused on what extactly implements fuzzy search. I must be missing something here. Does anyone know what i might do wrong? How to add fuzzy search

#172491
Dec 05, 2016 12:47
Vote:
 

If you return spelling suggestions, do you get salmon there?

#173265
Dec 19, 2016 0:06
Vote:
 

Thanks for replying.

How do i return spelling suggestions?

I actually managed to get it working using this filter i implemented:

public static ITypeSearch<T> FuzzyFilter<T>(
              this ITypeSearch<T> search,
              Expression<Func<T, string>> fieldSelector,
              string almost,
              double? minSimilarity = null,
              double? boost = null)
        {
            var fieldName = search.Client.Conventions
                .FieldNameConvention
                .GetFieldNameForAnalyzed(fieldSelector);

            var wildcardQuery = new FuzzyQuery(fieldName, almost.ToLowerInvariant())
            {
                MinSimilarity = minSimilarity,
                Boost = boost
            };

            //Add it to the search request body
            return new Search<T, WildcardQuery>(search, context =>
            {
                if (context.RequestBody.Query != null)
                {
                    var boolQuery = new BoolQuery();
                    boolQuery.Should.Add(context.RequestBody.Query);
                    boolQuery.Should.Add(wildcardQuery);
                    boolQuery.MinimumNumberShouldMatch = 1;
                    context.RequestBody.Query = boolQuery;
                }
                else
                {
                    context.RequestBody.Query = wildcardQuery;
                }
            });
        }
#173352
Dec 21, 2016 14:43
Vote:
 

Hi Jeppe,

What version of Find/CMS are you using? I had found that same extension for Wildcard search and it was working awesome. At some point after upgrading to latest version of Find, it stopped working. It just returned everything regardless of the query. I am not sure what caused it to stop working. It could have been totally unrelated to the FInd upgrade. I am curious as to what version you are on, as I may be able to rule out the upgrade as the cause of my issue.

Thanks!

John

#173360
Dec 21, 2016 18:38
Vote:
 

Hi John,

Sounds very strange.

My versions are:

  • Episerver Find - 12.2.4.0
  • Episerver CMS - 9.12.2

Best regards,

Jeppe

#173366
Dec 22, 2016 8:04
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.