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

Vote:
 

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:

{Product1
Manufacturers: [{Man1 Id: 1} {Man2 Id: 2}]
}
{Product2
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

Thanks

#150245
Jun 14, 2016 18:07
Vote:
 

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

http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-Find/12/DotNET-Client-API/Searching/nested-queries/

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

#150246
Jun 14, 2016 18:35
Vote:
 

Hi,

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())
#150265
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.