0

i have the following query:

GET index-*/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "field1": "AP"
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "field2": "SP"
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        },
        {
          "range": {
            "cls_timestamp": {
              "gte": "2022-09-01T00:00:00Z",
              "lt": "2022-10-01T00:00:00Z",
              "format": "strict_date_optional_time"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "id_pratica": {
      "terms": {
        "field": "pratica_id.keyword",
        "size": 10240
      }
    },
    "TestCountBucket": {
      "stats_bucket": {
        "buckets_path": "id_pratica>_count"
      }
    }
  }
}

With the above query I get the value of field "field1" (AP) and the value of field "field2" (SP). The problem is that these value are into two separate documents.

The response is:

"aggregations" : {
    "id_practice" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1582652d-8ddb-4795-9731-218b8373a709"
          "doc_count" : 4
        },
        {
          "key" : "23ef276e-58d7-41bb-b42f-fd5cd12015df",
          "doc_count" : 4
        },
        {
          "key" : "1540f170-44fa-451b-adae-3bd57e792b9c"
          "doc_count" : 3
          ...
          ...
          ...
          ...


"TestCountBucket" : {
      "count" : 59,
      "min" : 1.0,
      "max" : 4.0,
      "avg" : 1.423728813559322,
      "sum" : 84.0

where doc_count makes me count all practices that have string equal to AP and SP while I would need to filter in the buckets the practices that have at least one AP document and at least one SP document. How can i do this? thanks in advance.

EDIT:I solved it by using:

Scripted metric aggregation

0 Answers0