美文网首页
elasticsearch terms aggs初探

elasticsearch terms aggs初探

作者: 小木胆 | 来源:发表于2017-04-26 10:07 被阅读0次

通过elasticsearch的aggs,就可以方便的对数据进行初步的统计。比如结合terms的bucket。就可以统计出某个field的所有出现过的type,类似于mongodb的distinct。但是如果此字段不是一个单词,而是一个列表或者其他类型,文档里就没有讲。这里尝试了一下:

curl -XPOST 'http://192.168.1.107:9200/cars/transactions/_bulk?pretty' -d'
{ "index": {}}
{ "price" : 10000, "color" : ["red", "green"], "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red golden", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : ["green", "pink"], "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red yellow", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }'

这里color有单词,字符串和列表,然后运行一下aggs

curl -XGET 'http://192.168.1.107:9200/cars/transactions/_search?pretty' -d'
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            }
        }
    }
}'

结果如下:

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 21,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "popular_colors" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "red",
        "doc_count" : 12
      }, {
        "key" : "green",
        "doc_count" : 8
      }, {
        "key" : "blue",
        "doc_count" : 3
      }, {
        "key" : "pink",
        "doc_count" : 2
      }, {
        "key" : "golden",
        "doc_count" : 1
      }, {
        "key" : "yellow",
        "doc_count" : 1
      } ]
    }
  }
}

实际上位于列表和字符串里的golden, 位于列表中的pink和green都被正确的检索到了。看来elasticsearch的aggs功能相当智能。

相关文章

  • elasticsearch terms aggs初探

    通过elasticsearch的aggs,就可以方便的对数据进行初步的统计。比如结合terms的bucket。就可...

  • elasticsearch之aggs

    聚合 聚合表示的是对某个索引(index)下的所有文档进行一些统计之类的操作,如下图1所示,将左边索引中的文档按照...

  • Elasticsearch(七)聚合分析

    聚合分析对应数据库中的聚合函数。在 Elasticsearch 中使用 aggs 标签表示。 计算所有商品的价格总...

  • ElasticSearch初探

    本文仅对elasticSearch(ES)进行初步介绍,有不足与错误之处欢迎指正。本文仅介绍了ES某些特性,希望能...

  • Elasticsearch初探

    Elasticsearch安装 读者可以参考:Downloads Elasticsearch 下载 启动 遇到的一...

  • Elasticsearch 初探

    ElasticSearch 是一个基于 Lucene的 搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基...

  • ElasticSearch初探

    一、单机版ElasticSearch 对于基于 RedHat 的发行版,在 /etc/yum.repos.d/ 目...

  • Elasticsearch初探

    Elasticsearch是什么 Elasticsearch是一个基于Apache Lucene(TM)的一个分布...

  • elasticsearch bucket 之rare terms

    1、背景 我们知道当我们使用 terms聚合时,当修改默认顺序为_count asc时,统计的结果是不准备的,而且...

  • Elasticsearch 聚合查询(aggs)基本概念 ---

    ES中的聚合查询,类似SQL的SUM/AVG/COUNT/GROUP BY分组查询,主要用于统计分析场景。 下面先...

网友评论

      本文标题:elasticsearch terms aggs初探

      本文链接:https://www.haomeiwen.com/subject/orcizttx.html