ElasticSearch第6节 Kibana 的Dev Too
2019-06-14 本文已影响0人
小超_8b2f
image.png
http://<ip>:<port>/<索引>/<类型>/<文档ID>
http://<ip>:<port>/<索引>/_doc/<文档ID>
_doc
1. 建立索引并指定配置参数
####1. 建立索引并执行
put /lib/
{
"settings":{
"index":{
"number_of_shards":5, #指定当前索引的分片数
"number_of_replicas":0 #指定当前索引的备份数
}
}
}
#结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "lib"
}
2. 建立索引,配置默认
put lib3
#结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "lib3"
}
# 查看索引的配置
GET lib/_settings
GET lib2/_settings
GET _all/_settings
3. PUT创建一个文档 (指定id)
# lib 是索引,user是类型,1是id
PUT /lib/user/1
{
"first_name":"xiao",
"second_name":"chao",
"age":22,
"about":"I like to build",
"likes":["football","basketball"]
}
#获取全文档信息
GET /lib/user/1
#! Deprecation: [types removal] Specifying types in document
# get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{
"_index" : "lib",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"first_name" : "xiao",
"second_name" : "chao",
"age" : 22,
"about" : "I like to build",
"likes" : [
"football",
"basketball"
]
}
}
#获取文档指定字段
GET /lib/user/1?_source=age,about
4. POST生成文档(自动生成id)
#下面操作报错,新版本已经废除了type概念
! Deprecation: [types removal] Specifying types in document index requests is
deprecated, use the typeless endpoints
instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
POST /lib/user/
{
"first_name":"haha",
"second_name":"xixi",
"age":23,
"about":"I like to drink",
"likes":["hello","world"]
}
#获取文档
GET /lib/user/rrZYVGsBNohMOFFbhAxR
5.PUT 覆盖更新
PUT /lib/user/1
{
"first_name":"xiao",
"second_name":"chao",
"age":32,
"about":"I like to build",
"likes":["football","basketball"]
}
6. 专项属性更新
POST /lib/user/_update/1
{
"doc": {
"age":5,
"about":"I like to build",
"likes":["football"]
}
}
7. 删除
#删除文档
DELETE /lib/user/1
#删除索引
DELETE /lib
ES7.0.0 版本请求
不再有type,_doc是写死的
PUT /lib2/_doc/1 #增
DELETE /lib2/_doc/1 #删
POST lib2/_doc/1 #改
GET /lib2/_doc/1 #查
使用es提供的Multi Get API批量获取
通过es提供的Multi Get API可以通过:索引名、类型名、文档id一次得到一个文档集合。文档可以来自同一索引库,也可以来自不同的索引库。
方式一:使用curl命令:
$ curl -H "Content-Type: application/json" -XPOST http://192.168.249.104:9200/_mget -d '{
"docs":[
{
"_index":"lib1",
"_id":"2"
},
{
"_index":"lib2",
"_id":"1"
}
]
}'
方式二:kibana的Dev Tools GET方式
指定索引_index 和 _id
GET /_mget
{
"docs":[
{
"_index":"lib1",
"_id":"2"
},
{
"_index":"lib2",
"_id":"1"
},
{
"_index":"lib",
"_type":"user", #注意:新版本中不再有type了。下面结果中没有体现这个
"_id":"1"
}
]
}
{
"docs" : [
{
"_index" : "lib1",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "guchao",
"age" : 22
}
},
{
"_index" : "lib2",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 7,
"_primary_term" : 1,
"found" : true,
"_source" : {
"doc" : {
"age" : 5,
"about" : "I like to build",
"likes" : [
"football"
]
}
}
}
]
}
批量获取指定字段:_source
GET /_mget
{
"docs":[
{
"_index":"lib1",
"_id":"2",
"_source":["age"]
},
{
"_index":"lib2",
"_id":"1",
"_source":["doc"]
},
{
"_index":"lib2",
"_id":"2"
},
{
"_index":"lib",
"_type":"user",
"_id":"1",
"_source":["first_name"]
}
]
}
相同索引的简化查询参数,将索引赋值到路径上
GET lib2/_mget
{
"docs":[
{"_id":"1", "_source":["doc"] },
{"_id":"2"}
]
}
按文档id批量查询
GET lib2/_mget
{
"ids":["1","2"]
}