typesense / typesense-js

JavaScript / TypeScript client for Typesense
https://typesense.org/docs/api
Apache License 2.0
393 stars 74 forks source link

Inconsistent Search Results for Nested Objects #185

Closed sgm55 closed 8 months ago

sgm55 commented 8 months ago

Description

Hi, when performing search queries on nested objects, I'm receiving inconsistent search results.

For example, if I have a document that has the field "members": { {"user1": {"color": "blue"}}, {"user2": {"color": "red"}} } If I search for q="red" query_by="members", then the result is 1 hit. If I search for q="blue" query_by="members", then the result is 0 hit. If I search for q="*" query_by="members", then the result is 1 hit.

However all queries should result in 1 hit

Steps to reproduce

export TYPESENSE_API_KEY=xyz

curl "http://localhost:8108/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
         "name": "companies",
         "fields": [
           {"name": "company_name", "type": "string", "index": true, "optional": true },
           {"name": "num_employees", "type": "int32" },
           {"name": "is_good_standing", "type": "bool", "index": false, "optional": true },
           {"name": "owner", "type": "object", "index": false, "optional": true  },
           {"name": "owner.name", "type": "string", "index": false, "optional": true  },
           {"name": "owner.id", "type": "string", "index": false, "optional": true  },
           {"name": "employees", "type": "object", "index": true, "optional": false},
         ],
         "default_sorting_field": "num_employees",
         "enable_nested_fields": true,
       `}'`

Populate the Database:

curl "http://localhost:8108/collections/companies/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d ' {"company_name": "company1", "num_employees": "2", "owner": {"name": "owner1", "id": "o123" },"employees": {"employee1":{"name": "John", "id": "123"}, "employee2":{"name": "Mary", "id": "456"}}
             {"company_name": "company2", "num_employees": "3", "owner": {"name": "owner2", "id": "o456" },"employees": {"employee2":{"name": "Sarah", "id": "789"}, "employee2":{"name": "Lucas", "id": "147"}, "employee3":{"name": "Peter", "id": "852"}}'

Queries:

curl "http://localhost:8108/multi_search" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
          "searches": [
            {
              "collection": "companies",
              "q": "*",
              "query_by": "employees"
            },
            {
              "collection": "companies",
              "q": "John",
              "query_by": "employees"
            },
            {
              "collection": "companies",
              "q": "Mary",
              "query_by": "employees"
            }
          ]
        }'

Expected Behavior

Removing all non-relevant fields to this issue from the results:

{
    "results": [
        {
            "found": 2,
            "hits": [
                {
                    "document": {<DOCUMENT_DETAILS>},
                }
            ]
        },
        {
            "found": 1,
            "hits": [
                {
                    "document": {<DOCUMENT_DETAILS>},
                }
            ]
        },
        {
            "found": 1,
            "hits": [
                {
                    "document": {<DOCUMENT_DETAILS>},
                }
            ]
        }
    ]
}

Actual Behavior

Removing all non-relevant fields to this issue from the results:

{
    "results": [
        {
            "found": 2,
            "hits": [
                {
                    "document": {<DOCUMENT_DETAILS>},
                }
            ]
        },
        {
            "found": 1,
            "hits": [
                {
                    "document": {<DOCUMENT_DETAILS>},
                }
            ]
        },
        {
            "found": 0,
            "hits": [
                {
                    "document": {<DOCUMENT_DETAILS>},
                }
            ]
        }
    ]
}

Metadata

Typesense Version: Tested on versions 0.25.1 & 0.25.2.rc12

OS: Typesense Cloud