PUT /book
{
"mappings":{
"properties":{
"name":{"type": "keyword"},
"info":{"type": "text"},
"price":{"type": "integer"}
}
}
}
索引不支持修改现有的,只支持新增
DELETE /book
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_task_manager_1 J_Qp6cL1QGSpNNGSFadkiQ 1 0 2 0 6.6kb 6.6kb
yellow open book Itynp5i7QhmFyMQrKnf9hg 1 1 0 0 230b 230b
green open .apm-agent-configuration ce48C5agSqCWP7M3ZP2bEg 1 0 0 0 283b 283b
green open .kibana_1 XOg9ADncRKOByv5GAdN2xw 1 0 3 0 15.5kb 15.5kb
post:带id不存在则新增,存在则覆盖(全量更新),不带id永远新增,且自动生成id
put:必须带id,否则报错,如果存在则覆盖,如果不存在则新增
post带着id不存在则新增
POST /book/_doc/2
{
"name":"mysql",
"info":"mysql是最好用的数据库",
"price":66
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 4,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 14,
"_primary_term" : 3
}
GET /book/_doc/2
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 4,
"_seq_no" : 14,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "mysql",
"info" : "mysql是最好用的数据库",
"price" : 66
}
}
post带着id存在,则覆盖
POST /book/_doc/2
{
"name":"mysql",
"info":"mysql是最好用的数据库,而且还免费",
"price":66
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 15,
"_primary_term" : 3
}
GET /book/_doc/2
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 5,
"_seq_no" : 15,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "mysql",
"info" : "mysql是最好用的数据库,而且还免费"
}
}
post不带id则新增,且自动生成id
POST /book/_doc
{
"name":"php",
"info":"php是最好用的语言",
"price":66
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "Zp2064sB7DFkWXEUKAII",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 3
}
put请求不带id报错,带着id没有就新增,有就覆盖
put不带id报错
PUT /book/_doc
{
"name":"oracle",
"info":"oracle是收费的数据库",
"price":66
}
{
"error" : "Incorrect HTTP method for uri [/book/_doc?pretty=true] and method [PUT], allowed: [POST]",
"status" : 405
}
put带着id,不存在则新增
PUT /book/_doc/3
{
"name":"oracle",
"info":"oracle是收费的数据库",
"price":66
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "3",
"_version" : 6,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 11,
"_primary_term" : 3
}
GET /book/_doc/3
{
"_index" : "book",
"_type" : "_doc",
"_id" : "3",
"_version" : 6,
"_seq_no" : 11,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "oracle",
"info" : "oracle是收费的数据库",
"price" : 66
}
}
put带着id存在则覆盖
PUT /book/_doc/3
{
"name":"oracle",
"info":"oracle是收费的数据库,所以小企业一般不用"
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "3",
"_version" : 7,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 12,
"_primary_term" : 3
}
GET /book/_doc/3
{
"_index" : "book",
"_type" : "_doc",
"_id" : "3",
"_version" : 7,
"_seq_no" : 12,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "oracle",
"info" : "oracle是收费的数据库,所以小企业一般不用"
}
}
理解
POST /book/_doc/5可能是新增操作,也可能会是覆盖操作,如果加上_create就明确告诉es,我要新增,如果存在就会报错
POST /book/_doc/5/_create
{
"name":"kafka",
"price":66
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 18,
"_primary_term" : 3
}
再次新增时报错
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[5]: version conflict, document already exists (current version [1])",
"index_uuid" : "Itynp5i7QhmFyMQrKnf9hg",
"shard" : "0",
"index" : "book"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[5]: version conflict, document already exists (current version [1])",
"index_uuid" : "Itynp5i7QhmFyMQrKnf9hg",
"shard" : "0",
"index" : "book"
},
"status" : 409
}
DELETE /book/_doc/1
语法:POST /{index}/type /{id}/_update
或者POST /{index}/_update/{id}
POST /book/_update/4
{
"doc": {
"info":"redis一个内存数据库,目前很流行"
}
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "4",
"_version" : 2,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 17,
"_primary_term" : 3
}
GET /book/_doc/4
{
"_index" : "book",
"_type" : "_doc",
"_id" : "4",
"_version" : 2,
"_seq_no" : 17,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "redis",
"info" : "redis一个内存数据库,目前很流行",
"price" : 100
}
}
更新只支持post请求
PUT /book/_update/4
{
"doc": {
"price": 200
}
}
{
"error" : "Incorrect HTTP method for uri [/book/_update/4?pretty=true] and method [PUT], allowed: [POST]",
"status" : 405
}
PUT /person/_doc/2?if_seq_no=4&if_primary_term=1
{
"name":"ls2"
}
如果_seq_no不等于4或者_primary_term不等于1就会报错
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[2]: version conflict, required seqNo [4], primary term [1]. current document has seqNo [5] and primary term [1]",
"index_uuid" : "7CucaVfVTi-V5OvzmuXHgA",
"shard" : "0",
"index" : "person"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[2]: version conflict, required seqNo [4], primary term [1]. current document has seqNo [5] and primary term [1]",
"index_uuid" : "7CucaVfVTi-V5OvzmuXHgA",
"shard" : "0",
"index" : "person"
},
"status" : 409
}
根据id查找
GET /book/_doc/2
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"_seq_no" : 5,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "mysql",
"info" : "mysql是最好用的数据库,而且还免费",
"price" : 66
}
}
定制返回字段
GET /book/_doc/4?_source_includes=name,info
{
"_index" : "book",
"_type" : "_doc",
"_id" : "4",
"_version" : 2,
"_seq_no" : 17,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "redis",
"info" : "redis一个内存数据库,目前很流行"
}
}
GET /book/_doc/4?_source_excludes=price
{
"_index" : "book",
"_type" : "_doc",
"_id" : "4",
"_version" : 2,
"_seq_no" : 17,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "redis",
"info" : "redis一个内存数据库,目前很流行"
}
}
批量查询
GET /_mget
{
"docs":[
{"_id":1,"_index":"person"},
{"_id":2,"_index":"book"}
]
}
{
"docs" : [
{
"_index" : "person",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张三",
"age" : 18
}
},
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 5,
"_seq_no" : 15,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "mysql",
"info" : "mysql是最好用的数据库,而且还免费"
}
}
]
}
同一索引下批量查询
类似与数据库的in效果
GET /book/_mget
{
"docs":[
{"_id":2},
{"_id":4}
]
}
{
"docs" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_version" : 5,
"_seq_no" : 15,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "mysql",
"info" : "mysql是最好用的数据库,而且还免费"
}
},
{
"_index" : "book",
"_type" : "_doc",
"_id" : "4",
"_version" : 2,
"_seq_no" : 17,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "redis",
"info" : "redis一个内存数据库,目前很流行",
"price" : 100
}
}
]
}
搜索写法
GET /book/_search
{
"query":{
"ids":{
"values":[2,4]
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "mysql",
"info" : "mysql是最好用的数据库,而且还免费"
}
},
{
"_index" : "book",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "redis",
"info" : "redis一个内存数据库,目前很流行",
"price" : 100
}
}
]
}
}
批量操作 bulk
{ "action": { ...metadata... }}
{ "operation": { ...document... }}
{ "action": { ...metadata... }}
{ "operation": { ...document... }}
...
action
: 描述操作的元数据,包括index
(索引文档)、delete
(删除文档)、update
(更新文档)等。operation
: 包含具体的文档数据。举例
POST /_bulk
{ "delete": { "_index": "test_index", "_id": "5" }}
{ "create": { "_index": "test_index", "_id": "14" }}
{ "test_field": "test14" }
{ "update": { "_index": "test_index", "_id": "2"} }
{ "doc" : {"test_field" : "bulk test"} }
{ "delete": { "_index": "test_index", "_id": "5" }}
删除了test_index中id=5的文档
{ "create": { "_index": "test_index", "_id": "14" }}
{ "test_field": "test14" }
往test_index里新增了一条数据
{ "update": { "_index": "test_index", "_id": "2"} }
{ "doc" : {"test_field" : "bulk test"} }
更新test_index中id=2的数据
create:相当于强制创建 PUT /index/type/id/_create
update:执行的是局部更新partial update操作
每个操作互不影响。操作失败的行会返回其失败信息