elasticsearch获得唯一的值,并且这些值在多个字段中计数 [英] elasticsearch get unique values, and the values count on multiple fields

查看:49
本文介绍了elasticsearch获得唯一的值,并且这些值在多个字段中计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个字段中获得唯一值的列表,同时计算每个唯一值在两个不同字段中出现了多少次.

I want to get the list of unique values from a field, and at the same time, count how many times each of those unique values have appeared in two different fields.

假设我有一个既可以购买又可以出售的产品.我想查询:在 sold 字段中出现过的唯一值,并且,计算每个值在 sold border中都出现了多少次字段.

Let's say I have a product that can be both bought and sold. I want to query: Unique values that have appeared in the field sold, AND, count how many times each value has appeared in both sold, and bought fields.

到目前为止,我最接近的是 terms 聚合,我得到了两个值,但是在两个不同的存储桶中:

The closest I came so far is terms aggregation I get to have both values, but in two different buckets:

{
  "aggs": {
    "sold": {
      "terms":{
        "field": "productname.sold.keyword",
        "size": 1000
      }
    },
    "bought": {
      "terms":{
        "field": "product.bought.keyword",
        "size": 1000
      }
    }
  }
}

但这给了我两个分开的桶中的结果.我理想的输出是这样的:

But it gives me the result in two seperate buckets. My ideal output is like this:

  "aggregations": {
    "product_stat": {
      "key": "<product>"
      "sold": "<#>"
      "bought": "<#>"
    }
  }

我应该如何构成我的查询才能实现?我想获得已售出的唯一价值,并计算该价值出现在已售和已购字段中的次数.

How should I form my query to achieve so? I want to get unique values of sold, and count how many times that value has appeared in sold and bought fields.

推荐答案

尝试:

  body = {
  "size": 0,
  "aggs": {
    "sold": {
      "terms": {
        "field": "product.sold.keyword",
        "size": 40000
      },
      "aggs": {
        "bought": {
          "terms": {
            "field": "product.bought.keyword",
            "size": 40000
          }
        }
      }
    }
  }
}

这篇关于elasticsearch获得唯一的值,并且这些值在多个字段中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