#community-help

Issue with Numerical Faceting in Combination with Group_by

TLDR Vamshi encounters issues with numerical faceting when used in conjunction with group_by in Typesense. After a detailed query script, Kishore Nallan identifies this as a bug, encouraging the user to raise a Github issue.

Powered by Struct AI
7
3w
Solved
Join the chat
Sep 05, 2023 (3 weeks ago)
Vamshi
Photo of md5-14be304e872099e0a38762cb554dcb2c
Vamshi
03:52 PM
Hi everyone, I am trying to get numerical faceting working, but it doesn't seem to work with group_by added. All facets come as zero when group_by is added. Sample response with group by
"facet_counts": [
        {
            "counts": [
                {
                    "count": 0,
                    "highlighted": "",
                    "value": "good"
                },
                {
                    "count": 0,
                    "highlighted": "",
                    "value": "great"
                },
                {
                    "count": 0,
                    "highlighted": "",
                    "value": "average"
                }
            ],
            "field_name": "discountPercentage",
            "sampled": false,
            "stats": {
                "avg": 67.72263095600817,
                "max": 88.87149810791016,
                "min": 10.326700210571289,
                "sum": 3163188.6466932297,
                "total_values": 3
            }
        }
    ]

response without group by:
"facet_counts": [
        {
            "counts": [
                {
                    "count": 26168,
                    "highlighted": "",
                    "value": "good"
                },
                {
                    "count": 20511,
                    "highlighted": "",
                    "value": "great"
                },
                {
                    "count": 29,
                    "highlighted": "",
                    "value": "average"
                }
            ],
            "field_name": "discountPercentage",
            "sampled": false,
            "stats": {
                "avg": 67.72263095600817,
                "max": 88.87149810791016,
                "min": 10.326700210571289,
                "sum": 3163188.6466932297,
                "total_values": 3
            }
        }
    ]

the facet_by query is discountPercentage(average:[0,30],good:[30,70],great:[70,100]) and group_by is a simple productSlug.
Am I missing something? Any help is very appreciated. Typesense cloud is at the latest version so all features should be there.
TIA!
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:58 PM
Hmm, could you give me a simple script like this that creates a sample schema, adds a few sample docs and does a search query that replicates this issue?
Sep 06, 2023 (3 weeks ago)
Vamshi
Photo of md5-14be304e872099e0a38762cb554dcb2c
Vamshi
03:37 AM
Hi Jason hope this works:
export TYPESENSE_API_KEY=dummyAdminKeyToBeChanged

curl "" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"


curl "" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
         "name": "product_variants",
         "fields": [
           {"name": "discountPercentage", "type": "float", "facet": true },
           {"name": "productSlug", "type": "string", "facet": true },
           {"name": "variantID", "type": "int32"}
         ]
       }'

curl "" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"id": "124","discountPercentage": 10,"productSlug": "first","variantID": 1}
            {"id": "125","discountPercentage": 20,"productSlug": "first","variantID": 2}
            {"id": "126","discountPercentage": 30,"productSlug": "second","variantID": 3}
            {"id": "127","discountPercentage": 25,"productSlug": "second","variantID": 4}
            {"id": "128","discountPercentage": 60,"productSlug": "third","variantID": 5}
            {"id": "129","discountPercentage": 70,"productSlug": "third","variantID": 6}'


# without group_by
curl --location --globoff '' \
--header "x-typesense-api-key: ${TYPESENSE_API_KEY}"

# with group_by
curl --location --globoff '' \
--header "x-typesense-api-key: ${TYPESENSE_API_KEY}"

03:38
Vamshi
03:38 AM
and these are the results when I run them
curl --location --globoff '' \
--header "x-typesense-api-key: ${TYPESENSE_API_KEY}"

# with group_by
curl --location --globoff '' \
--header "x-typesense-api-key: ${TYPESENSE_API_KEY}"

{"facet_counts":[{"counts":[{"count":4,"highlighted":"","value":"average"},{"count":2,"highlighted":"","value":"good"}],"field_name":"discountPercentage","sampled":false,"stats":{"avg":35.833333333333336,"max":70.0,"min":10.0,"sum":215.0,"total_values":2}}],"found":6,"hits":[],"out_of":6,"page":1,"request_params":{"collection_name":"product_variants","per_page":0,"q":"*"},"search_cutoff":false,"search_time_ms":0}{"facet_counts":[{"counts":[{"count":0,"highlighted":"","value":"good"},{"count":0,"highlighted":"","value":"average"}],"field_name":"discountPercentage","sampled":false,"stats":{"avg":35.833333333333336,"max":70.0,"min":10.0,"sum":215.0,"total_values":2}}],"found":3,"found_docs":6,"grouped_hits":[],"out_of":6,"page":1,"request_params":{"collection_name":"product_variants","per_page":0,"q":"*"},"search_cutoff":false,"search_time_ms":0}%   

Note how stats is same but the facet_counts array is different in two results
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:04 AM
Vamshi This is a bug (or rather an oversight). Can you please create a Github issue. We will prioritize the fix.
Vamshi
Photo of md5-14be304e872099e0a38762cb554dcb2c
Vamshi
07:08 AM
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:13 AM
Thank you.