HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuidesLegal TermsDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Free text search

Describes how free text search works in Optimizely Search & Navigation, how to perform queries against fields, and how to work with stemming in free text search.

You typically use the For method to search for phrases or keywords in indexed documents. For instance, to search for blog posts that contain car, use the code in the following examples.

var searchResult = client.Search<BlogPost>()
    .For("Car")
    .GetResult();

Typically, user input fetches a search query.

var query = Request.QueryString["q"];
var searchResult = client.Search<BlogPost>()
    .For(q)
    .GetResult();

Search fields

By default, a search query using the For method is performed against a field named _all. When an object is indexed, it generates the field and combines it with the document's fields.

Search queries should specify fields using the InField, AndInField and InFields methods. These methods expect a lambda expression, which retrieves the field name. For example, this code searches the Title field of blog posts.

var searchResult = client.Search<BlogPost>()
    .For(q)
    .InField(x => x.Title)
    .GetResult();

To also search the Content field, add a second InField method.

var searchResult = client.Search<BlogPost>()
    .For(q)
    .InField(x => x.Title)
    .InField(x => x.Content)
    .GetResult();

Alternatively, you can use the InFields method or the AndInField method, as shown below

//Using InFields
var searchResult = client.Search<BlogPost>()
    .For(q)
    .InFields(x => x.Title, x => x.Content)
    .GetResult();

//Using AndInField
var searchResult = client.Search<BlogPost>()
    .For(q)
    .InField(x => x.Title)
    .AndInField(x => x.Content)
    .GetResult();

If you specify one or more fields using the methods described above, it does not perform the search against the All field. However, you can explicitly request the search to use the InAllField method.

var searchResult = client.Search<BlogPost>()
    .For(q)
    .InField(x => x.Title)
    .InAllField()
    .GetResult();

Stem words

Stemming is the process of reducing a word to its root form. When using stemming in free text search, words with similar meanings, such as car and cars, match.

Stemming is language-dependent, so you need to tell the search engine the language you are searching. To do this, pass an instance of the Language class to the Clients Search method. Instances of the Language class that match supported languages are available as static properties on the Language class.

📘

Note

Search with the All field cannot use stemming. This means that, for the language parameter to have any effect, you must specify search fields (such as InField) using methods described above. Below is a sample search request for cars_that matches blog posts titled _car or A blue car.

client.Search<BlogPost>(Language.English)
    .For("cars")
    .InField(x => x.Title)
    .GetResult();

Often, you want to search several known fields with stemming but also match text that is not in those fields. Although the All field does not support stemming, you can still search for it when using stemming. You can use the following code to search the Title and Content fields for blog posts and still match blog posts that do not contain the word cars in their Title or Content fields but have the word in another field (such as tags).

client.Search<BlogPost>(Language.English)
    .For("cars")
    .InField(x => x.Title)
    .InField(x => x.Content)
    .InAllField()
    .GetResult();

Use AND as operator in multi-word queries

You can configure the free text search to use AND as an operator instead of the default OR to match search terms minus stop words in multi-word queries. For example, searching for houses in Stockholm may return many irrelevant results for houses.

To avoid this, you can use WithAndAsDefaultOperator() after a method that returns IQueriedSearch<T>, such as For().

SearchClient.Search<AClass>()
.For("houses Stockholm")
.WithAndAsDefaultOperator();

This code only returns results matching houses and Stockholm.