elasticsearch,Curl,elasticsearch" /> elasticsearch,Curl,elasticsearch" />

Curl 为什么Elasticsearch“更新为”;脚本“;使用;“参数”;不删除文档?

Curl 为什么Elasticsearch“更新为”;脚本“;使用;“参数”;不删除文档?,curl,elasticsearch,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 'Conten

我用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
# 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
# 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 == 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
}
+ 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
}
+ 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
  }
}
正如所料,该文档已被删除

实验2:
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
# 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
# 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 == 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
}
+ 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
}
+ 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
  }
}
正如所料,该文档已被删除

实验3:
script
作为一行使用
params
以下是我的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
# 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
# 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 == 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
}
+ 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
}
+ 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
  }
}
为什么这次没有删除文档

根据文件,此时本应删除该文件。引用文件:

我们甚至可以根据文档内容选择删除文档,方法是将
ctx.op
设置为
delete

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

为什么这不起作用?

我认为这是权威指南中的一个错误,它实际上是一种正在进行的工作(当前的有效版本适用于ES的2.x版本)。错误在于在
脚本
附件外部使用
参数。您的实验#2应该是使用
参数
和脚本进行部分
\u更新
的正确格式


但是,我认为它也是错误的,当使用这种语法时,ES不会返回错误消息。因此,我为此而创作。

我认为这是《权威指南》中的一个错误,它实际上是一种正在进行的工作。错误在于在
脚本
附件外部使用
参数。你的实验#2应该是在脚本中使用params进行部分
更新的正确格式。我创建了这个:@andrestefan你的评论,指南中的错误可能是这个问题的有效答案。你为什么不把你所知道的作为一个答案呢?很好,安德烈!