elastic / go-elasticsearch

The official Go client for Elasticsearch
https://github.com/elastic/go-elasticsearch#go-elasticsearch
Apache License 2.0
65 stars 617 forks source link

While fetching from ESQL, inner struct fields are not getting populated #883

Closed kkamranCinedigmInd closed 5 months ago

kkamranCinedigmInd commented 5 months ago

I am using the new ESQL library in Go. While fetching data from it, the fields of inner structs are not getting populated.

type ProcessedLog struct {
    EventName    string                 `json:"event_name"`
    SessionId    string                 `json:"session_id"`
    Device       Device                 `json:"device"`
    User         User                   `json:"user"`
}

func (e ElasticStore) Logs(sessionId string, date string) ([]ProcessedLog, error) {
    query := "FROM " + e.rawLogIndex + "_" + date +
        ` | WHERE session_id == "` + sessionId + `"`
    return EsqlQueryMapper[ProcessedLog](query)
}

func EsqlQueryMapper[T any](queryStr string) ([]T, error) {
    qry := instance.client.Esql.Query().Query(queryStr)
    return query.Helper[T](context.Background(), qry)
}

Here, the EventName and SessionId gets populated but the fields in the Device and User struct do not.

Anaethelion commented 5 months ago

Hi @kkamranCinedigmInd

Thank you for raising this, indeed ES|QL flattens the nested structure such that:

{
    "user": {
            "id": 1
    }
}

Would translate to a user.id field in the response. The helper doesn't currently support this syntax, I've raised a topic on this with the team for future improvements.

You can however override the deserialization on your ProcessedLog by implementing a custom json.Unmarshaler interface to explicitly map your fields.