Create一个文档
支持自动生成文档ID和指定文档ID两种方式: 通过调用POST my_index/_doc/ 系统会自动生成ID
使用HTTP PUT my_index/_doc/1/_create 创建时, URI中显示指定_create, 此时如果该id 的文档已经存在,则操作失败。
DELETE users
############Create Document############
#create document. 自动生成 _id : KS5tOXcBQn2iJa_jWk65
POST users/_doc
{
"user" : "Mike",
"post_date" : "2019-04-15T14:12:12",
"message" : "trying out Kibana"
}
#create document. 指定Id。如果id已经存在,报错
PUT users/_doc/1?op_type=create
{
"user" : "Jack",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
#create document. 指定 ID 如果已经存在,就报错
# "reason": "[_doc][1]: version conflict, document already exists (current version [1])",
PUT users/_doc/1/_create
{
"user" : "kris",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
Get 一个文档
找到文档,返回HTTP 200
文档元信息
-
-
- _index/ _type/
- 版本信息,同一个id的文档,即使被删除,Version号也会不断增加
- _source中默认包含了文档的所有原始信息
-
找不到文档,返回HTTP 404
### Get Document by ID
#Get the document by ID
GET users/_doc/1
Index文档
Index和Create 不一样的地方:如果文档不存在,就索引新的文档。
否则现有文档会被删除,新的文档被索引。版本信息 +1
update文档
# Update方法不会删除原来的文档,而是实现真正的数据更新
# Post方法 /Payload需要包含在"doc"中
### Index & Update
#Update 指定 ID (先删除,在写入)
# 修改某个字段, updated, 会把之前的先删掉,再put,版本号+1
PUT users/_doc/1
{
"user" : "Mike"
}
#查询得到, 之前的user、post_date、message被删除了
GET users/_doc/1
#GET users/_doc/1
#在原文档上增加字段, 不会删除重新put
POST users/_doc/1/_update
{
"doc":{
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
GET users/_doc/1
Bulk API
常见错误返回
### Bulk 操作
#执行两次,查看每次的结果
#执行第1次
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
#执行第2次
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
### mget 操作
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_id" : "1"
},
{
"_index" : "test",
"_id" : "2"
}
]
}
#URI中指定index
GET /test/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}
### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}
### 清除测试数据
#清除数据
DELETE users
DELETE test
DELETE test2