es - elasticsearch mapping - field data type - 14

131 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :histogram有什么特点?
答 :
在这里插入图片描述

问 :histogram如何使用?
答 :

# histogram
PUT /histogram_test
{
  "mappings" : {
    "properties" : {
      "my_histogram" : {"type" : "histogram"}
    }
  }
}

# 索引
POST /histogram_test/_doc
{
  "my_histogram" : {
    "values" : [0.1, 3.2, 45.3, 78.4, 99.5],
    "counts" : [3, 7, 23, 12, 6]
  }
}

# 索引
POST /histogram_test/_doc
{
  "my_histogram" : {
    "values" : [0.1, 34.25, 67.35, 89.4, 90.45, 97.5],
    "counts" : [8, 17, 8, 7, 6, 2]
  }
}

# 聚合
GET /histogram_test/_search?size=0
{
  "aggs" : {
    "my_aggs" : {
      "histogram" : {
        "field"    : "my_histogram",
        "interval" : 10
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_aggs" : {
      "buckets" : [
        {
          "key" : 0.0,
          "doc_count" : 18
        },
        {
          "key" : 10.0,
          "doc_count" : 0
        },
        {
          "key" : 20.0,
          "doc_count" : 0
        },
        {
          "key" : 30.0,
          "doc_count" : 17
        },
        {
          "key" : 40.0,
          "doc_count" : 23
        },
        {
          "key" : 50.0,
          "doc_count" : 0
        },
        {
          "key" : 60.0,
          "doc_count" : 8
        },
        {
          "key" : 70.0,
          "doc_count" : 12
        },
        {
          "key" : 80.0,
          "doc_count" : 7
        },
        {
          "key" : 90.0,
          "doc_count" : 14
        }
      ]
    }
  }
}