Elasticsearch插入文档
插入之前需要了解一下标识符的概念
文档标识符
每一个ES文档都有一个索引内唯一的标识符(identifier),类似关系数据库中的主键
标识符由用户提供,使用HTTP PUT操作添加,例如将关系数据库中数据导入ES
标识符有ES自动生成,使用HTTP POST操作添加,UUID,例如将网站日志导入ES
PUT插入
创建索引ffbf
PUT /ffbf
{
"settings": {
"index":{
"number_of_shards":2,
"number_of_replicas":2
}
}
}
在ffbf索引带ID插入文档,_doc后面就是自定义的ID
PUT /ffbf/_doc/123
{
"name":"Monster",
"age":26,
"address":"天安云谷xx栋xx层",
"url":"ffbf.top"
}
返回结果,可以看见_id为123
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询一下ffbf索引
GET /ffbf/_search
返回结果
{
"took" : 808,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_score" : 1.0,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
}
]
}
}
小技巧:Elastic开发工具有自动补全提示命令非常好用,如果你想要在服务器使用curl命令一系列操作,可以直接点击小扳手选择复制为cURL
然后去服务器操作
[root@postgresql ~]# curl -XGET "http://192.168.170.111:9200/ffbf/_search"
{"took":2,"timed_out":false,"_shards":{"total":2,"successful":2,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"ffbf","_type":"_doc","_id":"123","_score":1.0,"_source":{
"name":"Monster",
"age":26,
"address":"天安云谷xx栋xx层",
"url":"ffbf.top"
}
}]}}[root@postgresql ~]#
PUT重复插入
我们重复执行一下刚才的插入操作会怎么样?
PUT /ffbf/_doc/123
{
"name":"Monster",
"age":26,
"address":"天安云谷xx栋xx层",
"url":"ffbf.top"
}
返回结果如下,result由created变成了updated状态,版本递增变成2了,做了一个更新的操作,由于没有更改内容,所以查询索引返回的结果还是一样的
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
POST插入
使用post插入不用指定id,会自动生成一个id
POST /ffbf/_doc
{
"name":"Helen",
"age":20,
"address":"天安云谷xx栋xx层",
"url":"qqbq.top"
}
返回结果,自己生成了一个唯一的标识符id
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "SQMeaYkB24MJMrhTgLUX",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
POST重复插入
同样,再执行一遍
POST /ffbf/_doc
{
"name":"Helen",
"age":20,
"address":"天安云谷xx栋xx层",
"url":"qqbq.top"
}
返回结果如下,_id变了,result还是created创建状态。因为post每次都会生成一个唯一的id所以每次都会创建一个文档,不会覆盖去做一个更新的操作。
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "SgMgaYkB24MJMrhTTrUM",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
查询索引
GET /ffbf/_search
返回结果如下,可以看见我们总共执行了2次put、2次post插入,但是只有3个文档,put第2次做了个更新操作,而post不带id每次都会创建个文档,而我们也没有改动数据重复插入所以可以看见最后两个文档除了id不同其它数据都一样。
{
"took" : 746,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_score" : 1.0,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "SQMeaYkB24MJMrhTgLUX",
"_score" : 1.0,
"_source" : {
"name" : "Helen",
"age" : 20,
"address" : "天安云谷xx栋xx层",
"url" : "qqbq.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "SgMgaYkB24MJMrhTTrUM",
"_score" : 1.0,
"_source" : {
"name" : "Helen",
"age" : 20,
"address" : "天安云谷xx栋xx层",
"url" : "qqbq.top"
}
}
]
}
}
_create避免PUT覆盖文档(更新操作)
Elasticsearch提供了_create可以避免文档被覆盖,如下将PUT的_doc跟换成_create,同样带id为123执行
PUT /ffbf/_create/123
{
"name":"Monster",
"age":26,
"address":"天安云谷xx栋xx层",
"url":"ffbf.top"
}
返回结果,报错提示版本冲突引擎异常,文档已存在
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[123]: version conflict, document already exists (current version [2])",
"index_uuid" : "7zMVV6IrQt6KrZBdIi26bw",
"shard" : "0",
"index" : "ffbf"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[123]: version conflict, document already exists (current version [2])",
"index_uuid" : "7zMVV6IrQt6KrZBdIi26bw",
"shard" : "0",
"index" : "ffbf"
},
"status" : 409
}
更换一个文档不存在的id执行,例如id为456
PUT /ffbf/_create/456
{
"name":"Monster",
"age":26,
"address":"天安云谷xx栋xx层",
"url":"ffbf.top"
}
返回结果,创建成功
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "456",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
自动创建索引
我们在最开始创建了个ffbf索引,这是我们手动创建的,而ES本身是默认开启了自动创建索引的。
action.auto_create_index #默认为true,开启的
例如我们再来创建个文档,想要在一个叫qqbq的索引下创建
我们不去做创建一个叫qqbq的索引动作,而是直接往qqbq索引创建文档。
PUT /qqbq/_doc/789
{
"name":"Helen",
"age":20,
"address":"天安云谷xx栋xx层",
"url":"qqbq.top"
}
返回结果,创建成功
{
"_index" : "qqbq",
"_type" : "_doc",
"_id" : "789",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询一下索引情况
GET /_cat/indices?v
返回结果,可以看见ffbf、qqbq两个索引都已存在
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .apm-custom-link Xmj6MPxBT_ipIOZyTbwCQw 1 1 0 0 416b 208b
green open .kibana_task_manager_1 RmlBFYfNRSSXXJzlmMf8aA 1 1 5 5002 1mb 550.4kb
green open .apm-agent-configuration QFeI8NB1R8OeOnSy51Z92w 1 1 0 0 416b 208b
green open .kibana-event-log-7.10.1-000001 p-r29FQyT6KWlM4C_hvIMw 1 1 2 0 22kb 11kb
green open qqbq lVGlJpM0RXOrA8WfhMauTA 1 1 1 0 10.6kb 5.3kb
green open ffbf 7zMVV6IrQt6KrZBdIi26bw 2 2 4 0 48.8kb 16.2kb
green open .kibana_1 9JoNLVfYTsaxpsYufqKI7g 1 1 33 10 4.2mb 2.1mb
关闭自动创建索引
如果你想要关闭自动创建索引,可以通过下面设置关闭
PUT /_cluster/settings
{
"persistent": {
"action.auto_create_index": "false"
}
}
评论区