roc-rui

导航

Kibana基础之直接操作ElasticSearch

1.入门级别操作

Elasticsearch采用Rest风格API,其API就是一次http请求,你可以用任何工具发起http请求

创建索引的请求格式:

  • 请求方式:PUT

  • 请求路径:/索引库名

  • 请求参数:json格式:

    {
       "settings": {
           "属性名": "属性值"
        }
    }

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

可以看到索引创建成功了。

使用kibana创建索引库

kibana的控制台,可以对http请求进行简化,示例:

创建索引库

PUT /索引库名

省去了elasticsearch的服务器地址,而且还有语法提示,非常舒服。

查看索引库

语法

Get请求可以帮我们查看索引信息,格式:

GET /索引库名

删除索引使用DELETE请求

DELETE /索引库名

示例

再次查看hema2:

 

RestApi 类型及映射操作

有了索引库,等于有了数据库中的database。接下来就需要索引库中的类型了,也就是数据库中的。创建数据库表需要设置字段约束,索引库也一样,在创建索引库的类型时,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做字段映射(mapping)

字段的约束我们在学习Lucene中我们都见到过,包括到不限于:

  • 字段的数据类型

  • 是否要存储

  • 是否要索引

  • 是否分词

  • 分词器是什么

我们一起来看下创建的语法。

创建字段映射

语法

请求方式依然是PUT

PUT /索引库名/_mapping/类型名称
{
  "properties": {
    "字段名": {
      "type": "类型",
      "index": true,
      "store": true,
      "analyzer": "分词器"
    }
  }
}
  • 类型名称:就是前面将的type的概念,类似于数据库中的表字段名:任意填写,下面指定许多属性,例如:默认值可以通过查看@Field注解

    • type:类型,可以是text、long、short、date、integer、object等

    • index:是否索引,默认为true

    • store:是否存储,默认为false

    • analyzer:分词器,这里的ik_max_word即使用ik分词器

       

发起请求:

PUT hema/_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:商品价格

查看映射关系

语法:

GET /索引库名/_mapping

 

2. 进阶级别操作(操作一套完成文档的crud)

RestApi 文档操作

文档,即索引库中某个类型下的数据,会根据规则创建索引,将来用来搜索。可以类比做数据库中的每一行数据。

2.1新增文档
2.1.1新增并随机生成id

通过POST请求,可以向一个已经存在的索引库中添加文档数据。

语法:

POST /索引库名/类型名
{
    "key":"value"
}

示例:

POST /hema/goods/
{
   "title":"小米手机",
   "images":"http://image.leyou.com/12479122.jpg",
   "price":2699.00
}

响应:

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

可以看到结果显示为:created,应该是创建成功了。 AWwQDNsJ4VusCdwhuQD7

另外,需要注意的是,在响应结果中有个_id字段,这个就是这条文档数据的唯一标示,以后的增删改查都依赖这个id作为唯一标示。

可以看到id的值为:r9c1KGMBIhaxtY5rlRKv,这里我们新增时没有指定id,所以是ES帮我们随机生成的id。

2.1.2新增文档并自定义id

如果我们想要自己新增的时候指定id,可以这么做:

POST /索引库名/类型/id值
{
    ...
}

示例:

POST /hema/goods/2
{
   "title":"大米手机",
   "images":"http://image.leyou.com/12479122.jpg",
   "price":2899.00
}

得到的数据:

{
 "_index": "hema",
 "_type": "goods",
 "_id": "2",
 "_score": 1,
 "_source": {
   "title": "大米手机",
   "images": "http://image.leyou.com/12479122.jpg",
   "price": 2899
}
}
2.2查看文档

根据rest风格,新增是post,查询应该是get,不过查询一般都需要条件,这里我们把刚刚生成数据的id带上。

通过kibana查看数据:

GET /heima/goods/r9c1KGMBIhaxtY5rlRKv

查看结果:

{
 "_index": "hema",
 "_type": "goods",
 "_id": "r9c1KGMBIhaxtY5rlRKv",
 "_version": 1,
 "found": true,
 "_source": {
   "title": "小米手机",
   "images": "http://image.leyou.com/12479122.jpg",
   "price": 2699
}
}
  • _source:源文档信息,所有的数据都在里面。

  • _id:这条文档的唯一标示

2.3修改数据

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

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

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

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

