ElasticSearch利用Kibana 进行CURD
添加索引
PUT /lib
{
"settings":{
"index":{
"number_of_shards":5,
"number_of_replicas":1
}
}
}
number_of_shards和number_of_replicas是不能修改的
返回信息
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "lib"
}
使用默认添加
PUT /lib2
使用GET获取lib的配置
GET /lib/_settings
返回信息
{
"lib": {
"settings": {
"index": {
"creation_date": "1561539156445",
"number_of_shards": "3",
"number_of_replicas": "0",
"uuid": "UPA0ox8rRiKTO-W41wncpw",
"version": {
"created": "6040299"
},
"provided_name": "lib"
}
}
}
}
获取lib2的配置
GET /lib2/_settings
返回信息
{
"lib2": {
"settings": {
"index": {
"creation_date": "1561539390153",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "zUMBW2vUS12yMVHsAAkvvg",
"version": {
"created": "6040299"
},
"provided_name": "lib2"
}
}
}
}
默认配置为分片为 5 个,备份为 1 个
获取所有索引的配置
GET _all/_settings
返回信息
{
"lib2": {
"settings": {
"index": {
"creation_date": "1561539390153",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "zUMBW2vUS12yMVHsAAkvvg",
"version": {
"created": "6040299"
},
"provided_name": "lib2"
}
}
},
"lib": {
"settings": {
"index": {
"creation_date": "1561539156445",
"number_of_shards": "3",
"number_of_replicas": "0",
"uuid": "UPA0ox8rRiKTO-W41wncpw",
"version": {
"created": "6040299"
},
"provided_name": "lib"
}
}
}
}
添加数据
PUT /lib/user/1
{
"first_name":"gao",
"last_name":"tiedan",
"age":22,
"about":"I like to collect rock albums",
"interests":["music"]
}
PUT 添加数据,lib 类似于数据库, user 类似于 数据库中的表, 1 类似于每一列的ID
返回信息
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
** index : 添加到的index **
** _type: 添加到的type**
** _id: 唯一id**
** _version: 版本号 **
** result: 添加结果**
如果不指定ID,想通过elasticsearch 来生成ID可以使用POSt方式
POST lib/user
{
"first_name":"liu",
"last_name":"ergou",
"age":32,
"about":"I like to collect make love",
"interests":["love"]
}
返回信息
{
"_index": "lib",
"_type": "user",
"_id": "n-QSk2sBI3jKMDQna__Q",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
** _id : 自动生成的id**
通过GET来查询文档
GET lib/user/1
返回信息
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"first_name": "gao",
"last_name": "songsong",
"age": 22,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
}
查询指定字段
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": 22
}
}
修改文档信息
1. 第一种办法,通过一个新的文档,来吧旧的文档来覆盖掉
PUT /lib/user/1
{
"first_name":"gao",
"last_name":"tiedan",
"age":55,
"about":"I like to collect rock albums",
"interests":["music"]
}
返回信息
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
你会发现版本号变成了2,result 变为了 updated
再次查询 id 为 1 的文档
GET /lib/user/1
返回信息
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"first_name": "gao",
"last_name": "tiedan",
"age": 55,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
}
** 年龄变成了55岁,版本号变为了 2**
2. 通过post方式来更新文档
POST lib/user/1/_update
{
"doc": {
"age":33
}
}
** POST index/type/id/_update **
** doc{“age”:33} 指定字段 为 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
}
删除一个文档
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
}
删除某个索引
DELETE lib2
返回信息
{
"acknowledged": true
}
查询所有的索引
GET _all
返回信息
{
"lib": {
"aliases": {},
"mappings": {
"user": {
"properties": {
"about": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"first_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"interests": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"last_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1561539156445",
"number_of_shards": "3",
"number_of_replicas": "0",
"uuid": "UPA0ox8rRiKTO-W41wncpw",
"version": {
"created": "6040299"
},
"provided_name": "lib"
}
}
}
}
CURD的所有语句
PUT /lib/
{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":0
}
}
}
PUT lib2
GET /lib/_settings
GET /lib2/_settings
GET _all/_settings
PUT /lib/user/1
{
"first_name":"gao",
"last_name":"songsong",
"age":22,
"about":"I like to collect rock albums",
"interests":["music"]
}
POST lib/user
{
"first_name":"liu",
"last_name":"yifei",
"age":32,
"about":"I like to collect make love",
"interests":["love"]
}
GET lib/user/1
GET lib/user/1?_source=age,about
PUT /lib/user/1
{
"first_name":"gao",
"last_name":"tiedan",
"age":55,
"about":"I like to collect rock albums",
"interests":["music"]
}
GET /lib/user/1
POST lib/user/1/_update
{
"doc": {
"age":33
}
}
DELETE lib/user/1
DELETE lib2
GET _all