Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Stellan Danald
Dec 6, 2016
  5046
(3 votes)

Episerver Find - Conditional Sorting?

A client asked me if it was possible to push a specific variation to the top of a list that is coming from Find. I immediately replied "Of course!", since like most things working with development, almost everything is possible; It's just a matter of time/effort, and I thought it would be a quick change.

When I first looked into the task, I noticed that the list was set to order by the variations SortOrder and not score, so boosting wasn't really an option.

SearchClient.Instance.Search<VariationContent>()
	.Filter(x => x.Parents.Match(refId.ToString()))
	…… // Other visibility conditions, facets/terms/skip/take etc
	.OrderBy(x => x.SortOrder)
	.GetResult();


My next step was to see if there are any possibilities do to a conditional sort, as in sorting with filters, or perhaps using custom script sorting which you can do with Elasticsearch that Episerver Find is based upon. After a lot of googling and plowing through a lot of interesting blog posts and questions on world.episerver.com, I couldn't find anything that was related to my task.


Next step was to do some reflection of the Episerver.Find assembly. What I found there was that it IS possible, IF you were sorting by nested objects, using the following method in the Episerver.Find.NestedSortingExtensions class

.OrderBy(enumerableFieldSelector, itemFieldSelector, filterExpression)

Unfortunately, there aren't any existing methods that does what I was looking for, e.g.

.OrderBy(fieldSelector, filterExpression)


Taking another look at the Find SDK, I stumbled upon .Include(filterExpression, boostMultiplier), which, together with the extension .ThenByScore(), written by Henrik Lindström, that we already use, would help me with my task.

.Include(filterExpression, boostMultiplier) allowes me to:

  1. Search for my specific item, using the regular .For(query) with the .InField(fieldSelector) and .Filter(filterExpression)
  2. Get the rest of the variations with the same boosting score (since they don't get a hit in the .For), that I can then order using the field SortOrder, just like before.

The final solution was

SearchClient.Instance.Search<VariationContent>()
	.For(itemId)
	.InField(x => x.ItemId)
	.Filter(x => x.Parents.Match(refId.ToString()))
	.Include(x => x.Parents.Match(refId.ToString()), 1)
	…… // Other visibility conditions, facets/terms/skip/take etc
	.OrderByScore()
	.ThenBy(x => x.SortOrder)
	.GetResult();


I'm sure there are multiple ways to achieve the same results, but this was the simplest way I could do it and it might help someone else.

Cheers! /
Stellan Danald,
Solutions Architect @ Nansen

Dec 06, 2016

Comments

Marcin Samojłowicz
Marcin Samojłowicz Jan 19, 2018 04:47 PM

Thanks Stellan You saved me fiew houers of work :) 

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog