第四节 Kibana的安装以及ElasticSearch数据的操作

亮子 2021-06-15 08:14:51 26394 1 0 0

1、官方文档

1)、kibana官网下载

https://www.elastic.co/cn/downloads/kibana

图片alt

2)、kibana对应版本

https://www.elastic.co/cn/downloads/past-releases#kibana

图片alt

选择相应的产品和对应的版本:

图片alt

3)、安装运行kibana

安装十分简单,直接解压即可,然后运行bin/kibana.bat批处理文件即可,如下图:

图片alt

运行效果如下:

图片alt

4)、配置访问

访问地址:

http://localhost:5601/app/kibana

图片alt

选择dev tools:

图片alt

进入操作页面:

图片alt

2、索引操作

1)、添加索引

PUT tb_hero
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
  • -d指定了你的参数,这里将这些参数放到了json文件中
  • settings设置内容含义
name 价格
number_of_shards 分片数
number_of_replicas 副本数
mappings 结构化数据设置 下面的一级属性 是自定义的类型
properties 类型的属性设置节点,下面都是属性
epoch_millis 表示时间戳

也可以使用默认配置

put tb_hero

执行结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "tb_hero"
}

图片alt

2)、查看索引

  • 查看索引列表
GET _cat/indices

执行结果:

图片alt

  • 查看某个索引信息

查看所有索引

GET tb_hero

执行结果:

图片alt

3)、添加文档

put /tb_hero/_doc/1
{
  "name":"武松",
  "age":"33",
  "gender":"男",
  "work":"打虎"
}

图片alt

  • _doc为该文档的类型
  • 1是该文档的id

也可不指定id,但需要使用post命令,id会自动生成

POST /tb_hero/_doc
{
  "name":"鲁智深",
  "age":"40",
  "gender":"男",
  "work":"拔树"
}

未指定id时的执行结果:

图片alt

4)、查看记录

  • 查看索引的所有文档
GET /tb_hero/_search

执行结果:

{
  "took" : 190,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "tb_hero",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "武松",
          "age" : "33",
          "gender" : "男",
          "work" : "打虎"
        }
      },
      {
        "_index" : "tb_hero",
        "_type" : "_doc",
        "_id" : "VeEjEHoBx-sP55Hp_muF",
        "_score" : 1.0,
        "_source" : {
          "name" : "鲁智深",
          "age" : "40",
          "gender" : "男",
          "work" : "拔树"
        }
      }
    ]
  }
}
  • 根据id查看:
GET /tb_hero/_doc/1

执行结果:

{
  "_index" : "tb_hero",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "武松",
    "age" : "33",
    "gender" : "男",
    "work" : "打虎"
  }
}

5)、更新文档

  • 直接覆盖
PUT /tb_hero/_doc/1
{
  "name":"武松",
  "age" : "44",
  "gender":"男",
  "work":"都头"
}
  • 只更新需要更新的字段
POST /tb_hero/_update/1
{
  "doc": {
    "age" : "32"
  }
}

5)、删除一个文档

DELETE /tb_hero/_doc/1

6)、条件删除文档

POST /tb_hero/_delete_by_query
{
  "query":{
    "match":{
      "name":"武松"
    }
  }
}

7)、删除索引

delete test

执行结果:
{
  "acknowledged": true
}

3、索引操作参考记录


# 查询索引数量 GET _cat/count # 查询索引的健康情况 GET _cat/health?v # 查询已经安装的插件 GET _cat/plugins # 查询有哪些索引 # show databases GET _cat/indices GET _cat/indices?v # 创建索引 # create database db_xxx PUT /kaoshi # 删除索引 # drop database db_xxx DELETE kaoshi DELETE /yunjisuan # 创建映射 # create table tb_xxx POST /kaoshi/_mappings { "properties": { "id": { "type": "long" }, "title": { "type": "text", "analyzer": "ik_smart" }, "content": { "type": "text", "analyzer": "ik_smart" } } } # show create table tb_xxx GET /kaoshi # insert into tb_xxx() values() put /kaoshi/_doc/2 { "id":"2", "title":"夜宿山寺", "content":"危楼高百尺" } # select * from tb_xxx GET /kaoshi/_search GET /kaoshi/_doc/2 GET /kaoshi/_search { "query": { "match": { "id": "1" } } }
GET _search
{
  "query": {
    "match_all": {}
  }
}

# 查询所有索引
GET _cat/indices

# 创建索引
PUT /shuihu

# 查看索引设置
GET /shuihu/_settings

# 创建映射
PUT /shuihu
{
  "mappings": {
    "properties":{
      "id":{"type":"long"},
      "name":{"type":"text","analyzer":"ik_max_word"},
      "sex":{"type":"text"},
      "age":{"type":"long"}
    }
  }
}

# 添加文档
put /shuihu/_doc/1
{
  "id":1,
  "name":"武松", 
  "age":32,
  "sex":"男",
  "begood":"打老虎"
}

put /shuihu/_doc/2
{
  "id":2,
  "name":"鲁智深", 
  "age":32,
  "sex":"男",
  "begood":"拔树"
}

# 查询文档
GET /shuihu/_doc/1

# 查询所有文档
GET /shuihu/_doc/_search
{
  "query": {
    "match_all": {}
  }
}

# 修改文档
put /shuihu/_doc/1
{
  "id":1,
  "name":"武松", 
  "age":39,
  "sex":"男",
  "begood":"打老虎"
}

# 删除id为1的文档
DELETE /shuihu/_doc/1

# 条件删除文档
delete /shuihu/_doc/_query
{
  "query": {
    "match": { "first_name": "zhou"}
  }
}

# 删除索引
delete /shuihu

# 删除所有索引
DELETE http://localhost:9200/_all