Kibana-Elasticsearch分析工具

原文地址:https://www.cnblogs.com/imdeveloper/p/12823384.html

Kibana 是在 Elasticsearch 有了相当多的数据之后,进行分析这些数据用的工具。Kibana 里面有一个叫做 Dev Tools 的工具,可以很方便地以 Restful 风格向 Elasticsearch 服务器提交请求。类似于使用Navicat工具连接MySQL这种关系型数据库,对数据库做操作。

启动

  1. 选择合适版本安装:https://www.elastic.co/cn/downloads/past-releases#kibana
  2. 在kibana的解压文件中启动kibana.bat
  3. 打开管理站点:http://localhost:5601/app/kibana#/dev_tools/console?_g=()
  4. 运行测试(查看服务器状态健康度,green表示一切OK):GET /_cat/health?v

Elasticsearch和kibana版本对应关系

Kibana版本 ES版本
4.1 1.4.4 +
4.2 2.0 +
4.3 2.1 +
4.4 2.2 +
4.5 2.3 +
4.6 2.4 +
5 5 +
... ...

使用简介

  1. ?pretty格式化返回json数据

索引管理

-- 增加索引
PUT /letbingo?pretty
-- 查询所有索引
GET /_cat/indices?v
-- 删除索引
DELETE /letbingo?pretty

中文分词器

GET _analyze
{
  "analyzer":"ik_max_word",
  "text":"趵突泉遭遇停喷危机"
}

文档管理

-- 增加文档
-- person在elasticsearch里是type的概念,相当于数据库里的表,这里就相当于向person表里插入了一条数据
PUT /letbingo/person/1?pretty
{
  "name": "lego"
}
-- 获取文档
GET /letbingo/person/1?pretty
-- 修改文档
-- 修改后版本号会发生变化
POST /letbingo/person/1/_update?pretty
{
  "doc": { "name": "letbingo" }
}
-- 删除文档
DELETE /letbingo/person/1?pretty

批量导入

简单的多条记录同时导入

这种方式能够插入的上限较小

POST _bulk
{"index":{"_index":"letbingo","_type":"person","_id":1}}
{"sex":"male","age":18,"name":"lego","place":"武汉","descrption":"javer"}
{"index":{"_index":"letbingo","_type":"person","_id":2}}
{"sex":"female","age":20,"name":"lisa","place":"上海","descrption":"ui"}

使用curl工具批量导入

curl是一个工具,可以模拟浏览器向服务器提交数据。
资源链接:https://pan.baidu.com/s/1HCN4nM1dSVvYrCeQnM15Cg 提取码:526r

  1. 按照第一种方法中的格式把记录写入json文件中
  2. 把curl.exe和json文件放在同一个目录下
  3. 在cmd中,运行如下命令:
cd C:\Downloads\curl
curl -H "Content-Type: application/json" -XPOST "localhost:9200/letbingo/person/_bulk?refresh" --data-binary "@persons.json"

查询操作

  1. 查询所有
GET /letbingo/_search
{
    "query": { "match_all": {} }
}
  1. 根据id倒序排列
GET /letbingo/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "_id": "desc" }
  ]
}
  1. 只返回指定字段
GET /letbingo/_search
{
  "query": { "match_all": {} },
  "_source": ["name","age"]
}
  1. 条件查询
GET /letbingo/_search
{
  "query": { "match": { "name": "lisa" } }
}
  1. 根据时间段查询
GET /letbingo/position/_search
{
   "query": { 
      "range" : {
         "time" : {
             "gte": "15810971331",
             "lte": "15830971331" 
         }
     }
   }
}
  1. 分页查询
GET /letbingo/_search
{
  "query": { "match_all": {} },
  "from": 1,
  "size": 3,
  "sort": { "_id": { "order": "desc" } }
}
  1. 聚合查询(类似传统数据库中的聚合查询)
    统计数据,第一个size:0表示不用显示每条数据,第二个size:3表示分组数据显示3条。
GET /letbingo/_search
{
  "size": 0,
  "aggs": {
    "group_by_place": {
      "terms": {
        "field": "place.keyword",
        "size": 3
      }
    }
  }
}

相当于Sql语句:select count(*),place from product group by place limit 0,3

可能遇到的问题

  1. Fielddata is disabled on text fields by default. Set fielddata=true on [id] in order to load fielddata in memory by uninverting the inverted index
    原因:因为加载Fielddata是一个昂贵的过程,可能会导致用户遇到延迟命中。所以默认禁用了Fielddata。解决方法是在聚合前发送指令开启Fielddata:
PUT /my_index/_mapping
{
  "properties": {
    "my_field": { 
      "type":     "text",
      "fielddata": true
    }
  }
}
  1. Elasticsearch health status显示为yellow
    解决方法:设置所有副本(rep)个数为0
curl -XPUT "http://localhost:9200/_settings" -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : 0
    }
}
posted @ 2020-05-03 20:56  letbingo  阅读(1225)  评论(0编辑  收藏  举报