Is Examine Fuzzy working in Umbraco 10?
# help-with-umbraco
d
I have migrated a umbraco v8 site to v10. The following search query is not working anymore i.e it is not returning any results: public ISearchResults GetRelatedItemsForPage(int siteId, string docUrl, int maxResults = 3) { ISearchResults results = null; try { IExamineManager examineManager = StaticServiceProvider.Instance.GetService(); if (examineManager.TryGetIndex("ExternalIndex", out var index)) { var query = index.Searcher.CreateQuery("content") .Field("relatedProfiles", docUrl.Fuzzy()) .And() .Field("searchPath", siteId.ToString()).And() .RangeQuery("displayDateForSorting".Split(','), DateTime.Now.AddYears(-2).Ticks, null) .OrderByDescending(new Examine.Search.SortableField("displayDateForSorting", SortType.Long)) .OrderByDescending(new Examine.Search.SortableField("updateDate", SortType.Long)); results = query.Execute(new QueryOptions(0, maxResults)); } } catch (Exception ex) { } return results; } If I remove fuzzy from the query it returns me some results. Anybody got Fuzzy working in umbraco 10? Any examine fuzzy related documentation?
d
Hi there, for readability, you can wrap your code sample inside triple backticks like so:
Copy code
csharp
public ISearchResults GetRelatedItemsForPage(int siteId, string docUrl, int maxResults = 3)
        {
            ISearchResults results = null;

            try
            {
                IExamineManager examineManager = StaticServiceProvider.Instance.GetService<IExamineManager>();

                if (examineManager.TryGetIndex("ExternalIndex", out var index))
                {
                    var query = index.Searcher.CreateQuery("content")
                        .Field("relatedProfiles", docUrl.Fuzzy())
                        .And()
                        .Field("searchPath", siteId.ToString()).And()
                        .RangeQuery<long>("displayDateForSorting".Split(','), DateTime.Now.AddYears(-2).Ticks, null)
                        .OrderByDescending(new Examine.Search.SortableField("displayDateForSorting", SortType.Long))
                        .OrderByDescending(new Examine.Search.SortableField("updateDate", SortType.Long)); 

                    results = query.Execute(new QueryOptions(0, maxResults));
                }
            }
            catch (Exception ex)
            {

            }

            return results;
        }
Just start with \``` and end with \```
That said, yes I did manage to get fuzzy search working myself. I notice that you haven't specified the amount of fuzzyness, have you tried specifying your fuzzyness? I usually use a value of 0.7, which seems to work very well.
m
should default to 0.5f...
Copy code
csharp
        public static IExamineValue Fuzzy(this string s)
        {
            return Fuzzy(s, 0.5f);
        }
https://github.com/Shazwazza/Examine/blob/release/3.0/src/Examine.Core/SearchExtensions.cs#L44-L47
you can also check your query by getting the raw value from
query
at run time and see what's been generated, and then test in the backoffice or luke.. You can also update the
LuceneDirectoryIndexOptions
for those sortable fields, and then you don't need to mess converting t o
Ticks
or specifying
SortType.Long
in the sortableFields. (don't forget to rebuild the index via the backoffice for changes to take effect) So it becomes
query.And().RangeQuery<DateTime>(new[] { "displayDateForSorting"}, min: DateTime.Now.AddYears(-2), max: null);
and
Copy code
csharp
query.OrderByDescending(new SortableField("displayDateForSorting"));
query.OrderByDescending(new SortableField("updateDate"));
d
Hi @D_Inventor thanks for pointing out to enclose code within triple backticks. I will keep this in mind. Regarding the fuzzy search , I tried adding the value 0.7f , but that did not fetch me any results. Not sure why its not working for me. I will try @Mike Chambers recommendation. Do let me know if you have any other suggestions.
Hi @D_Inventor @Mike Chambers , I tried debugging my umbraco v7 site and the query it generates is
Copy code
Category: "content", LuceneQuery: {+relatedProfiles:umb://document/229816e0e6314de79aed1b8943b91637~0.5 +searchPath:1433 +(displayDateForSorting:[637652963658635555 TO *])}
However the query generated by umbraco v10 site is
Copy code
Category: "content", LuceneQuery: {+relatedProfiles:umb://document/229816e0e6314de79aed1b8943b91637~2 +searchPath:1433 +(displayDateForSorting:[637652969796493318 TO *])}
As can be seen the fuzzy value is different. Is that the problem? But why is it giving us a different value of 2? Any thoughts?
d
That likely is the problem, yeah. I don't know why it might set the value to 2 though. I would also expect it to set the value to 0.5
n
Not sure why fuzzy would be used here. It looks to be trying to fuzzy match on a udi string, which doesn't really make sense
d
Found something here https://github.com/Shazwazza/Examine/issues/252. Im not an expert in examine/lucene search, but after reading that, does it mean that fuzzy search is no longer supported in lucene.NET v4?
If I dont use Fuzzy, this is what it results
Copy code
Category: "content", LuceneQuery: {+(relatedProfiles:umb relatedProfiles:document relatedProfiles:229816e0e6314de79aed1b8943b91637) +searchPath:1433 +(displayDateForSorting:[637652991549190507 TO *])}
d
Ah, I see. As I understand it, Lucene has changed how fuzzy works. It still supports fuzzy, but has set a limit. That is: the 2 in your query comes from the so-called "edit distance", which means that in the newer versions, you can only find values that are at most 2 characters different.
So the number 2 was correct, but I'm guessing that you don't have 50% similarity anymore, but rather up to 2 characters dissimilarity
I didn't know that myself to be honest
n
Fuzzy behavior change is interesting. However, the problem here is that it makes no sense at all to fuzzy match the udi. I assume this method had the url of the page passed in prior, rather than the udi.
d
Yes removed fuzzy search and got it to work by using the below code:
Copy code
public ISearchResults GetRelatedItemsForPage(int siteId, string key, int maxResults = 3)
        {
            try
            {
                IExamineManager examineManager = StaticServiceProvider.Instance.GetService<IExamineManager>();
               
                if (examineManager.TryGetIndex("ExternalIndex", out var index))
                {
                 
                    var query = index.Searcher.CreateQuery("content")
                        .Field("relatedProfiles", key.EnsureEndsWith(key))
                        .And()
                        .Field("searchPath", siteId.ToString()).And()
                        .RangeQuery<long>("displayDateForSorting".Split(','), DateTime.Now.AddYears(-2).Ticks, null) 
                        .OrderByDescending(new Examine.Search.SortableField("displayDateForSorting", SortType.Long))
                        .OrderByDescending(new Examine.Search.SortableField("updateDate", SortType.Long)); 

                    results = query.Execute(new QueryOptions(0, maxResults));
                }
            }
            catch (Exception ex)
            {
              
            }

            return results;
        }
4 Views