为什么Elasticsearch使用“params”使用“params”更新_update而不删除文档?

时间:2018-02-10 11:07:01

标签: curl elasticsearch

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

实验1:script为一行

这是我的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:script作为对象

这是我的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的文档,这应该删除了该文档。引用文档:

  

我们甚至可以通过将ctx.op设置为delete来选择根据文档内容删除文档:

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

为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

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

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