Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

HomeDev GuideAPI Reference
Dev GuideAPI ReferenceLegal TermsDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Include documents matching a filter

Describes how to work with queries and filtering, to include specific documents that do not match any queries or filters applied in Optimizely Search & Navigation.

The .NET API provides rich functionality for free text search and querying by applying filters. These can be combined to use certain criteria to free text search using filters. Sometimes, however, the opposite is desired. That is, instead of limiting search results by applying filters, you want to include documents that do not necessarily match the search query or previously applied filters.

Achieve this with the Include method. Just like the Filter method and the Boostmatching method, the Include method requires a filter expression as a parameter. The result includes documents that match this filter. The documents get a constant score because they may not match a previously-applied search query. So, they are ordered high or low in the search results depending on the score of other documents. The Include method has a second parameter that lets you enter a boost factor.

Set it to

  • 1 if you do not care about the order.
  • 10 if you want the documents high in the search results.
  • 0.1 if you want the documents low in the search results.

Examples

A typical use case for the Include method is building search-as-you-type functionality. Perform a regular free text search and include documents whose title contains a word that starts with user input. For instance, you build search-as-you-type functionality for blog posts, and the user enters Bar into the search box.

It may be that the user is searching for bar, or maybe the user is about to type in Barcelona. To retrieve sensible suggestions, perform a regular free-text search and include blog posts where the title (or other important but short fields) contains a word starting with bar.

var q = "Bar";

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

📘

Note

The above example uses the AnyWordBeginsWith filter method. Use this powerful method with caution. It is perfectly sensible to use it on short fields such as titles, tags and so forth. Do not use it on longer text fields -- that will have a severe, negative impact on performance.