Free text search
Introduction
Searching for phrases or keywords in indexed documents with the EPiServer Find.NET API is typically done with the For method. For instance, searching for blog posts containing the word car can be done with the below code:
var searchResult = client.Search<BlogPost>()
.For("Beethoven")
.GetResult();
Typically the search query isnt constant but rather fetched from user input meaning that a more realistic example would be:
var query = Request.QueryString["q"];
var searchResult = client.Search<BlogPost>()
.For(q)
.GetResult();
Searching in specific fields
By default search queries created using the For method is performed against a special field called the all field (its exact name is _all). This field is generated when an object is indexed and made up of all of the documents fields combined.
It is however possible, and recommended, to specify what fields to search in using the methods InField, AndInField and InFields. These methods expect a lambda expression by which the field name is retrieved. For instance to only search in a field name Title for blog posts the above example code be modified to look like this:
var searchResult = client.Search<BlogPost>()
.For(q)
.InField(x => x.Title)
.GetResult();
To also search in a field named content we can add another invocation of the InField method:
var searchResult = client.Search<BlogPost>()
.For(q)
.InField(x => x.Title)
.InField(x => x.Content)
.GetResult();
Alternatively we could use the InFields method or the AndInField method:
//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();
When one or more fields have been specified using the methods described above the search will no longer be performed against the All field. It is however possible to explicitly request that it will using the InAllField method.
var searchResult = client.Search<BlogPost>()
.For(q)
.InField(x => x.Title)
.InAllField()
.GetResult();
Stemming
Stemming is the process in which a word is reduced to its root form. When using stemming in free text search words that have similar meaning, such as car and cars will match. As stemming is language dependent we need to tell the search engine what language we are searching in. This is done by passing an instance of the language class to the Clients Search method. Instances of the language class matching all supported languages are available as static properties on the Language class.
It is important to note that it is not possible to search using stemming in the All field. This means that in order for the language parameter to have any effect we must specify what fields to search in using the methods described above, such as InField. Below is an example of a search request for the word cars that will match blog posts titled car or A blue car.
client.Search<BlogPost>(Language.English)
.For("cars")
.InField(x => x.Title)
.GetResult();
In many situations we want to search in a number of known fields with stemming but also be able to match text that is not in these fields. While the All field does not support stemming it is still fully viable to search in it when using stemming. So, for instance to search in the Title and Content fields for blog posts and still be able to match blog posts that does not contain the word cars in their Title or Content fields but contain the word in some other field (tags perhaps) we could use the below code:
client.Search<BlogPost>(Language.English)
.For("cars")
.InField(x => x.Title)
.InField(x => x.Content)
.InAllField()
.GetResult();
Last updated: Jun 10, 2014