在给定过滤器的弹性搜索中获得不同的值

时间:2018-07-22 11:04:06

标签: elasticsearch

我想在给定过滤器的索引中获取不同的字段,例如
对于以下数据

|Name      |Age |Country|Amount|
================================
|Alfreds   |34  |Germeny|100   |
--------------------------------
|Andrew    |43  |U.K    |333   |
--------------------------------
|Raj       |54  |India  |4553  |
--------------------------------
|John      |43  |U.K    |325   |
--------------------------------
|Ana       |32  |Mexico |56    |
--------------------------------
|Christina |22  |Sweden |345   |

我想使用一个不同的国家/地区名称,例如,金额大于300。

实现相同目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

如果仅对获取不同值的数量感兴趣,可以使用字段聚合:

{
  "query": {
    "range": {
      "amount": {
        "gt": 300
      }
    }
  },
  "size": 0,
  "aggs" : {
    "uniq_country" : {
      "terms" : {"field" : "country"}
    }
  }
}

上面的查询将为您提供以下结果:

"aggregations": {
    "uniq_country": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "u.k",
          "doc_count": 2
        },
        {
          "key": "india",
          "doc_count": 1
        },
        {
          "key": "sweden",
          "doc_count": 1
        }
      ]
    }
  }
相关问题