【Elasticsearch】查詢並刪除匹配文檔之_delete_by_query

思路:先查詢確認,後精準刪除

假設我想刪除title是」小明今晚真的不加班「這條記錄,先查看一下現有的記錄:程序員

(不加班很差嗎?爲何要刪除呢?)json

tips:能夠使用 match_phrase精準查詢,查詢命令能夠經過curl查詢,也能夠經過其餘工具請求(其實道理都同樣)
curl -X POST "http://192.168.16.65:9211/blog/_search" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_phrase": {
            "title": "小明今晚真的不加班"
        }
    }
}
'

blog爲索引,_search爲es的查詢指令,查詢結果以下:app

{
    "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": "程序員小明",
                "capture_time": 1583820020000,
                "content": "今晚能夠終於能夠王者榮耀帶妹了",
                "title": "小明今晚真的不加班",
                "top_domain": "mynamecoder.com",
                "url": "http://blog.mynamecoder.com"
            }
        }]
    }
}

能夠看到數據中有一條符合條件的文檔,咱們如今就要刪除該文檔。dom

刪除title爲"小明今晚真的不加班"的文檔(忍痛):curl

curl -X POST "http://192.168.16.65:9211/blog/_delete_by_query" -H 'Content-Type: application/json' -d'
{
  "query":{
    "match":{
      "title":"小明今晚真的不加班"
    }
  }
}
'
tips: 使用 _delete_by_query時,必須指定索引,此處 blog就是索引, _delete_by_query是elasticsearch的刪除指令

刪除結果:異步

{
  "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" : [ ]
}

重點關注total(查詢到的條數)和deleted(刪除的總數)兩個字段,最後不放心的話,能夠再查詢一下剛纔那個文檔是否還存在。elasticsearch

tips:es執行刪除的時候es並非當即刪除,雖然咱們再次查詢已經找不到了,但es自身是將該文檔先標記準備刪除狀態,一段時間後,異步刪除。
相關文章
相關標籤/搜索