typesense / typesense-go

Go client for Typesense: https://github.com/typesense/typesense
Apache License 2.0
208 stars 55 forks source link

Unable to make search query with group by, with the error: json: cannot unmarshal number into Go struct field SearchGroupedHit.grouped_hits.group_key of type map[string]interface {} #141

Open vamshiaruru32 opened 1 year ago

vamshiaruru32 commented 1 year ago

Description

When I make a search query with group by, I get the error

json: cannot unmarshal number into Go struct field SearchGroupedHit.grouped_hits.group_key of type map[string]interface {}

Steps to reproduce

    filterString := fmt.Sprintf("productSlug:=%s", productSlug)
    searchParams := &api.SearchCollectionParams{
        Q:          "*",
        QueryBy:    "searchText0,searchText1,searchText2",
        FilterBy:   &filterString,
        GroupBy:    utils.Ref("productID"), // utils.Ref just gets a pointer
        GroupLimit: utils.Ref(1),
    }
    resp, err := tsClient.Collection(collection).Documents().Search(searchParams)

And here's the schema

{
  "name": "product_variants_all",
  "fields": [    
    {
      "name": "searchText0",
      "type": "string",
      "facet": false,
      "optional": false,
      "index": true,
      "sort": false,
      "infix": false,
      "locale": ""
    },
    {
      "name": "productSlug",
      "type": "string",
      "facet": false,
      "optional": false,
      "index": true,
      "sort": false,
      "infix": false,
      "locale": ""
    },
    {
      "name": "searchText1",
      "type": "string",
      "facet": false,
      "optional": false,
      "index": true,
      "sort": false,
      "infix": false,
      "locale": ""
    },
    {
      "name": "searchText2",
      "type": "string",
      "facet": false,
      "optional": false,
      "index": true,
      "sort": false,
      "infix": false,
      "locale": ""
    },
    {
      "name": "rankingScore",
      "type": "float",
      "facet": false,
      "optional": false,
      "index": true,
      "sort": true,
      "infix": false,
      "locale": ""
    },
    {
      "name": "productID",
      "type": "int32",
      "facet": true,
      "optional": false,
      "index": true,
      "sort": true,
      "infix": false,
      "locale": ""
    }
  ],
  "default_sorting_field": "rankingScore",
  "enable_nested_fields": true,
  "symbols_to_index": [],
  "token_separators": []
}

Expected Behavior

No error

Actual Behavior

Unmarshal error

Metadata

Typesense Version: 0.24.1

OS: Ubuntu 22.04

vamshiaruru32 commented 1 year ago

The error is happening because this is the search response when an api call is made:

"group_key": [
        5
      ]

But the type is defined as

GroupKey []map[string]interface{} 

It should instead be

GroupKey []interface{}

I have created a fork with that change and used that to test things, it works. However, I don't know if that is the right solution, buecase every code gen will override that change.

Mebby8448 commented 1 year ago

I faced the same issue. @kishorenc Could you please take a look?

kishorenc commented 1 year ago

Will be looking into it this week.

Mebby8448 commented 1 year ago

@kishorenc As a quick workaround, on line https://github.com/typesense/typesense-api-spec/blob/0768c9f88dafc35b45ba8b4e888bd894ade8d208/openapi.yml#L1448

From:

          group_key:
          type: array
          items:
            type: object

Can we change the type to string? To:

          group_key:
          type: array
          items:
            type: string
kishorenc commented 1 year ago

I've just published v0.8.0 which change the type of group_key to array of string. I've also added a test for this. Please check and confirm.

vamshiaruru32 commented 1 year ago

Hi @kishorenc I am still getting the same error:

json: cannot unmarshal number into Go struct field SearchGroupedHit.grouped_hits.group_key of type string

This time because the type for GroupKey is defined as []string, where as I am using an int field. But I haven't seen in documentation that group by has to be a string. so, is this a bug in the go sdk or is it a miss in the documentation? Can you verify please?

kishorenc commented 1 year ago

I finally figured out how to make openai generator spit out interface type. I've pushed a commit for this: https://github.com/typesense/typesense-go/commit/24486a683104574d73c3ee0f070facb671052391

Can you please try with this SHA and let me know if it works? If it does, I will create a new release.

vamshiaruru32 commented 1 year ago

Hi @kishorenc , thanks for the fix. It is working fine with your commit.