#community-help

Faceting Objects in Typesense

TLDR John wants to facet objects in Typesense, but faces issues. Kishore Nallan suggests creating a feature request on GitHub.

Powered by Struct AI

1

1

this

1

Mar 20, 2023 (9 months ago)
John
Photo of md5-21545f1facb7836c149bc4c70752bd2b
John
01:23 PM
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

01:23
John
01:23 PM
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))
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:24 PM
👋 Can you try with color.* in the facet_by?
John
Photo of md5-21545f1facb7836c149bc4c70752bd2b
John
01:27 PM
typesense.exceptions.ObjectNotFound: [Errno 404] Could not find a facet field named `color.*` in the schema.
01:27
John
01:27 PM
color.name for example works
01:27
John
01:27 PM
but only gets me the names ofc
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:28 PM
What version are you using?
01:30
Kishore Nallan
01:30 PM
Sorry this works only on the 0.25 RC builds, e.g. 0.25.0.rc14
John
Photo of md5-21545f1facb7836c149bc4c70752bd2b
John
01:31 PM
Ohh nice, I’ll try that, thanks
01:35
John
01:35 PM
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
{
  "value": {"name": "blue", "rgb": "#123456"},
  "count": 3
},
{
  "value": {"name": "black", "rgb": "#000000"},
  "count": 2
}

Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:40 PM
Can you explain what you mean by "independently"?
John
Photo of md5-21545f1facb7836c149bc4c70752bd2b
John
01:43 PM
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, …}
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:44 PM
Ah I see, you want the facet to be whole object content. Interesting! That's a feature request then 😜
John
Photo of md5-21545f1facb7836c149bc4c70752bd2b
John
01:45 PM
😄 Exactly
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:45 PM
Can you please create an issue on GitHub? We will add to roadmap.
John
Photo of md5-21545f1facb7836c149bc4c70752bd2b
John
01:45 PM
Sure, thank you
Mar 21, 2023 (9 months ago)

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community