Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Jul 25, 2023 · 8 comments

Comments

@vamshiaruru32
Copy link

vamshiaruru32 commented Jul 25, 2023

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
Copy link
Author

vamshiaruru32 commented Jul 25, 2023

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
Copy link

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

@kishorenc
Copy link
Member

Will be looking into it this week.

@Mebby8448
Copy link

@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
Copy link
Member

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
Copy link
Author

vamshiaruru32 commented Sep 1, 2023

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
Copy link
Member

I finally figured out how to make openai generator spit out interface type. I've pushed a commit for this: 24486a6

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

@vamshiaruru32
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants