使用Kibana实现基本的增删改查操作

栏目: 数据库 · 发布时间: 6年前

内容简介:es中的索引对应mysql的数据库、类型对应mysql的表、文档对应mysql的记录、映射对应mysql的索引索引:index类型:type

es中的索引对应 mysql 的数据库、类型对应mysql的表、文档对应mysql的记录、映射对应mysql的索引

索引:index

类型:type

映射:mappings

1、创建索引

在kibana的Dev Tools中输入如下

PUT /lib/
{
    "settings":{
        "index":{
            "number_of_shards":3,
            "number_of_replicas":0
        }
    }
}

索引的名称为lib

number_of_shards:分片的数量为3,分片的数量一旦确定了就不能修改

number_of_replicas: 备份的数量为1,由于就1台服务器因此备份数量为0

创建成果后在右边显示如下

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

直接创建一个索引,会使用默认配置

PUT lib2

查看已经创建好的索引的配置

GET /lib/_settings

GET /lib2/_settings

查看所有索引的配置

GET _all/_settings

2、在索引下添加文档

# user 是类型

# 1 是文档的id

PUT /lib/user/1
{
    "first_name": "Jane",
    "last_name": "Smith",
    "age": 32,
    "about": "I like to collect rock albums",
    "interests": ["music"]
}

右边显示结果如下

{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

如果添加文档时没有指定id,那么这个id会由es自动生成,那么我们要使用POST方式添加文档

POST /lib/user/
{
    "first_name": "Douglas",
    "last_name": "Fir",
    "age": 23,
    "about": "I like to build cabinets",
    "interests": ["forestry"]
}

结果如下

{
  "_index": "lib",
  "_type": "user",
  "_id": "3z0vM2kBpR5Gle8qjwsu",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

3、查询文档

# 根据文档id进行查询

GET /lib/user/1

结果如下

{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "first_name": "Jane",
    "last_name": "Smith",
    "age": 32,
    "about": "I like to collect rock albums",
    "interests": [
      "music"
    ]
  }
}

GET /lib/user/3z0vM2kBpR5Gle8qjwsu

# 查看文档的部分信息

GET /lib/user/1?_source=age,about

结果如下

{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "about": "I like to collect rock albums",
    "age": 32
  }
}

4、修改文档

# 使用一个新的文档覆盖之前的文档,会覆盖文档所有字段的值

PUT /lib/user/1
{
    "first_name": "Jane",
    "last_name": "Smith",
    "age": 36,
    "about": "I like to collect rock albums",
    "interests": ["music"]
}

结果如下,会提示结果为updated

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

# 修改文档中的指定字段的值

POST /lib/user/1/_update
{
    "doc":{
        "age":33
    }
}

结果如下

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

5、删除文档

DELETE /lib/user/1

结果如下

{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

6、删除一个索引

DELETE lib2

结果如下

{

“acknowledged”: true

}


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

网络心理学

网络心理学

玛丽•艾肯 (Mary Aiken) / 中信出版社 / 2018-8-1 / CNY 58.00

《五十度灰》如何利用恋物心理,成为全球仅次于《圣经》的畅销读物? 为什么相对于亲朋好友,你更愿意向网络陌生人敞开心扉? 上网时总感觉时间飞逝,原来是网络的时间扭曲效应? 网络游戏中埋伏了哪些“上瘾”机关,暗中操控着你的行为? 为什么科技越发达,我们就越怕死? ...... 网络空间是一个巨大的兔子洞,里面集合了新奇、刺激、喜悦、痛苦、不安等各种元素。在日复一日的......一起来看看 《网络心理学》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具