This content is retired. See latest version here.

Last updated: Jun 10 2014

Introduction

EPiServer Find excels both in traditional free text search scenarios as well as more data-centric queries involving exact matching using filters. It also supports data aggregation using facets. Searching and querying is done using an instance of the IClient interface. The IClient interface along with extension methods offer both a traditional object oriented API as well as convenient fluent API. Most of the documentation found here covers the fluent API.

Search examples

In its simplest form a free text search for objects of a specific type can be made using the Search and For methods.

C#
client.Search<BlogPost>()
    .For("Beethoven");

The above lines of code does not actually perform a search request, but the return object implements IEnumerable and returns instance of the type we searched for. Once we start to iterate over it the search request will be performed.

var searchQuery = client.Search<BlogPost>()
                        .For("Beethoven");

foreach (BlogPost blogPost in searchQuery)
{
                
}

While iterating over the query returns the actual indexed objects we can also retrieve more information by instead using the GetResult method which returns an object with result items, the total number of documents that matched the search query, facets and information about the execution time.

C#
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.

C#
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 searched are performed over all indexed properties, including nested properties. It is also possible to specify what fields to search in.

C#
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 will restrict search results to those matching the filters.

C#
client.Search<BlogPost>()
    .For("Beethoven")
    .Filter(x => x.Tags.Match("Music"));

If applied without a free text query filters offer a way to query for objects in an exact manner, similar to traditional database queries.

C#
client.Search<BlogPost>()
    .Filter(x => x.Tags.Match("Music"));

Comments