elatingsearch 详细文档

分享 123456789987654321 ⋅ 于 2020-06-03 10:34:52 ⋅ 最后回复由 青牛 2020-06-03 18:42:10 ⋅ 1675 阅读

ElasticSearch的restful参考文档 一. 索引库

1.1.创建索引库 Create Indexe

创建索引的请求格式:

请求方式:PUT

请求路径:/索引库名

请求参数:json格式:

PUT /heima
{
    "settings": {
        "属性名": "属性值"
     }
 }

settings:就是索引库设置,其中可以定义索引库的各种属性,目前我们可以不设置,都走默认。

1.2、查看索引库 Get Index

请求方式:GET

请求路径:/索引库名

语法:GET /索引库名

示例:

GET /heima

1.3、删除索引库 delete index

请求方式:DELETE

请求路径:/索引库名

语法:DELETE /索引库名

示例:

DELETE /heima

二. 类型及映射操作

2.1、 创建映射关系

语法:

PUT /索引库名/_mapping/类型名称
{
 "properties": {
   "字段名": {
     "type": "类型",
     "index": true,
     "store": true,
     "analyzer": "分词器"
   }
 }
 }

· 类型名称:就是前面将的type的概念,类似于数据库中的表 字段名:任意填写,下面指定许多属性,例如:

  • type:类型,可以是text、long、short、date、integer、object等
  • index:是否索引,默认为true
  • store:是否存储,默认为false
  • analyzer:分词器,这里的ik_max_word即使用ik分词器

示例

发起请求:

PUT heima/_mapping/goods

{
 "properties": {
   "title": {
     "type": "text",
     "analyzer": "ik_max_word"
  },
   "images": {
     "type": "keyword",
     "index": "false"
  },
   "price": {
     "type": "float"
  }
}
}

响应结果:

{
 "acknowledged": true
 }

上述案例中,就给heima这个索引库添加了一个名为goods的类型,并且在类型中设置了3个字段:

  • title:商品标题
  • images:商品图片
  • price:商品价格

2.2、查看映射关系

语法:

GET /索引库名/_mapping

查看某个索引库中的所有类型的映射。如果要查看某个类型映射,可以再路径后面跟上类型名称。即:

GET /索引库名/_mapping/映射名 或者 GET /heima/_mapping/goods

示例:

GET /heima/_mapping

响应:

{
 "heima": {
   "mappings": {
     "goods": {
       "properties": {
         "images": {
           "type": "keyword",
           "index": false
        },
         "price": {
           "type": "float"
        },
         "title": {
           "type": "text",
           "analyzer": "ik_max_word"
        }
      }
    }
  }
}
}

2.3、一次创建索引库和类型

基本语法:

put /索引库名
{
   "settings":{
       "索引库属性名":"索引库属性值"
  },
   "mappings":{
       "类型名":{
           "properties":{
               "字段名":{
                   "映射属性名":"映射属性值"
              }
          }
      }
  }
}

示例:

PUT /heima2
{
 "settings": {}, 
 "mappings": {
   "goods": {
     "properties": {
       "title": {
         "type": "text",
         "analyzer": "ik_max_word"
      }
    }
  }
}
}

三. 文档操作

3.1、新增文档

语法:

POST /索引库名/类型/id值

{

  ...

}

示例:

POST /heima/goods/2

{

   "title":"大米shouj",

   "images":"hxxp://image.leyou.com/12479122.jpg",

   "price":2899.00

}

或者

POST /heima/goods/

{

   "title":"小米shouj",

   "images":"hxxp://image.leyou.com/12479122.jpg",

   "price":2699.00

}

3.2、查看文档

语法:

GET /heima/goods/2

查看结果:

{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_version": 1,
"found": true,
"_source": {
"title": "小米shouj",
"images": "hxxp://image.leyou.com/12479122.jpg",
"price": 2699
}
}

3.3、修改文档

把新增的请求方式改为PUT,就是修改了。不过修改必须指定id,

