typesense-go icon indicating copy to clipboard operation
typesense-go copied to clipboard

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 {}

Open vamshiaruru32 opened this issue 2 years ago • 8 comments

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 avatar Jul 25 '23 11:07 vamshiaruru32

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.

vamshiaruru32 avatar Jul 25 '23 11:07 vamshiaruru32

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

Mebby8448 avatar Aug 01 '23 13:08 Mebby8448

Will be looking into it this week.

kishorenc avatar Aug 02 '23 15:08 kishorenc

@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

Mebby8448 avatar Aug 06 '23 13:08 Mebby8448

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.

kishorenc avatar Aug 08 '23 09:08 kishorenc

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?

vamshiaruru32 avatar Sep 01 '23 17:09 vamshiaruru32

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.

kishorenc avatar Sep 06 '23 07:09 kishorenc

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

vamshiaruru32 avatar Sep 06 '23 12:09 vamshiaruru32