I'm trying to create a dynamic faceting feature by using TermsFacetFor and a dynamic MetaField property. My first thought was to do something like:
var colourFacetsResults = SearchClient.Instance.Search()
.TermsFacetFor(x => x.Property["BaseColour"].Value.ToString())
var colourFacets =
x => x.GetPropertyValue(y => y.Property["BaseColour"].Value.ToString())).Terms;
However that doesn't return me any facets. If i use a field defined in my BaseProduct class (eg. TermsFacetFor(x => x.BaseColour), it works, but this won't allow me to acheive what I'm trying to acheive. Has anybody managed to get the idea above working?? If so, how?
An approach you can use is to use an extension method to push additional fields into your index that can then be faceted on. This has been discussed over in this thread:
Thanks for the reply, however we're trying to allow Editors to add new properties to a product using MetaFields and have them as facets, all without having to do a deployment, so we wouldn't be able to create an extension method to push those new fields in to the index.
Is there any other way of creating a facet based on product.Property["x"]?
I don't think it's possible yet, not in an easy way however. I will make sure the PM get's this information so we can create a way of doing facets against not strongly typed properties.
Thanks for the reply. It would be good to be able to do this out of the box, however, I have found a way around this in our case. I can create an extension method on our product class which loops through the metafields and adds them to a Dictionary object. We then include that extension method in the index in our initialization module. I also store a reference to the metafields we want to facet against in DDS, then, when building my facets, I can get those values from DDS and loop through, creating a facet for each, based on the values in the dictionary object.
It sounds much more complicated than it actually is, but I've managed to get it working. I sense a blog post coming up! :)
Certainly sounds blog-worthy Rob - I'd be very interested in seeing that :)!
Hi David, the blog is now live :)