通过查询删除无效

时间:2016-12-26 14:26:50

标签: elasticsearch

我正在尝试删除日期低于12月1日的文档但看起来它实际上并没有删除任何内容。

我尝试使用delete by query 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"
                    }
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:9)

你的语法确实是正确的。在版本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"
      }
    }
  }
}
相关问题