2016-12-26 92 views
1

我想刪除文件的日期比12月1日低,但它看起來不像它實際上刪除任何東西。刪除通過查詢不工作

我試着使用刪除通過查詢API:

curl -XPOST "http://localhost:9200/mediadata/events/_delete_by_query" -d' 
{ 
    "query": { 
    "range": { 
     "created_at": { 
     "lt": "2016-12-01 00:00:00" 
     } 
    } 
    } 
}' 

或者這句法:

curl -XDELETE 'http://localhost:9200/mediadata/events/_query' -d ... 

我得到這種結果:

{"_index":"mediadata","_type":"events","_id":"_delete_by_query","_version":10,"_shards":{"total":3,"successful":2,"failed":0},"created":false} 

在此先感謝。

編輯:這裏是映射:

{ 
    "mediadata": { 
     "mappings": { 
      "events": { 
       "properties": { 
        "channels": { 
         "properties": { 
          "kdata": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "mail": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "md5": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "mobile": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "ssp": { 
           "type": "string", 
           "index": "not_analyzed" 
          } 
         } 
        }, 
        "contents": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "created_at": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        }, 
        "editor": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "end": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        }, 
        "location": { 
         "type": "geo_point" 
        }, 
        "message": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "price": { 
         "type": "double" 
        }, 
        "quantity": { 
         "type": "long" 
        }, 
        "query": { 
         "properties": { 
          "bool": { 
           "properties": { 
            "filter": { 
             "properties": { 
              "range": { 
               "properties": { 
                "created_at": { 
                 "properties": { 
                  "lt": { 
                   "type": "string" 
                  } 
                 } 
                } 
               } 
              } 
             } 
            }, 
            "must": { 
             "properties": { 
              "match_all": { 
               "type": "object" 
              } 
             } 
            } 
           } 
          }, 
          "filtered": { 
           "properties": { 
            "filter": { 
             "properties": { 
              "range": { 
               "properties": { 
                "created_at": { 
                 "properties": { 
                  "lt": { 
                   "type": "string" 
                  } 
                 } 
                } 
               } 
              } 
             } 
            }, 
            "query": { 
             "properties": { 
              "match_all": { 
               "type": "object" 
              } 
             } 
            } 
           } 
          }, 
          "range": { 
           "properties": { 
            "created_at": { 
             "properties": { 
              "lt": { 
               "type": "string" 
              }, 
              "lte": { 
               "type": "string" 
              } 
             } 
            } 
           } 
          } 
         } 
        }, 
        "reference": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "source": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "start": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        }, 
        "type": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "updated_at": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        } 
       } 
      } 
     } 
    } 

} 
+0

分享您正在使用的映射。是否將字段映射爲日期? –

+0

@FacundoLaRocca添加了映射,是的,它的類型是date – azekirel555

+0

@ azekirel555您是否使用GET查詢得到結果? I – Yeikel

回答

5

你的語法確實是正確的。在版本5.x中,通過查詢刪除如下。

POST mediadata/events/_delete_by_query?conflicts=proceed 
{ 
    "query": { 
    "range": { 
     "created_at": { 
     "gt": "2016-11-02 00:00:00" 
     } 
    } 
    } 
} 

現在的基礎上,你是從ES

{"_index":"mediadata","_type":"events","_id":"_delete_by_query","_version":10,"_shards":{"total":3,"successful":2,"failed":0},"created":false} 

得到我會假設你正在運行2.x版,其中的語法是不同的反應。

首先,在版本2.x通過查詢刪除是,你需要使用安裝插件:

plugin install delete-by-query 

然後運行它:

curl -XDELETE "http://localhost:9200/mediadata/events/_query" -d' 
{ 
    "query": { 
    "range": { 
     "created_at": { 
     "gt": "2016-11-02 00:00:00" 
     } 
    } 
    } 
}' 

響應看起來像:

{ 
    "took": 0, 
    "timed_out": false, 
    "_indices": { 
    "_all": { 
     "found": 1, 
     "deleted": 1, 
     "missing": 0, 
     "failed": 0 
    }, 
    "mediadata": { 
     "found": 1, 
     "deleted": 1, 
     "missing": 0, 
     "failed": 0 
    } 
    }, 
    "failures": [] 
} 

完整的示例:

PUT mediadata 
{ 
    "mappings": { 
      "events": { 
       "properties": { 
        "channels": { 
         "properties": { 
          "kdata": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "mail": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "md5": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "mobile": { 
           "type": "string", 
           "index": "not_analyzed" 
          }, 
          "ssp": { 
           "type": "string", 
           "index": "not_analyzed" 
          } 
         } 
        }, 
        "contents": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "created_at": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        }, 
        "editor": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "end": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        }, 
        "location": { 
         "type": "geo_point" 
        }, 
        "message": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "price": { 
         "type": "double" 
        }, 
        "quantity": { 
         "type": "long" 
        }, 
        "query": { 
         "properties": { 
          "bool": { 
           "properties": { 
            "filter": { 
             "properties": { 
              "range": { 
               "properties": { 
                "created_at": { 
                 "properties": { 
                  "lt": { 
                   "type": "string" 
                  } 
                 } 
                } 
               } 
              } 
             } 
            }, 
            "must": { 
             "properties": { 
              "match_all": { 
               "type": "object" 
              } 
             } 
            } 
           } 
          }, 
          "filtered": { 
           "properties": { 
            "filter": { 
             "properties": { 
              "range": { 
               "properties": { 
                "created_at": { 
                 "properties": { 
                  "lt": { 
                   "type": "string" 
                  } 
                 } 
                } 
               } 
              } 
             } 
            }, 
            "query": { 
             "properties": { 
              "match_all": { 
               "type": "object" 
              } 
             } 
            } 
           } 
          }, 
          "range": { 
           "properties": { 
            "created_at": { 
             "properties": { 
              "lt": { 
               "type": "string" 
              }, 
              "lte": { 
               "type": "string" 
              } 
             } 
            } 
           } 
          } 
         } 
        }, 
        "reference": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "source": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "start": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        }, 
        "type": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "updated_at": { 
         "type": "date", 
         "format": "yyyy-MM-dd' 'HH:mm:ss" 
        } 
       } 
      } 
     } 
} 

PUT mediadata/events/1 
{ 
    "created_at" : "2016-11-02 00:00:00" 
} 


PUT mediadata/events/3 
{ 
    "created_at" : "2016-11-03 00:00:00" 
} 

#The one to delete 
PUT mediadata/events/4 
{ 
    "created_at" : "2016-10-03 00:00:00" 
} 

#to verify that the documents are in the index 
GET mediadata/events/_search 
{ 
    "query": { 
    "range": { 
     "created_at": { 
     "lt": "2016-11-02 00:00:00" 
     } 
    } 
    } 
} 

DELETE /mediadata/events/_query 
{ 
    "query": { 
    "range": { 
     "created_at": { 
     "gt": "2016-11-02 00:00:00" 
     } 
    } 
    } 
} 
+0

謝謝,我安裝了插件並重新啓動了ES。該查詢似乎現在運行良好,但尚未完成,但我看到文檔數量在減少。再次感謝。 – azekirel555

+0

@ azekirel555不客氣。 – Yeikel