Try our conversational search powered by Generative AI!

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

Recommended reading 

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.

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

Typically, a search query is fetched from user input.

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

Searching fields

By default, a search query using the For method is performed against a field named _all. The field is generated when an object is indexed, and it is made up of all of the document's fields combined.

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.

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

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

C#
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. Their usage is demonstrated below.

C#
//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 one or more fields are specified using methods described above, the search is no longer performed against the All field. However, you can explicitly request the search to use the InAllField method.

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

Stemming

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 in which 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 all 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.

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

Often, you want to search a number of 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 it when using stemming. So, for example, 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 do contain the word in another field (such as tags), you can use the following code.

C#
client.Search<BlogPost>(Language.English)
    .For("cars")
    .InField(x => x.Title)
    .InField(x => x.Content)
    .InAllField()
    .GetResult();
Do you find this information helpful? Please log in to provide feedback.

Last updated: Nov 16, 2015

Recommended reading