user2831852
user2831852

Reputation: 615

Request to delete record by id failure in Elasticsearch?

I'm studying Elasticsearch 5 and I have not found a way to delete the data that has been inserted into it.

When I query to list the records, elasticsearch returns this:

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

Return:

{
   "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":"[email protected]",
               "phone":"47-1234-9876",
               "documentCode":"9876543-8",
               "documentType":"RR",
               "gender":"MALE",
               "createdOn":"2019-07-04T20:11:47.314Z",
               "updateOn":null,
               "active":false,
               "userId":952
            }
         }
      ]
   }

}

Reading some examples, I made the following DELETE request that returns error:

Request:

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

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

How do I make the delete request to delete the record by _id or id?

Upvotes: 1

Views: 1737

Answers (3)

user2831852
user2831852

Reputation: 615

I found in ElasticSearch documentation about _delete_by_query. This is the requisition that worked for me:

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

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

If Your search query returns the below response,

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

then from your search query's hits response we came to know that your have index with name usersystem with a type usersystem. So to delete any document by id you can do this way-

DELETE /usersystem/usersystem/951

OR

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

See More: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html

Upvotes: 0

JBone
JBone

Reputation: 1836

make this something like this.

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

Upvotes: 2

Related Questions