elastic / go-elasticsearch

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

missing `bucket_selector.script` when unmarshalJSON `BucketSelectorAggregation` #882

Closed xinghui closed 4 months ago

xinghui commented 5 months ago

missing script when unmarshalJSON BucketSelectorAggregation

type EsRequest struct {
    Query types.Query                   `json:"query"` // query
    Size  int                           `json:"size"`  // size
    Aggs  map[string]types.Aggregations `json:"aggs,omitempty"`
        // ...
}

input dsl

{
  "_source": {
    "includes": [
      "field1"
    ]
  },
  "aggs": {
    "groupby": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "group_field1": {
              "terms": {
                "field": "field1",
                "missing_bucket": true
              }
            }
          }
        ]
      },
      "aggs": {
        "avg_field1": {
          "avg": {
            "field": "field1"
          }
        },
        "having": {
          "bucket_selector": {
            "buckets_path": {
              "avg_field1": "avg_field1"
            },
            "script": "params.avg_field1 > 50000"
          }
        }
      }
    }
  },
  "size": 0
}
r := EsRequest{}
    err := json.Unmarshal([]byte(dsl), &r)
    if err != nil {
        return nil, err
    }

data, _ := json.Marshal(r)
fmt.Print("output:", string(data))

output dsl:

{
  "query": {

  },
  "size": 0,
  "_source": {
    "includes": [
      "field1"
    ]
  },
  "aggs": {
    "groupby": {
      "aggregations": {
        "avg_field1": {
          "avg": {
            "field": "field1"
          }
        },
        "having": {
          "bucket_selector": {
            "buckets_path": {
              "avg_field1": "avg_field1"
            }
          }
        }
      },
      "composite": {
        "size": 1000,
        "sources": [
          {
            "group_field1": {
              "terms": {
                "field": "field1",
                "missing_bucket": true
              }
            }
          }
        ]
      }
    }
  }
}
Anaethelion commented 4 months ago

Hello!

This is indeed not handled properly because the specification doesn't give any pointer to the default type that could handle the string shortcut. See: https://github.com/elastic/elasticsearch-specification/blob/d555c1aba5b795b31dda7e6fde172c860284949b/specification/_types/Scripting.ts#L87C1-L89C51

I would recommend for now that you use an extended syntax like:

{
...
"script": {"source": "params.avg_field1 > 50000"}
...
}

That should be enough pointers for the script not to disappear.

xinghui commented 4 months ago

ok!

I accept, Thanks for you response.