elasticsearch 获取每个版本的客户计数,仅包括客户拥有的最高版本

9gm1akwq  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(106)

我使用一个聚合来获取每个版本的客户数:

{
  "aggs": {
    "2": {
      "terms": {
        "field": "version.string.keyword",
        "order": {
          "_key": "desc"
        },
        "size": 50
      },
      "aggs": {
        "1": {
          "cardinality": {
            "field": "orgId.keyword"
          }
        }
      }
    }
  }

这样做的问题是,如果一个客户同时运行两个版本,则该客户将包含在这两个版本中。我需要的是该客户仅包含在最高版本中。例如,如果我有文档:

{
    "orgId": "A",
    "version": {
        "string": "1.1",
        "major": 1,
        "minor": 1
    }
}
{
    "orgId": "A",
    "version": {
        "string": "1.2",
        "major": 1,
        "minor": 2
    }
}
{
    "orgId": "B",
    "version": {
        "string": "1.1",
        "major": 1,
        "minor": 2
    }
}

响应应为:

[
    {
        "1": {
            "value": 1
        },
        "key": "1.1"
    },
    {
        "1": {
            "value": 1
        },
        "key": "1.2"
    }
]

而不是:

[
    {
        "1": {
            "value": 2
        },
        "key": "1.1"
    },
    {
        "1": {
            "value": 1
        },
        "key": "1.2"
    }
]

我尝试了以下查询,它正确返回每个客户的最高版本:

{
  "aggs": {
    "2": {
      "terms": {
        "field": "orgId.keyword",
        "order": {
          "_key": "desc"
        },
        "size": 50
      },
      "aggs": {
        "sorted_version": {
          "top_hits": {
            "sort": [
              {
                "version.major": {
                  "order": "desc"
                },
                "version.minor": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": [
                "version.string"
              ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

我有点迷失了现在如何合并这两个查询,任何帮助将不胜感激。

w6lpcovy

w6lpcovy1#

这个结果对你有帮助吗?

{
  "size": 0,
  "aggs": {
    "group_by_version_string": {
      "terms": {
        "field": "version.string.keyword",
        "order": {
          "_key": "desc"
        }
      },
      "aggs": {
        "group_by_orgId": {
          "terms": {
            "field": "orgId.keyword",
            "order": {
              "_key": "desc"
            }
          }
        }
      }
    }
  }
}

回应

"buckets": [
        {
          "key": "1.2",
          "doc_count": 1,
          "group_by_orgId": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "A",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "1.1",
          "doc_count": 2,
          "group_by_orgId": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "B",
                "doc_count": 1
              },
              {
                "key": "A",
                "doc_count": 1
              }
            ]
          }
        }
      ]

相关问题