Searching
EPiServer Find excels in traditional, free-text search scenarios as well as data-centric queries involving exact matching using filters. Find also supports data aggregation using facets. Searching and querying are done using an instance of the IClient interface. The IClient interface, along with extension methods, offers a traditional, object-oriented API as well as convenient fluent API. Most of this documentation covers the fluent API.
Search examples
In its simplest form, you can execute a free-text search for objects of a specific type using the Search and For methods.
client.Search<BlogPost>()
.For("Beethoven");
The above code does not actually perform a search request, but the return object implements IEnumerable and returns instance of the type searched for. Once you start to iterate over it, the search request is performed.
var searchQuery = client.Search<BlogPost>()
.For("Beethoven");
foreach (BlogPost blogPost in searchQuery)
{
}
While iterating over the query returns the actual indexed objects, you can retrieve more information by using the GetResult method instead, which returns
- an object with result items
- the number of documents that match the search query
- facets
- information about execution time
var searchResult = client.Search<BlogPost>()
.For("Beethoven")
.GetResult();
int numberOfDocsMatchingTheSearch = searchResult.TotalMatching;
int executionTime = searchResult.ServerDuration;
FacetResults facets = searchResult.Facets;
IEnumerable<SearchResultItem<BlogPost>> hits = searchResult.Hits;
Each hit object contains the indexed object, its ID, highlights (if requested) and its score.
var searchResult = client.Search<BlogPost>()
.For("Beethoven")
.GetResult();
foreach (var hit in searchResult.Hits)
{
string id = hit.Id;
BlogPost item = hit.Item;
Highlights highlights = hit.Highlights;
double? score = hit.Score;
}
Unless specified, searches are performed over all indexed properties, including nested properties. You can specify which properties to search.
client.Search<BlogPost>()
.For("Beethoven")
.InFields(
x => x.Title,
x => x.Tags,
x => x.Author.Name);
Filtering
The service and client API also supports filtering. If applied to a free text query, filtering restricts search results to those matching the filters.
client.Search<BlogPost>()
.For("Beethoven")
.Filter(x => x.Tags.Match("Music"));
If applied without a free text query, filters query for objects in an exact manner, similar to traditional database queries.
client.Search<BlogPost>()
.Filter(x => x.Tags.Match("Music"));
Last updated: Feb 23, 2015