欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ES-04-ElasticSearch增删改查

时间:2023-04-18
说明

ElasticSearch索引的基本增、删、改、查ElasticSearch文档的基本增、删、改、查官方文档:https://www.elastic.co/cn/ 常用操作 》创建索引

相当于MySQL中的创建数据库

请求格式:/<索引名称>

请求示例

请求方式:GET

发送请求:

curl -X GET http://192.168.3.201:9200/index001

响应结果:

{ "acknowledged": true, "shards_acknowledged": true, "index": "index001"}

PUT请求具有幂等性,首次请求成功,再次请求就是400状态码

》获取单个索引

请求格式:/<索引名称>

请求示例

请求方式:GET

发送请求:

curl -X GEThttp://192.168.3.201:9200/index001

响应结果:

{ "index001": { "aliases": {}, "mappings": {}, "settings": { "index": { "creation_date": "1642472671251", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "OHFCZiCwTC6bvlrPGUCZHQ", "version": { "created": "7090399" }, "provided_name": "index001" } } }}

》获取全部索引

与获取单个索引类似

请求格式:/_cat/indices?v

可选参数[v]:带上v会以表格的形式返回数据,内容更详细,显式更清晰

请求示例

请求方式:GET

发送请求:

curl -X GET http://192.168.3.201:9200/_cat/indices?v

响应结果:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.sizeyellow open index001 OHFCZiCwTC6bvlrPGUCZHQ 1 1 0 0 208b 208b

》删除单个索引

请求格式:/<索引名称>

请求示例

请求方式:DELETE

发送请求:

curl -X DELETE http://192.168.3.201:9200/index001

响应结果:

{ "acknowledged": true}

》创建文档

请求格式:/<索引名称>/_doc/<自定义ID>

自定义ID:不填则随机生成另一种格式:你也可以把_doc改为_create

请求体body

是否必填:是格式:JSON请求体中参数名随意

注意:重复创建文档的操作会覆盖数据然后更新_version

请求示例

请求方式:POST

发送请求:

curl -X POST http://192.168.3.201:9200/index001/_doc/1001 -H 'Content-Type:application/json' -d '{"id":1001,"name":"张三","age":18}'

响应结果:

{ "_index": "index001", "_type": "_doc", "_id": "1001", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 5}

当成功创建文档后,请求体body中的自定义字段就会被加入到关联索引的mappings中的properties中,此时获取index索引结果如下:

{ "index001": { "aliases": {}, "mappings": { "properties": { "age": { "type": "long" }, "id": { "type": "long" }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "settings": { "index": { "creation_date": "1642474213474", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "MaZr80M8RgCl0PFZ_wjEXA", "version": { "created": "7090399" }, "provided_name": "index001" } } }}

》获取单个文档

请求格式:/<索引名称>/_doc/<自定义ID>

请求示例

请求方式:GET

发送请求:

curl -X GET http://192.168.3.201:9200/index001/_doc/1001

响应结果:

{"_index": "index001","_type": "_doc","_id": "1001","_version": 2,"_seq_no": 6,"_primary_term": 1,"found": true,"_source": {"id": 1001,"name": "张三","age": 18}}

》获取索引下全部文档

请求格式:/<索引名称>/_search

注意:此请求务必不要传请求体body

请求示例

请求方式:GET

发送请求:

curl -X GET http://192.168.3.201:9200/index001/_search

响应结果:

{"took": 756,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "index001","_type": "_doc","_id": "tJcja34BW7FmJcLFEhTd","_score": 1.0,"_source": {"id": 1001,"name": "张三","age": 18}},{"_index": "index001","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"id": 1001,"name": "张三","age": 18}}]}}

》全量更新某文档

请求格式:/<索引名称>/_doc/<文档ID>

请求体body

是否必填:是格式:JSON

请求示例

请求方式:PUT

发送请求:

curl -X PUT http://192.168.3.201:9200/index001/_doc/1001 -H 'Content-Type:application/json' -d '{"id":1001,"name":"张三","sex":1}'

响应结果:

{ "_index": "index001", "_type": "_doc", "_id": "1001", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 7, "_primary_term": 1}

问题:如果全量更新文档时请求体body中的字段名跟原有的字段名不一样怎么处理?

索引中会增量记录所有使用过的字段(查看索引中记录的字段,其中包含原有的字段age以及新增的字段sex)

{ "properties": { "age": { "type": "long" }, "id": { "type": "long" }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "sex": { "type": "long" } }}

文档中保留最后一次全量更新的所有字段(同一个索引中不同的文档可以具有不同的字段属性),如下示例一个是age属性一个是sex属性

{"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "index001","_type": "_doc","_id": "tJcja34BW7FmJcLFEhTd","_score": 1.0,"_source": {"id": 1001,"name": "张三","age": 18}},{"_index": "index001","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"id": 1001,"name": "张三","sex": 1}}]}}

》局部更新某文档

请求格式:/<索引名称>/_update/<文档ID>

请求体body

是否必填:是格式:JSON,字段用“doc”键包裹注意:字段是目标文档中已存在的字段时更新原有字段,否则插入新字段。所有字段都会记录在索引的mappings中即使不再使用。

请求示例

请求方式:POST

发送请求:

curl -X POST http://192.168.3.201:9200/index001/_update/1001 -H 'Content-Type:application/json', -d '{"doc":{"sex":0}}'

响应结果:

{ "_index": "index001", "_type": "_doc", "_id": "1001", "_version": 4, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 8, "_primary_term": 1}

》删除某文档

请求格式:/<索引名称>/_doc/<文档ID>

请求示例

请求方式:DELETE

发送请求:

curl -X DELETE http://192.168.3.201:9200/index001/_doc/1001

响应结果:

{ "_index": "index001", "_type": "_doc", "_id": "1001", "_version": 8, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 12, "_primary_term": 1}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。