Elasticsearch / OpenSearch-数组中的共同值的交集

发布于 2025-01-30 15:03:38 字数 247 浏览 2 评论 0原文

给定的

a = [1,2,3,4]

以及

b = [1,3]
c = [5,6]
d = [1,6]

如何在Elasticsearch中使用a的确切数量a

我希望:

b -> 2
c -> 0
d -> 1

given

a = [1,2,3,4]

and

b = [1,3]
c = [5,6]
d = [1,6]

how can i find the exact number of common values of a with b,c,d in elasticsearch?

I would expect:

b -> 2
c -> 0
d -> 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2025-02-06 15:03:38

让您考虑以下是您的Elasticsearch文档,

{
  "b": [1,3],
  "c": [5,6],
  "d": [1,6]
}

您的 elasticsearch Query 将看起来如下:

在这里,您需要使用第一个术语聚合,并且需要应用SUM_BUCKET聚合。

{
  "size": 0,
  "aggs": {
    "b_count": {
      "terms": {
        "field": "b",
        "size": 10,
        "include": [1,2,3,4]
      }
    },
    "c_count": {
      "terms": {
        "field": "c",
        "size": 10,
        "include": [1,2,3,4]
      }
    },
    "d_count": {
      "terms": {
        "field": "d",
        "size": 10,
        "include": [1,2,3,4]
      }
    },
    "b_sum": {
      "sum_bucket": {
        "buckets_path": "b_count>_count"
      }
    },
    "c_sum": {
      "sum_bucket": {
        "buckets_path": "c_count>_count"
      }
    },
    "d_sum": {
      "sum_bucket": {
        "buckets_path": "d_count>_count"
      }
    }
  }
}

示例响应:

您可以使用b_sumc_sumd_sum的值。

"aggregations" : {
    "b_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1
        },
        {
          "key" : 3,
          "doc_count" : 1
        }
      ]
    },
    "d_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1
        }
      ]
    },
    "c_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ ]
    },
    "b_sum" : {
      "value" : 2.0
    },
    "c_sum" : {
      "value" : 0.0
    },
    "d_sum" : {
      "value" : 1.0
    }
  }

Let consider below is your elasticsearch document,

{
  "b": [1,3],
  "c": [5,6],
  "d": [1,6]
}

Your Elasticsearch Query will looks like below:

Here, You need to use first terms aggregation and above that you need to apply sum_bucket aggregation.

{
  "size": 0,
  "aggs": {
    "b_count": {
      "terms": {
        "field": "b",
        "size": 10,
        "include": [1,2,3,4]
      }
    },
    "c_count": {
      "terms": {
        "field": "c",
        "size": 10,
        "include": [1,2,3,4]
      }
    },
    "d_count": {
      "terms": {
        "field": "d",
        "size": 10,
        "include": [1,2,3,4]
      }
    },
    "b_sum": {
      "sum_bucket": {
        "buckets_path": "b_count>_count"
      }
    },
    "c_sum": {
      "sum_bucket": {
        "buckets_path": "c_count>_count"
      }
    },
    "d_sum": {
      "sum_bucket": {
        "buckets_path": "d_count>_count"
      }
    }
  }
}

Sample Response:

You can use value of b_sum, c_sum and d_sum from below response.

"aggregations" : {
    "b_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1
        },
        {
          "key" : 3,
          "doc_count" : 1
        }
      ]
    },
    "d_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1
        }
      ]
    },
    "c_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ ]
    },
    "b_sum" : {
      "value" : 2.0
    },
    "c_sum" : {
      "value" : 0.0
    },
    "d_sum" : {
      "value" : 1.0
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文