Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TermsFacetFor<> - properties of objects in IEnumerables
public static class TermsFacetExtensions
{
public static ITypeSearch<TSource> TermsFacetFor<TSource, TListItem>(this ITypeSearch<TSource> search, Expression<Func<TSource, IEnumerable<TListItem>>> fieldListSelector, Expression<Func<TListItem, object>> fieldListItemSelector)
{
fieldListSelector.ValidateNotNullArgument("fieldListSelector");
fieldListItemSelector.ValidateNotNullArgument("fieldListItemSelector");
var facetName = fieldListSelector.GetFieldPath() + "." + fieldListItemSelector.GetFieldPath();
var fieldName = search.Client.Conventions.FieldNameConvention.GetFieldName(fieldListSelector) + "." + search.Client.Conventions.FieldNameConvention.GetFieldName(fieldListItemSelector);
return new Search<TSource, IQuery>(search, context =>
{
var facetRequest = new TermsFacetRequest(facetName);
facetRequest.Field = fieldName;
context.RequestBody.Facets.Add(facetRequest);
});
}
public static TermsFacet TermsFacetFor<TResult, TListItem>(this IHasFacetResults<TResult> facetsResultsContainer, Expression<Func<TResult, IEnumerable<TListItem>>> fieldListSelector, Expression<Func<TListItem, object>> fieldListItemSelector)
{
fieldListSelector.ValidateNotNullArgument("fieldListSelector");
fieldListItemSelector.ValidateNotNullArgument("fieldListItemSelector");
var facetName = fieldListSelector.GetFieldPath() + "." + fieldListItemSelector.GetFieldPath();
var facet = facetsResultsContainer.Facets[facetName];
if (facet.IsNull())
{
return null;
}
return facet as TermsFacet;
}
}
// Document
public class DocumentListObject
{
public string Name { get; set; }
}
public class Document
{
public Document()
{
DocumentListObjects = new List<DocumentListObject>();
}
public string Name { get; set; }
public List<DocumentListObject> DocumentListObjects { get; set; }
}
// query
var result = client.Search<Document>()
.TermsFacetFor(x => x.DocumentListObjects, x => x.Name)
.GetResult();
var facet = result.TermsFacetFor(x => x.DocumentListObjects, x => x.Name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment