Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Facet for Enumerable field


I have an index of Products, each with a Manufacturer. I created a histogram facet for the Manufacturer Ids in this way:

HistogramFacetFor(p => p.manufacturer.Id, 1)

and this worked just fine. Then I learned each Product can have a list of Manufacturers. How can I create a facet now?

For example, if the index contains Products:

Manufacturers: [{Man1 Id: 1} {Man2 Id: 2}]
Manufacturers: [{Man3 Id: 3} {Man2 Id: 2}]

I need to know that there is:
1 product with Manufacturer.Id 1
2 products with Manufacturer.Id 2
1 product with Manufacturer.Id 3


Jun 14, 2016 18:07

Then you need to check into nested queries. Check documentation here

That's exactly what nested queries were meant to support...

Jun 14, 2016 18:35


I think you should be able to do this using TermsFacetFor.

Create an extension method to get the Ids:

    public static class Extensions
        public static List<int> Ids(this IEnumerable<Manufacturer> manufacturers)
            return manufacturers.Select(x => x.Id).ToList();

then make sure there are indexed:

SearchClient.Instance..Conventions.ForInstancesOf<Book>().IncludeField(x => x.Manufacturers.Ids());

Remember to reindex!

Since the Id's are ints and not strings, you'll need to do a small "hack" in order to get Find to accept the input:

 .TermsFacetFor(x => (string)(object) x.Manufacturers.Ids())
Jun 15, 2016 9:32
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.