Aggregation with collapsed query

Hi

I just want to apply aggregation on a query result. My query is eleminating the double documents keeping last one according to a clumn. But the aggragation doesn't care the actual result. I've read that the collapse doesn't affect the aggragation. I could't find a way to do that!

GET /db-oracle-kys-aktarim-*/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {"range": {"islemtarih":{"gte":"now/d"}}},
        {"match_phrase": {
           "islemyapan": "pasaport"
         }}
        ]
    }
  },
  "collapse": {
    "field": "kayitno"
  },
  "sort": [
    {
      "islemtarih": {
        "order": "desc"
      }
    }
  ],
  "from": 0, 
  "aggs": {
    "categories": {
      "terms": {
        "field": "aciklama.keyword"
      }
    }
  }
}

and the result is,

"aggregations" : {
    "categories" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Baþarili",
          "doc_count" : 4598
        },
        {
          "key" : "Beklemede",
          "doc_count" : 4474
        }
      ]
    }
  }

but it must be,

"aggregations" : {
    "categories" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Baþarýlý",
          "doc_count" : 4598
        },
        {
          "key" : "Beklemede",
          "doc_count" : 30
        }
      ]
    }
  }

because of doubled documents!

Do you have some examples of docs to simulate?

The documnts has same "kayitno" with date column "islemtarih" so I just want to keep latest doc for aggregation.

{
  "_index": "db-cc-kys-aktarim-000001",
  "_id": "tckk_2978351655",
  "_version": 1,
  "_score": 1,
  "_source": {
    "islemtur": 1,
    "islemtarih": "2022-04-28T06:59:06Z",
    "kysdurum": 1,
    "islemyapantc": xxxx,
    "tckimlikno": xxxx,
    "@version": "1",
    "kayitno": 2222222222,
    "islemyapan": "tckk",
    "aciklama": "Gönderilmedi",
    "basvurutarih": "2022-04-28T06:57:24Z",
    "tckkkayitno": 111111111,
    "@timestamp": "2022-04-28T06:59:10.558690800Z",
    "type": "kys-aktarim"
  },
  "fields": {
    "tckimlikno": [
      35806692548
    ],
    "islemyapan": [
      "tckk"
    ],
    "islemyapan.keyword": [
      "tckk"
    ],
    "@version.keyword": [
      "1"
    ],
    "type": [
      "kys-aktarim"
    ],
    "kysdurum": [
      1
    ],
    "islemtur": [
      1
    ],
    "aciklama": [
      "Gönderilmedi"
    ],
    "@timestamp": [
      "2022-04-28T06:59:10.558Z"
    ],
    "kayitno": [
      2978351654
    ],
    "type.keyword": [
      "kys-aktarim"
    ],
    "basvurutarih": [
      "2022-04-28T06:57:24.000Z"
    ],
    "@version": [
      "1"
    ],
    "islemtarih": [
      "2022-04-28T06:59:06.000Z"
    ],
    "aciklama.keyword": [
      "Gönderilmedi"
    ],
    "tckkkayitno": [
      335669560
    ],
    "islemyapantc": [
      88888888880
    ]
  }
}



{
  "_index": "db-cc-kys-aktarim-000001",
  "_id": "tckk_2978351654",
  "_version": 1,
  "_score": 1,
  "_source": {
    "islemtur": 1,
    "islemtarih": "2022-04-28T07:00:06Z",
    "kysdurum": 1,
    "islemyapantc": xxxx,
    "tckimlikno": xxxx,
    "@version": "1",
    "kayitno": 2222222222,
    "islemyapan": "tckk",
    "aciklama": "Gönderidi",
    "basvurutarih": "2022-04-28T07:00:05Z",
    "tckkkayitno": 111111111,
    "@timestamp": "2022-04-28T07:00:06Z",
    "type": "kys-aktarim"
  },
  "fields": {
    "tckimlikno": [
      35806692548
    ],
    "islemyapan": [
      "tckk"
    ],
    "islemyapan.keyword": [
      "tckk"
    ],
    "@version.keyword": [
      "1"
    ],
    "type": [
      "kys-aktarim"
    ],
    "kysdurum": [
      1
    ],
    "islemtur": [
      1
    ],
    "aciklama": [
      "Gönderidi"
    ],
    "@timestamp": [
      "2022-04-28T07:00:06Z"
    ],
    "kayitno": [
      2978351654
    ],
    "type.keyword": [
      "kys-aktarim"
    ],
    "basvurutarih": [
      "2022-04-28T06:57:24.000Z"
    ],
    "@version": [
      "1"
    ],
    "islemtarih": [
      "2022-04-28T07:00:06Z"
    ],
    "aciklama.keyword": [
      "Gönderilmedi"
    ],
    "tckkkayitno": [
      335669560
    ],
    "islemyapantc": [
      88888888880
    ]
  }
}

Try this:

 "aggs": {
    "categories": {
      "terms": {
        "field": "aciklama.keyword"
      },
      "aggs": {
        "unique_values": {
          "cardinality": {
            "field": "kayitno"
          }
        }
      }
    }
  }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.