· id对应文档存在,则修改

· id对应文档不存在,则新增

比如,我们把使用id为3,不存在,则应该是新增:

PUT /heima/goods/3
{
"title":"超米shouj",
"images":"hxxp://image.leyou.com/12479122.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}

我们再次执行刚才的请求,不过把数据改一下:

PUT /heima/goods/3

{
   "title":"超大米sj",
   "images":"hxxp://image.leyou.com/12479122.jpg",
   "price":3299.00,
   "stock": 100,
   "saleable":true
}

查看结果:

{
 "_index": "heima",
 "_type": "goods",
 "_id": "3",
 "_version": 2,
 "result": "updated",
 "_shards": {
   "total": 2,
   "successful": 1,
   "failed": 0
},
 "_seq_no": 2,
 "_primary_term": 1
}

可以看到结果是:updated,显然是更新数据

3.4、删除文档

删除使用DELETE请求,同样,需要根据id进行删除:

语法

DELETE /索引库名/类型名/id值

示例:

DELETE /heima/goods/3

四. 文档查询

基本语法

GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}

这里的query代表一个查询对象,里面可以有不同的查询属性

· 查询类型:

o 例如:match_all, match,term , range 等等

4.1、查询所有(match_all)

示例:

GET /heima/_searc
{
   "query":{
       "match_all": {}
  }
}

· query:代表查询对象

· match_all:代表查询所有

4.2、匹配查询(match)

match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系

GET /heima/_search

{

   "query":{

       "match":{

           "title":"小米电视"

      }

  }

}

4.3、词条匹配(term)

term 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串

GET /heima/_search

{

   "query":{

       "term":{

           "price":2699.00

      }

  }

}

4.4、模糊查询 (fuzzy)

我们新增一个商品:

POST /heima/goods/4

{

   "title":"applesj",

   "images":"hxxp://image.leyou.com/12479122.jpg",

   "price":6899.00

}

fuzzy 查询是 term 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:

GET /heima/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}

上面的查询,也能查询到appleshouj

我们可以通过fuzziness来指定允许的编辑距离:

GET /heima/_search

{

 "query": {

   "fuzzy": {

       "title": {

           "value":"appla",

           "fuzziness":1

      }

  }

}

}

4.5、范围查询 (range)

range 查询找出那些落在指定区间内的数字或者时间

GET /heima/_search

{

   "query":{

       "range": {

           "price": {

               "gte":  1000.0,

               "lt":   2800.00

          }

  }

  }

}

range查询允许以下字符:

操作符 说明
gt 大于
gte 大于等于
lt 小于
lte 小于等于

4.6、布尔组合 (bool)

bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合

GET /heima/_search

{

   "query":{

       "bool":{

      "must":     { "match": { "title": "大米" }},

      "must_not": { "match": { "title":  "电视" }},

      "should":   { "match": { "title": "shouj" }},
      "filter":

      }

  }

}

结果:

{

 "took": 10,

 "timed_out": false,

 "_shards": {

   "total": 3,

   "successful": 3,

   "skipped": 0,

   "failed": 0

},

 "hits": {

   "total": 1,

   "max_score": 0.5753642,

   "hits": [

    {

       "_index": "heima",

       "_type": "goods",

       "_id": "2",

       "_score": 0.5753642,

       "_source": {

         "title": "大米shouj",

         "images": "hxxp://image.leyou.com/12479122.jpg",

         "price": 2899

      }

    }

  ]

}

}

4.7、结果过滤

默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。

如果我们只想获取其中的部分字段,我们可以添加_source的过滤

4.7.1、直接指定字段

示例:

GET /heima/_search

{

 "_source": ["title","price"],

 "query": {

   "term": {

     "price": 2699

  }

}

}

返回的结果:

