标签:php 配置 命令 search 一个 elastics version post pos
Bulk是elasticsearch提供的适用于批量操作的API,可以实现批量的添加、修改、删除,上一篇中记录的Multi Get只能实现批量的获取。
格式包含请求行为action和请求数据requestbody,这两个是一条命令,但是要换行,如下:
{action:{metatata}}\n
{requestbody}\n
action是操作行为包含以下几种:
create:文档不存在时创建
update:更新文档
index:创建新文档或替换已有文档
delete:删除一个文档
metatata:包含_index,_type,_id 即要执行的索引、类型、文档id
create 和 index的区别:如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。
具体示例如下:
1、批量添加
向索引列表lib2中的books里添加id是1,3,4的文档。
POST /lib2/books/_bulk {"index":{"_id":1}} {"title":"Html5","price":45} {"index":{"_id":3}} {"title":"Php","price":35} {"index":{"_id":4}} {"title":"Python","price":50}
用上一节记录的命令查看一下是否成功
1 GET /lib2/books/_mget 2 { 3 "ids":["1","3","4"] 4 }
2、批量修改
修改文档id是1、3、4的文档,把age分别修改为51、53、54
POST /lib2/books/_bulk {"update":{"_index":"lib2","_type":"books","_id":1}} {"doc":{"price":51}} {"update":{"_index":"lib2","_type":"books","_id":3}} {"doc":{"price":53}} {"update":{"_index":"lib2","_type":"books","_id":4}} {"doc":{"price":54}}
3、批量删除
POST /lib2/books/_bulk {"delete":{"_index":"lib2","_type":"books","_id":1}} {"delete":{"_index":"lib2","_type":"books","_id":3}} {"delete":{"_index":"lib2","_type":"books","_id":4}}
4、可以用POST混合执行操作
如下,添加文档id是1、3、4的文档,把1的age修改为53,4的age修改为54,然后删除掉id是3、4的文档,此时只剩下id为1的文档,age是53
POST /lib2/books/_bulk {"index":{"_id":1}} {"title":"Html5","price":45} {"index":{"_id":3}} {"title":"Php","price":35} {"index":{"_id":4}} {"title":"Python","price":50} {"update":{"_index":"lib2","_type":"books","_id":1}} {"doc":{"price":53}} {"update":{"_index":"lib2","_type":"books","_id":4}} {"doc":{"price":54}} {"delete":{"_index":"lib2","_type":"books","_id":3}} {"delete":{"_index":"lib2","_type":"books","_id":4}}
查询一下执行结果,与预期一致
{ "docs": [ { "_index": "lib2", "_type": "books", "_id": "1", "_version": 2, "found": true, "_source": { "title": "Html5", "price": 53 } }, { "_index": "lib2", "_type": "books", "_id": "3", "found": false }, { "_index": "lib2", "_type": "books", "_id": "4", "found": false } ] }
bulk会把将要处理的数据载入内存中,所以数据量是有限制的,最佳的数据量不是一个确定的数值,它取决于硬件,文档大小,复杂性,索引、以及搜索的负载。一般建议是1000-5000个文档,大小建议是5-15M,默认不能超过100M,可以在elasticsearch的配置文件elasticsearch.yml中配置。
(09)ElasticSearch 使用Bulk实现批量操作
标签:php 配置 命令 search 一个 elastics version post pos
原文地址:https://www.cnblogs.com/javasl/p/11405362.html