elasticsearch A필드의 count가 가장 큰 B필드 구하기

개발새발·2023년 6월 11일
0

elasticsearch

목록 보기
50/54

elasticsearch 마스터의 길은 멀고도 험하고..ㅠㅠ BMT테스트를 진행하려고 특정 필드(array) 값의 count가 가장 큰 아이가 필요했다. 갑오작오~~~ 🏃

🏀 쿼리문

간단하니 걱정들마십쇼~~ 아래 쿼리 베껴가심됩니다~

  • A필드 = count 하는 필드 / B필드 = A필드의 count가 가장 큰 필드
GET /{인덱스명}/_search
{
  "size": 0,
  "aggs": {
    "{group_by_B}": {
      "terms": {
        "field": "{B field}",
        "size": 1, // count가 가장 큰 필드 하나만 나온다 (내림차순으로 정렬했으니)
        "order": {
          "count": "desc" // count를 내림차순대로 정렬한다
        }
      },
      "aggs": {
        "count": {
          "cardinality": {
            "field": "{A field}"
          }
        }
      }
    }
  }
}

🍀 결과

{
    "took": 97,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "{group_by_B}": {
            "doc_count_error_upper_bound": -1,
            "sum_other_doc_count": 48290,
            "buckets": [
                {
                    "key": "{B field}", // A field count가 가장 큰 필드! 
                    "doc_count": 1,
                    "count": {
                        "value": 198
                    }
                }
            ]
        }
    }
}
profile
발새발개

0개의 댓글