Searching
Episerver Find excels in traditional, free-text search scenarios and data-centric queries involving exact matching using filters. Find also supports data aggregation using facets. This topic introduces search and filtering in Find.
How it works
You search and query using an instance of the IClient interface. This, along with extension methods, offers a traditional, object-oriented API and convenient fluent API. Most documentation found here covers the fluent API.
Searching
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 code above does not actually perform a search request, but the return object implements IEnumerable and returns instance of the type searched for. After you start to iterate over it, the search request is performed.
var searchQuery = client.Search<BlogPost>()
.For("Beethoven");
foreach (BlogPost blogPost in searchQuery)
{
}
While you iterate 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 let you query for objects in an exact manner, similar to traditional database queries.
client.Search<BlogPost>()
.Filter(x => x.Tags.Match("Music"));
Last updated: Nov 16, 2015