使用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

}


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

查看所有标签

猜你喜欢:

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

Understanding Machine Learning

Understanding Machine Learning

Shai Shalev-Shwartz、Shai Ben-David / Cambridge University Press / 2014 / USD 48.51

Machine learning is one of the fastest growing areas of computer science, with far-reaching applications. The aim of this textbook is to introduce machine learning, and the algorithmic paradigms it of......一起来看看 《Understanding Machine Learning》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具