Click [elastic search] to query and delete the "delete by" of the matching document

Keywords: Java curl JSON ElasticSearch

Idea: query and confirm first, then delete accurately

If I want to delete the record with the title "Xiao Ming really doesn't work overtime tonight", first check the existing record:

Isn't it good not to work overtime? Why delete? )

tips: you can use match_phrase to query accurately. The query command can be queried by curl or by other tools (the truth is the same)

curl -X POST "http://192.168.16.65:9211/blog/_search" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_phrase": {
            "title": "Xiao Ming really doesn't work overtime tonight"
        }
    }
}
'

blog is the index and search is the query instruction of es. The query results are as follows:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 110.28655,
        "hits": [{
            "_index": "blog",
            "_type": "_doc",
            "_id": "6a0d343fb629da2e2cdf6f4bf250af04",
            "_score": 110.28655,
            "_source": {
                "author": "Xiaoming, programmer",
                "capture_time": 1583820020000,
                "content": "Tonight I can finally bring my sister to glory",
                "title": "Xiao Ming really doesn't work overtime tonight",
                "top_domain": "mynamecoder.com",
                "url": "http://blog.mynamecoder.com"
            }
        }]
    }
}

You can see that there is a qualified document in the data, and we will delete it now.

Delete the document title d "Xiao Ming really doesn't work overtime tonight" (enduring pain):

curl -X POST "http://192.168.16.65:9211/blog/_delete_by_query" -H 'Content-Type: application/json' -d'
{
  "query":{
    "match":{
      "title":"Xiao Ming really doesn't work overtime tonight"
    }
  }
}
'

tips: when using delete by query, you must specify the index. Here, blog is the index, and delete by query is the delete instruction of elasticsearch

Delete result:

{
  "took" : 147,
  "timed_out": false,
  "deleted": 1,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1.0,
  "throttled_until_millis": 0,
  "total": 119,
  "failures" : [ ]
}

Focus on the two fields of total (number of items queried) and deleted (total number of items deleted). If you are not sure, you can query whether the document just existed.

tips:es does not delete the document immediately when it is deleted. Although we can't find it again, es itself marks the document as ready to delete. After a period of time, the document is deleted asynchronously.

Posted by DiscoTrio on Sat, 21 Mar 2020 09:54:29 -0700