PUT /hema/goods/3
{
   "title":"超米手机",
   "images":"http://image.leyou.com/12479122.jpg",
   "price":3899.00
}

结果:

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

可以看到是created,是新增。

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

PUT /hema/goods/3
{
   "title":"超大米手机",
   "images":"http://image.leyou.com/12479122.jpg",
   "price":3299.00,
   "stock": 100,
   "saleable":true
}

查看结果:

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

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

 

2.4删除数据

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

语法

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

示例:

 

3.查询

我们从4块来讲查询:

  • 基本查询

  • _source过滤

  • 结果过滤

  • 高级查询

  • 排序

5.1.基本查询:

基本语法

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

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

  • 查询类型:

    • 例如:match_all, matchterm , range 等等

  • 查询条件:查询条件会根据类型的不同,写法也有差异,后面详细讲解

 

5.1.1 查询所有(match_all)

示例:

GET /hema/_search
{
   "query":{
       "match_all": {}
  }
}
  • query:代表查询对象

  • match_all:代表查询所有

结果:

{
 "took": 2,
 "timed_out": false,
 "_shards": {
   "total": 3,
   "successful": 3,
   "skipped": 0,
   "failed": 0
},
 "hits": {
   "total": 2,
   "max_score": 1,
   "hits": [
    {
       "_index": "hema",
       "_type": "goods",
       "_id": "2",
       "_score": 1,
       "_source": {
         "title": "大米手机",
         "images": "http://image.leyou.com/12479122.jpg",
         "price": 2899
      }
    },
    {
       "_index": "hema",
       "_type": "goods",
       "_id": "r9c1KGMBIhaxtY5rlRKv",
       "_score": 1,
       "_source": {
         "title": "小米手机",
         "images": "http://image.leyou.com/12479122.jpg",
         "price": 2699
      }
    }
  ]
}
}
  • took:查询花费时间,单位是毫秒

  • time_out:是否超时

  • _shards:分片信息

  • hits:搜索结果总览对象

    • total:搜索到的总条数

    • max_score:所有结果中文档得分的最高分

    • hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息

      • _index:索引库

      • _type:文档类型

      • _id:文档id

      • _score:文档得分

      • _source:文档的源数据

 

5.1.2 匹配查询(match)

我们先加入一条数据,便于测试:

PUT /hema/goods/3
{
   "title":"小米电视4A",
   "images":"http://image.leyou.com/12479122.jpg",
   "price":3899.00
}

现在,索引库中有2部手机,1台电视:

  • or关系

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

GET /hema/_search
{
   "query":{
       "match":{
           "title":"小米电视"
      }
  }
}

结果:

 

在上面的案例中,不仅会查询到电视,而且与小米相关的都会查询到,多个词之间是or的关系。

 

  • and关系

某些情况下,我们需要更精确查找,我们希望这个关系变成and,可以这样做:

GET hema/goods/_search
{
   "query":{
       "match":{
           "title":{"query":"小米电视","operator":"and"}
      }
  }
}

结果:

本例中,只有同时包含小米电视的词条才会被搜索到。

 

5.1.3 词条匹配(term)

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

GET /hema/_search
{
   "query":{
       "term":{
           "price":2699.00
      }
  }
}

结果:

{
 "took": 2,
 "timed_out": false,
 "_shards": {
   "total": 3,
   "successful": 3,
   "skipped": 0,
   "failed": 0
},
 "hits": {
   "total": 1,
   "max_score": 1,
   "hits": [
    {
       "_index": "hema",
       "_type": "goods",
       "_id": "r9c1KGMBIhaxtY5rlRKv",
       "_score": 1,
       "_source": {
         "title": "小米手机",
         "images": "http://image.leyou.com/12479122.jpg",
         "price": 2699
      }
    }
  ]
}
}

 

5.1.4 布尔组合(bool)

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

GET /hema/_search
{
   "query":{
       "bool":{
      "must":     { "match": { "title": "大米" }},
      "must_not": { "match": { "title":  "电视" }},
      "should":   { "match": { "title": "手机" }}
      }
  }
}

结果:

