Last active
December 12, 2015 00:18
-
-
Save lindstromhenrik/4682885 to your computer and use it in GitHub Desktop.
TermsFacetFor<> - properties of objects in IEnumerables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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