Nested filtering with EPiServer Find
Some have already noticed one of the x2find mebers, Nested2Find, that enables nested object mappings and filtering to the EPiServer Find API. I would like to give a short description of what it does and how it can help you in some filtering scenarios.
Sometimes we need to be able to filter documents based on matching a specific object in a lists of complex objects on the document. Say for instance that we have documents that have a list of all authors that have contributed. Authors that all have a set of properties such as name and address. We then want to find all documents where one of the authors match a specific set of criterias, say all documents that have a swedish author named Henrik. This is what Nested2Find enables you to do. It lets you define nested lists of complex objects on a document for which you then later can specify a matching criteria when querying.
How to use the Nested2Find extension
Add the nested conventions to the conventions:
client.Conventions.AddNestedConventions();
Create an object containing a NestedList<> of objects (NestedList<> is simply a typed List<>):
public class Document
{
public Document()
{
Authors = new NestedList<Author>();
}
public string Title { get; set; }
public NestedList<Author> Authors { get; set; }
public string Body { get; set; }
}
public class Author
{
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
Index and start filtering:
result = client.Search<Document>()
.Filter(x => x.Authors, p => p.Name.Match("Henrik") & p.Country.Match("Sweden"))
.GetResult();
or:
result = client.Search<Document>()
.Filter(x => x.Authors.MatchItem(p => p.FirstName.Match("Henrik") & p.Country.Match("Sweden")))
.GetResult();