{
 "took": 10,
 "timed_out": false,
 "_shards": {
   "total": 3,
   "successful": 3,
   "skipped": 0,
   "failed": 0
},
 "hits": {
   "total": 1,
   "max_score": 0.5753642,
   "hits": [
    {
       "_index": "hema",
       "_type": "goods",
       "_id": "2",
       "_score": 0.5753642,
       "_source": {
         "title": "大米手机",
         "images": "http://image.leyou.com/12479122.jpg",
         "price": 2899
      }
    }
  ]
}
}

 

 

5.1.5 范围查询(range)

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

GET /hema/_search
{
   "query":{
       "range": {
           "price": {
               "gte":  1000.0,
               "lt":   2800.00
          }
  }
  }
}

range查询允许以下字符:

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

 

5.2.结果过滤

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

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

5.2.1.直接指定字段

示例:

GET /hema/_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": "hema",
       "_type": "goods",
       "_id": "r9c1KGMBIhaxtY5rlRKv",
       "_score": 1,
       "_source": {
         "price": 2699,
         "title": "小米手机"
      }
    }
  ]
}
}

 

5.2.2.指定includes和excludes

我们也可以通过:

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

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

二者都是可选的。

示例:

GET /hema/_search
{
 "_source": {
   "includes":["title","price"]
},
 "query": {
   "term": {
     "price": 2699
  }
}
}

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

GET /hema/_search
{
 "_source": {
    "excludes": ["images"]
},
 "query": {
   "term": {
     "price": 2699
  }
}
}

 

 

5.3 过滤(filter)

条件查询中进行过滤

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

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

 

 

5.4 排序

5.5.1 单字段排序

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

GET /hema/_search
{
 "query": {
   "match": {
     "title": "小米手机"
  }
},
 "sort": [
  {"price": { "order": "desc"}
  }
]
}

 

5.5.分页

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

  • from:开始位置 从0开始

  • size:每页大小

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

 

5.6.高亮

高亮原理:

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

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

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

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

GET /hema/_search
{
 "query": {
   "match": {
     "title": "手机"
  }
},
 "highlight": {
   "pre_tags": "<em>",
   "post_tags": "</em>",
   "fields": {
     "title": {}
  }
}
}

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

  • pre_tags:前置标签

  • post_tags:后置标签

  • fields:需要高亮的字段

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

 

6. 聚合aggregations

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

  • 什么品牌的手机最受欢迎?

  • 这些手机的平均价格、最高价格、最低价格?

  • 这些手机每月的销售情况如何?

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

6.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运行即可:

_bulk批量操作

POST /car/orders/_bulk
{ "index": {"_id":"1"}}
{ "price" : 10000, "color" : "红", "make" : "本田", "sold" : "2014-10-28" }
{ "index": {"_id":"2"}}
{ "price" : 20000, "color" : "红", "make" : "本田", "sold" : "2014-11-05" }
{ "index": {"_id":"3"}}
{ "price" : 30000, "color" : "绿", "make" : "福特", "sold" : "2014-05-18" }
{ "index": {"_id":"4"}}
{ "price" : 15000, "color" : "蓝", "make" : "丰田", "sold" : "2014-07-02" }
{ "index": {"_id":"5"}}
{ "price" : 12000, "color" : "绿", "make" : "丰田", "sold" : "2014-08-19" }
{ "index": {"_id":"6"}}
{ "price" : 20000, "color" : "红", "make" : "本田", "sold" : "2014-11-05" }
{ "index": {"_id":"7"}}
{ "price" : 80000, "color" : "红", "make" : "宝马", "sold" : "2014-01-01" }
{ "index": {"_id":"8"}}
{ "price" : 25000, "color" : "蓝", "make" : "福特", "sold" : "2014-02-12" }
{ "index" : { "_index" : "索引名", "_type" : "类名", "_id" : "1" } }
可以忽略为{ "index": {}}

 

6.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:这个桶中的文档数量

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

 

6.3 桶内度量

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

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

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

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字段,这是度量聚合的结果

6.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字段进行划分

//分组颜色后,分组make,再求make的平均值
GET /car/_search
{
"size": 0,
"aggs": {
"popular_colors": {
"terms": {
"field": "color"
},
"aggs": {
"maker": {
"terms": {
"field": "make"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}

 

6.5.划分桶的其它方式

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

  • Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组

  • Histogram Aggregation:根据数值阶梯分组,与日期类似

  • Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组

  • Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组

posted on 2020-02-27 16:35  roc-rui  阅读(1968)  评论(0编辑  收藏  举报