请求在Elasticsearch中通过ID删除记录失败?

时间:2019-07-05 14:49:52

标签: elasticsearch elasticsearch-5

我正在研究Elasticsearch 5,但没有找到一种方法来删除已插入其中的数据。

当我查询列出记录时,elasticsearch返回以下内容:

curl -X POST \
  http://localhost:9201/usersystem/_search \
  -d '{
    "query": {
        "terms": { "_id": [951] }
    }
}'

返回:

{
   "took":1,
   "timed_out":false,
   "_shards":{
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":1,
      "max_score":1.0,
      "hits":[
         {
            "_index":"usersystem",
            "_type":"usersystem",
            "_id":"951",
            "_score":1.0,
            "_source":{
               "id":951,
               "name":"User Name",
               "email":"user.name@host.com",
               "phone":"47-1234-9876",
               "documentCode":"9876543-8",
               "documentType":"RR",
               "gender":"MALE",
               "createdOn":"2019-07-04T20:11:47.314Z",
               "updateOn":null,
               "active":false,
               "userId":952
            }
         }
      ]
   }

}

阅读一些示例,我发出了以下返回错误的DELETE请求:

请求:

curl -X DELETE \
  http://localhost:9201/usersystem/_query \
  -d '{
    "query": {
        "terms": { "_id": [951] }
    }
}'

错误:No handler found for uri [/usersystem/_query] and method [DELETE]

如何发出删除请求以按_id或id删除记录?

3 个答案:

答案 0 :(得分:1)

使它像这样。

curl -X DELETE http://localhost:9201/usersystem/_doc/951

答案 1 :(得分:0)

如果您的搜索查询返回以下响应,

[
  {
    "_index": "usersystem",
    "_type": "usersystem",
    "_id": "951",
    "_score": 1,
    "_source": {
      ....
  }
]

然后从您的搜索查询的匹配响应中得知,您拥有index名称为 usersystem 且具有type usersystem 。因此,要通过 id 删除任何文档,您可以采用这种方式-

DELETE /usersystem/usersystem/951

OR

curl -X DELETE "localhost:9201/usersystem/usersystem/951"

查看更多信息: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html

答案 2 :(得分:0)

我在ElasticSearch文档中找到有关_delete_by_query的信息。这是对我有用的请购单:

curl -X POST \
  http://localhost:9201/usersystem/_delete_by_query \
  -d '{
    "query": {
        "terms": { "_id": [951] }
    }
}'