Man*_*ngh 5 groovy elasticsearch
请考虑以下文档
{
"title": "My first blog entry",
"text": "Starting to get the hang of this...",
"tags": [ "testing" ],
"views": 0
}
Run Code Online (Sandbox Code Playgroud)
我需要运行一种upsert操作.如果我遇到像这样的数据
{
"id": 1,
"tags": [ "new tag" ]
}
Run Code Online (Sandbox Code Playgroud)
我想更新具有相同ID的现有文档.所以结果应该是:
{
"id": 1,
"title": "My first blog entry",
"text": "Starting to get the hang of this...",
"tags": [ "testing", "new tag" ],
"views": 0
}
Run Code Online (Sandbox Code Playgroud)
如果不存在具有相同id的文档,我想创建一个新文档.
现在在像mongoDB这样的数据库中,我可以使用$ addToSet或$ push操作进行更新.我在Elasticsearch中找不到类似的操作.
我读到它可以通过在groovy中编写脚本来完成.但是,这需要在包含2亿条记录的文件上完成.我不确定我是否可以将groovy与批量API结合使用.可能吗 ?
您不需要为此使用批量API.您可以使用upsert请求.Upsert请求也可以嵌入批量请求中.
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script": "if (ctx._source.tags.contains(\"tags\")) {ctx._source.tags += tag;} else {ctx._source.tags = [tag]}",
"params": {
"tag": "newTag"
},
"upsert": {
"title": "My first blog entry",
"text": "Starting to get the hang of this...",
"tags": [
"newTag"
],
"views": 0
}
}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1904 次 |
| 最近记录: |