gpt4 book ai didi

curl - 为什么 Elasticsearch _update with "script"using "params"不删除文档?

转载 作者:行者123 更新时间:2023-11-29 02:56:45 25 4
gpt4 key购买 nike

我使用 Elasticsearch 6.1.3 进行了这些实验。

实验 1:脚本 为单行

这是我的 shell 脚本:

# Index document.
curl -sX PUT -H 'Content-Type: application/json' \
http://localhost:9200/a/a/1?pretty -d '{ "n": 10 }'

# Delete document (single-line script)
curl -siX POST -H 'Content-Type: application/json' \
http://localhost:9200/a/a/1/_update?pretty -d '{

"script": "ctx.op = ctx._source.n == 10 ? \"delete\" : \"none\""
}'

# Print document.
sleep 1
curl -si http://localhost:9200/a/a/1?pretty

这是最后两个 curl 命令的输出:

+ curl -siX POST -H 'Content-Type: application/json' 'http://localhost:9200/a/a/1/_update?pretty' -d '{

"script": "ctx.op = ctx._source.n == 10 ? \"delete\" : \"none\""
}'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 213

{
"_index" : "a",
"_type" : "a",
"_id" : "1",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 20,
"_primary_term" : 1
}
+ sleep 1
+ curl -si 'http://localhost:9200/a/a/1?pretty'
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 72

{
"_index" : "a",
"_type" : "a",
"_id" : "1",
"found" : false
}

如预期的那样,文档已被删除。

实验 2:脚本作为对象

这是我的 shell 脚本:

# Index document.
curl -sX PUT -H 'Content-Type: application/json' \
http://localhost:9200/a/a/1?pretty -d '{ "n": 10 }'

# Print document.
sleep 1
curl http://localhost:9200/a/a/1?pretty

# Delete document (single-line script)
curl -siX POST -H 'Content-Type: application/json' \
http://localhost:9200/a/a/1/_update?pretty -d '{

"script": {
"inline": "ctx.op = ctx._source.n == params.count ? \"delete\" : \"none\"",
"params": {
"count": 10
}
}
}'

# Print document.
sleep 1
curl -si http://localhost:9200/a/a/1?pretty

这是最后两个 curl 命令的输出:

+ curl -siX POST -H 'Content-Type: application/json' 'http://localhost:9200/a/a/1/_update?pretty' -d '{

"script": {
"inline": "ctx.op = ctx._source.n == params.count ? \"delete\" : \"none\"",
"params": {
"count": 10
}
}
}'
HTTP/1.1 200 OK
Warning: 299 Elasticsearch-6.1.3-af51318 "Deprecated field [inline] used, expected [source] instead" "Sat, 10 Feb 2018 10:58:57 GMT"
content-type: application/json; charset=UTF-8
content-length: 213

{
"_index" : "a",
"_type" : "a",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 22,
"_primary_term" : 1
}
+ sleep 1
+ curl -si 'http://localhost:9200/a/a/1?pretty'
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 72

{
"_index" : "a",
"_type" : "a",
"_id" : "1",
"found" : false
}

如预期的那样,文档已被删除。

实验 3:script 使用 params 作为单行

这是我的 shell 脚本:

# Index document.
curl -sX PUT -H 'Content-Type: application/json' \
http://localhost:9200/a/a/1?pretty -d '{ "n": 10 }'

# Print document.
sleep 1
curl http://localhost:9200/a/a/1?pretty

# Delete document (single-line script)
curl -siX POST -H 'Content-Type: application/json' \
http://localhost:9200/a/a/1/_update?pretty -d '{

"script": "ctx.op = ctx._source.n == params.count ? \"delete\" : \"none\"",
"params": {
"count": 10
}
}'

# Print document.
sleep 1
curl -si http://localhost:9200/a/a/1?pretty

这是最后两个 curl 命令的输出:

+ curl -siX POST -H 'Content-Type: application/json' 'http://localhost:9200/a/a/1/_update?pretty' -d '{

"script": "ctx.op = ctx._source.n == params.count ? \"delete\" : \"none\"",
"params": {
"count": 10
}
}'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 169

{
"_index" : "a",
"_type" : "a",
"_id" : "1",
"_version" : 1,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
}
}
+ sleep 1
+ curl -si 'http://localhost:9200/a/a/1?pretty'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 123

{
"_index" : "a",
"_type" : "a",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"n" : 10
}
}

为什么这次没有删除文档?

根据 https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html 处的文档这应该删除文件。引用文档:

We can even choose to delete a document based on its contents, by setting ctx.op to delete:

POST /website/blog/1/_update
{
"script" : "ctx.op = ctx._source.views == count ? 'delete' : 'none'",
"params" : {
"count": 1
}
}

为什么这不起作用?

最佳答案

我认为这是权威指南中的一个错误,它实际上是一种正在进行中的工作(当前有效版本适用于 ES 的 2.x 版本)。错误基本上是在 script 外壳之外使用 params。您的实验 #2 应该是将 params 与脚本一起用于部分 _update 的正确格式。

但是,我认为它也是错误的,因为使用此语法时 ES 不会返回错误消息。因此,我创建了 https://github.com/elastic/elasticsearch/issues/28740为此。

关于curl - 为什么 Elasticsearch _update with "script"using "params"不删除文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com