{

 "took": 12,

 "timed_out": false,

 "_shards": {

   "total": 3,

   "successful": 3,

   "skipped": 0,

   "failed": 0

},

 "hits": {

   "total": 1,

   "max_score": 1,

   "hits": [

    {

       "_index": "heima",

       "_type": "goods",

       "_id": "r9c1KGMBIhaxtY5rlRKv",

       "_score": 1,

       "_source": {

         "price": 2699,

         "title": "小米shouj"

      }

    }

  ]

}

}

4.7.2、指定includes和excludes

我们也可以通过:

· includes:来指定想要显示的字段

· excludes:来指定不想要显示的字段

二者都是可选的。

示例:

GET /heima/_search

{

 "_source": {

   "includes":["title","price"]

},

 "query": {

   "term": {

     "price": 2699

  }

}

}

与下面的结果将是一样的:

GET /heima/_search

{

 "_source": {

    "excludes": ["images"]

},

 "query": {

   "term": {

     "price": 2699

  }

}

}

4.8、查询过滤(filter)

4.8.1、条件查询中进行过滤

所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter方式:

GET /heima/_search
{
   "query":{
       "bool":{
      "must":{ "match": { "title": "小米shouj" }},
      "filter":{
               "range":{"price":{"gt":2000.00,"lt":3800.00}}
      }
      }
  }
}

4.8.2、无查询条件,直接过滤

如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。

GET /heima/_search
{
   "query":{
       "constant_score":   {
           "filter": {
          "range":{"price":{"gt":2000.00,"lt":3000.00}}
          }
      }
}
}

4.9、排序

4.9.1、单字段排序

sort 可以让我们按照不同的字段进行排序,并且通过order指定排序的方式

GET /heima/_search
{
 "query": {
   "match": {
     "title": "小米shouj"
  }
},
 "sort": [
  {
     "price": {
       "order": "desc"
    }
  }
]
}

4.9.2、多字段排序

假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:

GET /heima/_search
{
   "query":{
       "bool":{
      "must":{ "match": { "title": "小米shouj" }},
      "filter":{
               "range":{"price":{"gt":2000,"lt":3000}}
      }
      }
  },
   "sort": [
    { "price": { "order": "desc" }},
    { "_score": { "order": "desc" }}
  ]
}

4.10、分页

elasticsearch的分页与mysql数据库非常相似,都是指定两个值:

· from:开始位置

· size:每页大小

GET /heima/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ],
  "from": 2,
  "size": 3
}

结果:

{

 "took": 4,

 "timed_out": false,

 "_shards": {

   "total": 5,

   "successful": 5,

   "skipped": 0,

   "failed": 0

},

 "hits": {

   "total": 4,

   "max_score": null,

   "hits": [

    {

       "_index": "heima",

       "_type": "goods",

       "_id": "4",

       "_score": null,

       "_source": {

         "title": "小米电视4A",

         "images": "hxxp://image.leyou.com/12479122.jpg",

         "price": 3899

      },

       "sort": [

         3899

      ]

    }

  ]

}

}

4.11、高亮

高亮原理:

· 服务端搜索数据,得到搜索结果

· 把搜索结果中,搜索关键字都加上约定好的标签

· 前端页面提前写好标签的CSS样式,即可高亮

elasticsearch中实现高亮的语法比较简单:

GET /heima/_search

{

 "query": {

   "match": {

     "title": "shouj"

  }

},

 "highlight": {

   "pre_tags": "<em>",

   "post_tags": "</em>", 

   "fields": {

     "title": {}

  }

}

}

在使用match查询的同时,加上一个highlight属性:

· pre_tags:前置标签

· post_tags:后置标签

· fields:需要高亮的字段

o title:这里声明title字段需要高亮,后面可以为这个字段设置特有配置,也可以空

结果:

