Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

How it works

The .NET API provides rich functionality both for free text search and for querying by applying filters. These can also be combined so that certain criteria are applied to free text search by means of filters. Sometimes however the opposite is desired. That is, instead of limiting search results by applying filter we may want to include documents that does not necessarily match the search query or previously applied filters.

You can achieve this using the Include method. Just like the Filter method and the Boostmatching method, the Include method requires a filter expression as a parameter. Documents that match this filter are included in the result even if they otherwise would not be included. Because these documents may not match any previously applied search query they are given a constant score so they are ordered high or low in the search results depending on the score of other documents in the result. Therefor the Include method has a second parameter by which you can enter a boost factor. Set it to 1 if you do not care about the order, to 10 if you want the documents high in the search results, and to 0.1 if you want them low in the search results.

Examples

A typical use case for using the Include method is when building search as you type functionality. Perform a regular free text search AND include documents whose title contains a word that starts with what the user has entered. For instance, if you are building search as you type functionality for blog posts and the user has entered Bar into the search text box. It may be that the user is searching for bar, or it may be that the user is about to type in Barcelona. To retrieve sensible suggestions for the user, perform a regular free text search but also include blog posts where the title (or other important but short fields) contains a word that starts with bar.

C#
var q = "Bar";

searchResult = client.Search<BlogPost>()
  .For(q)
  .Include(x => x.Title.AnyWordBeginsWith(q), 1)
  .GetResult();

Note: We are using the AnyWordBeginsWith filter method in the above example. This is a powerfull method that should be used with caution. It is perfectly sensible to use it on short fields such as titles, tags and the like, but not on longer text fields as that will have a sever, negative, effect on performance.

Do you find this information helpful? Please log in to provide feedback.

Last updated: Nov 16, 2015

Recommended reading