Question, anyone ever had to deal with "dynamic" f...
# community-help
r
Question, anyone ever had to deal with "dynamic" facets? Our products can be assigned various properties (and one or multiple corresponding values). We want to facet by those properties. So we introduced a field named
facets
with type
object
and then
facets.*
which has facet set to true and type
string[]
. we then map our data by (simplified example)
facets = {property.slugified(): values[]}
- however, we'd like to show the actual property name for the facet, not the slugified version. is it possible to maybe specify our schema as
facets.*.name
type
string
,
facets.*.values
as type
string[]
and then somehow let typesense know that the values for
facet.X
are in
facet.X.values
? does this make sense? 😅 Or maybe you could suggest a completely different way to solving our issue?
k
Have you checked the
facet_return_parent
parameter?
r
yes I have, but it is unclear to me how I can use it
k
When you facet on a field like
color.name
you can do:
Copy code
"facet_return_parent": "color.name"
This will return the parent
color
object as
parent
property in the facet response.
r
I'm currently doing a
filter_by: category:X
and
facet_by: *
search to get a list of all facets applicable to the products in given category, so I can dynamically created facet widgets for the relevant facets
so I don't know the facets up front until after that initial query
k
So basically it's a way for you to return the parent object of the faceting field, because you can store some readable properties as sibling of the nested facet field.
r
i don't see how I could use that, since I do not know which facets i am faceting by up front
or is this part of the schema definition?
my search is returning a payload like
Copy code
{
    "facet_counts": [
        {
            "counts": [
                {
                    "count": 7,
                    "highlighted": "Plamuren",
                    "value": "Plamuren"
                },
                ...
            ],
            "field_name": "facets.toepassing.value",
            "sampled": false,
            "stats": {
                "total_values": 5
            }
        },
        {
            "counts": [
                {
                    "count": 12,
                    "highlighted": "Gelcoat & Topcoat",
                    "value": "Gelcoat & Topcoat"
                },
                ...
            ],
            "field_name": "facets.type-product.value",
            "sampled": false,
            "stats": {
                "total_values": 8
            }
        },
        {
            "counts": [
                { "count": 32, "highlighted": "RESION", "value": "RESION" },
                ...
            ],
            "field_name": "brand",
            "sampled": false,
            "stats": {
                "total_values": 5
            }
        }
    ],
    "found": 48,
    "hits": [
        {
this lets me know I want to create 3 facet widgets, for
facets.toepassing.value
,
facets.type-product.value
and for
brand
(this last one we i18n in the frontend).
but the first two their localized name is stored under
facets.X.name
k
Try doing
facet_return_parent=facets
r
where do i specify that though?
/search?q=*&query_by=name&filter_by=category_normalized:Polyester&sort_by=&facet_by=*&facet_return_parent=facets
i'm not really seeing anything different in the response payload
it looks like I need to call
/search?q=*&query_by=name&filter_by=category_normalized:Polyester&sort_by=&facet_by=*&facet_return_parent=facets.toepassing.value,facets.type-product.value
but that is problematic because I do not know which facets are returned up front. i'd have to issue multiple queries to finally be able to start creating an overview of the producs within a certain category and their respective facets
k
Would you able to do these queries initially once and cache the values for constructing subsequent search requests?
r
we don't really do a lot of client side caching yet
but i could cache it for the duration of the page session
k
The
facet_return_parent
unfortunately requires an explicit reference to a concrete child field value right now.
r
i'm just wondering if our approach to facets is maybe just the wrong way to go.
k
Maybe if we supported a wildcard in the field name it will help.
r
it feels like other people would have run into this problem already before us if this was indeed a common approach to solving it
j
Typically, I've seen users just use the display value as the facet field name. So in your case, you'd do something like:
Copy code
{
  "facets": {
    "Facet Field A 123": [
      "Value 1",
      "Value 2"
    ],
    "Facet Field B 456": "Value 3"
  }
}
Notice the spaces in the field values. It looks odd, but this works
r
ah.. I was worried it might not work well with special characters
j
Ah hmm, yeah special characters especially parentheses will have issues. Another approach is to store a static mapping of sluggified names to display names in a separate place, and use that at render time
r
yeh i was leaning towards that as well
j
Separate place could be for eg, a headless CMS system, may be a separate Typesense collection that is heavily cached that just stores this, static variable, etc
All of that said, @Kishore Nallan I still agree having wild card support for
facet_return_parent
would be helpful for dynamic faceting. We added
facet_return_parent
to solve for not having to do these extra steps and that works for non-dynamic facets currently. So would be good to extend that same mechanism to work with dynamic facets as well
👍 1
r
i get the impression that i get back facets from documents that should be excluded based on my filter_by value. in algolia this was not the case. any idea why typesense is different in this behaviour?
wait, my bad, i think i see what i did wrong
nope... still including documents not matching my filter_by
j
Hmmm, the faceting behavior is to only facet on the current resultset after applying filters (which is the same as Algolia)
Could you try replicating with a set of simple set of commands like this: https://gist.github.com/jasonbosco/7c3432713216c378472f13e72246f46b
r
think i solved it. was missing the configure widget with filters
without that, selecting facets results in a multi search query where the filter_by is missing
👍 1