{

 "took": 8,

 "timed_out": false,

 "_shards": {

   "total": 5,

   "successful": 5,

   "skipped": 0,

   "failed": 0

},

 "hits": {

   "total": 3,

   "max_score": 0.2876821,

   "hits": [

    {

       "_index": "heima",

       "_type": "goods",

       "_id": "2",

       "_score": 0.2876821,

       "_source": {

         "title": "大米shouj",

         "images": "hxxp://image.leyou.com/12479122.jpg",

         "price": 2899

      },

       "highlight": {

         "title": [

           "大米<em>shouj</em>"

        ]

      }

    },

    {

       "_index": "heima",

       "_type": "goods",

       "_id": "JP6xa2kBtq36Pzvvvvvvpjaf",

       "_score": 0.19856805,

       "_source": {

         "title": "小米shouj",

         "images": "hxxp://image.leyou.com/12479122.jpg",

         "price": 2699

      },

       "highlight": {

         "title": [

           "小米<em>shouj</em>"

        ]

      }

    },

    {

       "_index": "heima",

       "_type": "goods",

       "_id": "3",

       "_score": 0.16853254,

       "_source": {

         "title": "超大米shouj",

         "images": "hxxp://image.leyou.com/12479122.jpg",

         "price": 3299,

         "stock": 200,

         "saleable": true,

         "subTitle": "哈哈"

      },

       "highlight": {

         "title": [

           "超大米<em>shouj</em>"

        ]

      }

    }

  ]

}

}

五. 聚合aggregations

聚合可以让我们极其方便的实现对数据的统计、分析。例如:

  • 什么品牌的shouj最受欢迎?
  • 这些shouj的平均价格、最高价格、最低价格?
  • 这些shouj每月的xs情况如何?

实现这些统计功能的比数据库的sql要方便的多,而且查询速度非常快,可以实现近实时搜索效果。

5.1 基本概念

Elasticsearch中的聚合,包含多种类型,最常用的两种,一个叫,一个叫度量

桶(bucket)

桶的作用,是按照某种方式对数据进行分组,每一组数据在ES中称为一个,例如我们根据国籍对人划分,可以得到中国桶英国桶日本桶……或者我们按照年龄段对人进行划分:0~10,10~20,20~30,30~40等。

Elasticsearch中提供的划分桶的方式有很多:

  • Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组
  • Histogram Aggregation:根据数值阶梯分组,与日期类似,需要知道分组的间隔(interval)
  • Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
  • Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组
  • ……

综上所述,我们发现bucket aggregations 只负责对数据进行分组,并不进行计算,因此往往bucket中往往会嵌套另一种聚合:metrics aggregations即度量

度量(metrics)

分组完成以后,我们一般会对组中的数据进行聚合运算,例如求平均值、最大、最小、求和等,这些在ES中称为度量

比较常用的一些度量聚合方式:

  • Avg Aggregation:求平均值
  • Max Aggregation:求最大值
  • Min Aggregation:求最小值
  • Percentiles Aggregation:求百分比
  • Stats Aggregation:同时返回avg、max、min、sum、count等
  • Sum Aggregation:求和
  • Top hits Aggregation:求前几
  • Value Count Aggregation:求总数
  • ……

为了测试聚合,我们先批量导入一些数据

创建索引:

PUT /car
{
  "mappings": {
    "orders": {
      "properties": {
        "color": {
          "type": "keyword"
        },
        "make": {
          "type": "keyword"
        }
      }
    }
  }
}

注意:在ES中,需要进行聚合、排序、过滤的字段其处理方式比较特殊,因此不能被分词,必须使用keyword数值类型。这里我们将color和make这两个文字类型的字段设置为keyword类型,这个类型不会被分词,将来就可以参与聚合

导入数据,这里是采用批处理的API,大家直接复制到kibana运行即可:

POST /car/orders/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "红", "make" : "本田", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "红", "make" : "本田", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "绿", "make" : "福特", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "蓝", "make" : "丰田", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "绿", "make" : "丰田", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "红", "make" : "本田", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "红", "make" : "宝马", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "蓝", "make" : "福特", "sold" : "2014-02-12" }

5.2 聚合为桶

首先,我们按照 汽车的颜色color来划分,按照颜色分桶,最好是使用TermAggregation类型,按照颜色的名称来分桶。

