Hey! We’re trying to use the nested fields to hav...
# community-help
j
Hey! We’re trying to use the nested fields to have a pretty color filter where we’d like to store something like
name="Blue", h=0, s=10, l=20
, facet by that, and then have the frontend nicely display the name along with the actual color. However, it doesn’t seem like faceting based on objects is working, you just get the empty string. Is this something you could implement support for or do we have to try to find a workaround? I imagine you could hash the objects and facet by that as a string under the hood or something. Code in thread.
this 1
Copy code
import json

import typesense

COLLECTION = "example"
client = typesense.Client(
    {
        "api_key": "TYPESENSEDEV",
        "nodes": [{"host": "localhost", "port": "8108", "protocol": "http"}],
        "connection_timeout_seconds": 2,
    }
)

try:
    client.collections[COLLECTION].delete()
except:
    pass

fields = [
    {
        "name": "title",
        "type": "string",
    },
    {
        "name": "color",
        "type": "object",
        "facet": True,
    },
]
client.collections.create(
    {
        "name": COLLECTION,
        "fields": fields,
        "enable_nested_fields": True,
    }
)

products = [
    {
        "title": "foo",
        "color": {
            "name": "red",
            "h": 0,
            "s": 100,
            "l": 50,
        },
    },
    {
        "title": "bar",
        "color": {
            "name": "blue",
            "h": 240,
            "s": 100,
            "l": 50,
        },
    },
    {
        "title": "baz",
        "color": {"name": "Multi-colored", "special_type": "multi"},
    },
]

client.collections[COLLECTION].documents.import_(products, {"action": "create"})

facet_counts = client.collections[COLLECTION].documents.search(
    {
        "q": "*",
        "facet_by": "color",
    }
)["facet_counts"]
print(json.dumps(facet_counts, indent=4))
k
👋 Can you try with
color.*
in the facet_by?
j
Copy code
typesense.exceptions.ObjectNotFound: [Errno 404] Could not find a facet field named `color.*` in the schema.
color.name
for example works
but only gets me the names ofc
k
What version are you using?
Sorry this works only on the 0.25 RC builds, e.g.
0.25.0.rc14
j
Ohh nice, I’ll try that, thanks
Yeah that works in
0.25.0.rc14
but it still seems to facet the fields independently, whereas what I’d want is something like
Copy code
{
  "value": {"name": "blue", "rgb": "#123456"},
  "count": 3
},
{
  "value": {"name": "black", "rgb": "#000000"},
  "count": 2
}
k
Can you explain what you mean by "independently"?
j
I mean that it’s faceting the subfields, not the actual objects, and there’s not really a way to join them back together from the API response. So in this case I could get that there’s 137 documents with Hue value 15, 49 documents with Hue value 19, 137 documents with Name value Blue et.c. But what I want is to know that there are 137 documents with {name=blue, hue=15, …}
k
Ah I see, you want the facet to be whole object content. Interesting! That's a feature request then 😜
j
😄 Exactly
k
Can you please create an issue on GitHub? We will add to roadmap.
j
Sure, thank you