Extended Querying in Episerver Find
Episerver Find has, over the years, proven to be a great choice when a search engine has to be selected on our Episerver CMS and Episerver Commerce projects. It is, in most cases, supporting the features needed to build a great search experience with various search related features such as facets.
We are sometimes required to extend the Episerver Find matching algorithm due to special requirements to e.g. free-text search behavior. What our experience shows is, that this can easily be done.
One of the latest examples is a need to match differently, during a free-text search, on significant fields, such as Page Title and Main Body, and less important fields like a Supporting Text.
Let us start out with a simple query that matches only the significant fields via the built in queries such as InField() and AndInField():
That was simple and intuitive. Next step is to match differently on less important fields. Real-life requirements dictates that a match is when all words, regardless of order, position, are present in a property. Individual words, in the free text term, also has to be treated as wild cards, meaning that we needed to allow characters leading and following each word.
Below example shows how the search term “manage on-premise license” will match and become a result.
“Episerver offers complete choice and flexibility in ownership and deployment scenarios for customers. Whether you favor full cloud deployment provisioned as a subscription service, a managed service of a hosted environment, or even on-premise deployment of a perpetual license, you have options to deploy on your terms.”
Default queries in Episerver Find cannot provide us with a solution to above requirement. It means, we need to extend the matching algorithm with a custom query.
Extending Episerver Find
We can achieve this via an extension of ITypeSearch<>, which takes the search term along with a reference to the property to search within.
Usage will, based on the previous example, be something similar to:
We are able to achieve the required behavior by adding an optional sub-query, to the existing Episerver Find query, that details how each word, with wildcards, has to be present (via AND operators).
As you can see, we are splitting the search term into separate words and are adding these, treated as wildcards via the asterisk character, to an optional subquery. I think the code is pretty self-explanatory, so I will not go into any more details.
Lastly, I want to show how this is interpreted to the Episerver Find query language
It clearly shows that we achieved our goal of matching differently on our Supporting Text fields. I would like to emphasize that above example has been taken out of a greater context and are, in reality, recommended to be combined with e.g. advanced boosting that makes sure that the search results are not being to fuzzy.