GET /car/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            }
        }
    }
}
  • size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
  • aggs:声明这是一个聚合查询,是aggregations的缩写
    • popular_colors:给这次聚合起一个名字,可任意指定。
    • terms:聚合的类型,这里选择terms,是根据词条内容(这里是颜色)划分
      • field:划分桶时依赖的字段

结果:

{
  "took": 33,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "红",
          "doc_count": 4
        },
        {
          "key": "绿",
          "doc_count": 2
        },
        {
          "key": "蓝",
          "doc_count": 2
        }
      ]
    }
  }
}
  • hits:查询结果为空,因为我们设置了size为0
  • aggregations:聚合的结果
  • popular_colors:我们定义的聚合名称
  • buckets:查找到的桶,每个不同的color字段值都会形成一个桶
    • key:这个桶对应的color字段的值
    • doc_count:这个桶中的文档数量

通过聚合的结果我们发现,目前红色的小车比较畅销!

5.3 桶内度量

前面的例子告诉我们每个桶里面的文档数量,这很有用。 但通常,我们的应用需要提供更复杂的文档度量。 例如,每种颜色汽车的平均价格是多少?

因此,我们需要告诉Elasticsearch使用哪个字段使用何种度量方式进行运算,这些xx息要嵌套在内,度量的运算会基于内的文档进行

现在,我们为刚刚的聚合结果添加 求价格平均值的度量:

GET /car/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            },
            "aggs":{
                "avg_price": { 
                   "avg": {
                      "field": "price" 
                   }
                }
            }
        }
    }
}
  • aggs:我们在上一个aggs(popular_colors)中添加新的aggs。可见度量也是一个聚合
  • avg_price:聚合的名称
  • avg:度量的类型,这里是求平均值
  • field:度量运算的字段

结果:

{
  "took": 23,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "红",
          "doc_count": 4,
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "绿",
          "doc_count": 2,
          "avg_price": {
            "value": 21000
          }
        },
        {
          "key": "蓝",
          "doc_count": 2,
          "avg_price": {
            "value": 20000
          }
        }
      ]
    }
  }
}

可以看到每个桶中都有自己的avg_price字段,这是度量聚合的结果

5.4 桶内嵌套桶

刚刚的案例中,我们在桶内嵌套度量运算。事实上桶不仅可以嵌套运算, 还可以再嵌套其它桶。也就是说在每个分组中,再分更多组。

比如:我们想统计每种颜色的汽车中,分别属于哪个制造商,按照make字段再进行分桶

GET /car/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            },
            "aggs":{
                "avg_price": { 
                   "avg": {
                      "field": "price" 
                   }
                },
                "maker":{
                    "terms":{
                        "field":"make"
                    }
                }
            }
        }
    }
}
  • 原来的color桶和avg计算我们不变
  • maker:在嵌套的aggs下新添一个桶,叫做maker
  • terms:桶的划分类型依然是词条
  • filed:这里根据make字段进行划分

部分结果:

{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "红",
          "doc_count": 4,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "本田",
                "doc_count": 3
              },
              {
                "key": "宝马",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "绿",
          "doc_count": 2,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "丰田",
                "doc_count": 1
              },
              {
                "key": "福特",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 21000
          }
        },
        {
          "key": "蓝",
          "doc_count": 2,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "丰田",
                "doc_count": 1
              },
              {
                "key": "福特",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 20000
          }
        }
      ]
    }
  }
}
  • 我们可以看到,新的聚合maker被嵌套在原来每一个color的桶中。
  • 每个颜色下面都根据 make字段进行了分组
  • 我们能读取到的xx息:
    • 红色车共有4辆
    • 红色车的平均售价是 $32,500 美元。
    • 其中3辆是 Honda 本田制造,1辆是 BMW 宝马制造。

5.5.划分桶的其它方式

前面讲了,划分桶的方式有很多,例如:

  • Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组
  • Histogram Aggregation:根据数值阶梯分组,与日期类似
  • Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
  • Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组

