typesense / typesense-go

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

Update document error with 201 status #138

Closed dexp-io closed 7 months ago

dexp-io commented 1 year ago

Description

Erorr: status: 201 response: {....}

Metadata

Typesense Version: v0.25

OS: Linux

kishorenc commented 1 year ago

@dexp-io

I'm not able to understand this issue -- can you please elaborate?

bazookon commented 1 year ago

when document updated, always return error with map[]{ status:201, response : "the same interface sended" }

aliml92 commented 1 year ago

I have the same error, is anyone working on the issue

kishorenc commented 1 year ago

Can you post a small code sample that reproduces the issue that I can run and check?

aliml92 commented 1 year ago

Sorry for the late response. Here are the small example that shows the issue:

  1. docker-compose.yml
    services:
    typesense:
    image: typesense/typesense:0.25.0
    restart: on-failure
    ports:
      - "8110:8108"
    volumes:
      - ./typesense-data:/data
    command: '--data-dir /data --api-key=xyz --enable-cors'
  2. client.go
    
    package main

import ( "fmt"

"github.com/typesense/typesense-go/typesense"
"github.com/typesense/typesense-go/typesense/api"
"github.com/typesense/typesense-go/typesense/api/pointer"

)

func main() { // create client client := typesense.NewClient( typesense.WithServer("http://localhost:8110"), typesense.WithAPIKey("xyz"))

// create schema    
schema := &api.CollectionSchema{
    Name: "companies",
    Fields: []api.Field{
        {
            Name: "company_name",
            Type: "string",
        },
        {
            Name: "num_employees",
            Type: "int32",
        },
        {
            Name:  "country",
            Type:  "string",
            Facet: pointer.True(),
        },
    },
    DefaultSortingField: pointer.String("num_employees"),
}

client.Collections().Create(schema)

// create document
document := struct {
    ID           string `json:"id"`
    CompanyName  string `json:"company_name"`
    NumEmployees int    `json:"num_employees"`
    Country      string `json:"country"`
}{
    ID:           "123",
    CompanyName:  "Stark Industries",
    NumEmployees: 5215,
    Country:      "USA",
}

client.Collection("companies").Documents().Create(document)

// update document
d := struct {
    CompanyName  string `json:"company_name"`
    NumEmployees int    `json:"num_employees"`
}{
    CompanyName:  "Stark Industries",
    NumEmployees: 5500,
}

res, err := client.Collection("companies").Document("123").Update(d)
if err != nil {
    fmt.Printf("err: %v\n", err)
}
fmt.Printf("res: %v\n", res)

}

server is updating the document successfully with `201` status code, but the response is returning in the position of error.
```sh
err: status: 201 response: {"company_name":"Stark Industries","country":"USA","id":"123","num_employees":5500}
res: map[]
danidomi commented 9 months ago

yes, I have the same issue! I opened a PR fixing it!

https://github.com/typesense/typesense-go/pull/151

gdevillele commented 8 months ago

I confirm the issue, I have it too. Thank you for taking care of this. 🙂

kishorenc commented 7 months ago

The actual issue here was with Typesense server: it should not be returning a 201 status code for the update operation since 201 can only be returned when a new resource is created. This was an oversight on our part. This has been fixed in Typesense 0.25.2 -- please upgrade to this version. I will be closing this issue, since a patch on the client is no longer required.

kishorenc commented 7 months ago

There was a typo in my earlier comment. Please use Typesense 0.25.2 to ensure that you don't run into this issue.