刚刚的案例中,我们采用的是Terms Aggregation,即根据词条划分桶。

接下来,我们再学习几个比较实用的:

5.5.1.阶梯分桶Histogram

原理:

histogram是把数值类型的字段,按照一定的阶梯大小进行分组。你需要指定一个阶梯值(interval)来划分阶梯大小。

举例:

比如你有价格字段,如果你设定interval的值为200,那么阶梯就会是这样的:

0,200,400,600,...

上面列出的是每个阶梯的key,也是区间的启点。

如果一件商品的价格是450,会落入哪个阶梯区间呢?计算公式如下:

bucket_key = Math.floor((value - offset) / interval) * interval + offset

value:就是当前数据的值,本例中是450

offset:起始偏移量,默认为0

interval:阶梯间隔,比如200

因此你得到的key = Math.floor((450 - 0) / 200) * 200 + 0 = 400

操作一下:

比如,我们对汽车的价格进行分组,指定间隔interval为5000:

GET /cars/_search
{
  "size":0,
  "aggs":{
    "price":{
      "histogram": {
        "field": "price",
        "interval": 5000
      }
    }
  }
}

结果:

{
  "took": 21,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price": {
      "buckets": [
        {
          "key": 10000,
          "doc_count": 2
        },
        {
          "key": 15000,
          "doc_count": 1
        },
        {
          "key": 20000,
          "doc_count": 2
        },
        {
          "key": 25000,
          "doc_count": 1
        },
        {
          "key": 30000,
          "doc_count": 1
        },
        {
          "key": 35000,
          "doc_count": 0
        },
        {
          "key": 40000,
          "doc_count": 0
        },
        {
          "key": 45000,
          "doc_count": 0
        },
        {
          "key": 50000,
          "doc_count": 0
        },
        {
          "key": 55000,
          "doc_count": 0
        },
        {
          "key": 60000,
          "doc_count": 0
        },
        {
          "key": 65000,
          "doc_count": 0
        },
        {
          "key": 70000,
          "doc_count": 0
        },
        {
          "key": 75000,
          "doc_count": 0
        },
        {
          "key": 80000,
          "doc_count": 1
        }
      ]
    }
  }
}

你会发现,中间有大量的文档数量为0 的桶,看起来很丑。

我们可以增加一个参数min_doc_count为1,来约束最少文档数量为1,这样文档数量为0的桶会被过滤

示例:

GET /cars/_search
{
  "size":0,
  "aggs":{
    "price":{
      "histogram": {
        "field": "price",
        "interval": 5000,
        "min_doc_count": 1
      }
    }
  }
}

结果:

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price": {
      "buckets": [
        {
          "key": 10000,
          "doc_count": 2
        },
        {
          "key": 15000,
          "doc_count": 1
        },
        {
          "key": 20000,
          "doc_count": 2
        },
        {
          "key": 25000,
          "doc_count": 1
        },
        {
          "key": 30000,
          "doc_count": 1
        },
        {
          "key": 80000,
          "doc_count": 1
        }
      ]
    }
  }
}

完美,!

如果你用kibana将结果变为柱形图,会更好看:

5.5.2.范围分桶range

范围分桶与阶梯分桶类似,也是把数字按照阶段进行分组,只不过range方式需要你自己指定每一组的起始和结束大小。

版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海汼部落-123456789987654321,http://hainiubl.com/topics/75110
回复数量: 2
  • 青牛 国内首批大数据从业者,就职于金山,担任大数据团队核心研发工程师
    2020-06-03 18:39:52

    基础类文章,总结的还算全,补充一下: mapping还有一个动态映射和模板的概念,这个两个也比较重要。打赏1元以资鼓励:+1:

  • 青牛 国内首批大数据从业者,就职于金山,担任大数据团队核心研发工程师
    2020-06-03 18:42:10

    同学你的码放的不对啊,扫不上:bowtie:

暂无评论~~
  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
Ctrl+Enter