删除Elasticsearch索引而不删除其映射

时间:2014-01-22 10:04:43

标签: database elasticsearch tire

如何在不删除索引映射的情况下从弹性搜索数据库中删除数据?

我是Tire gem并使用delete命令删除所有映射并再次运行create命令。我想避免一次又一次地运行create命令。

请帮我解决这个问题。

5 个答案:

答案 0 :(得分:4)

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

找到了它
DELETE <index>/_query
{
  "query" : {
    "match_all": {}
  }
}

您也可以通过将其更改为DELETE <index>/<type>/_query

来删除特定类型

这将删除数据并维护映射,设置等。

答案 1 :(得分:3)

您可以使用index templates,它将应用于名称与模式匹配的索引。

这样你就可以使用delete index api删除索引(比删除其中的所有文档更好),当你重新创建相同的索引时,匹配的索引模板将被应用到它,这样你就可以了不需要重新创建它的映射,设置,加热器......

当发生映射时,映射将被删除,因为它们引用了您删除的索引,但由于它们存储在索引模板中,因此稍后在重新创建相同的索引时不需要再次重新提交映射。

答案 2 :(得分:1)

在1.5.3

中不推荐按查询删除

您应该使用滚动/扫描API查找所有匹配的ID,然后发出批量请求以删除它们。

记录在案here

curl -XGET 'localhost:9200/realestate/houses/_search?scroll=1m' -d '
{
    "query": {
        "match_all" : { }
    },
    "fields": []
}
'

然后批量删除(不要忘记在最后一行之后添加新行)

curl -XPOST 'localhost:9200/_bulk' -d '
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "1" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "2" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "3" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "4" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "5" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "6" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "7" } }
{ "delete" : { "_index" : "realestate", "_type" : "houses", "_id" : "8" } }
'

答案 3 :(得分:0)

由于ElasticSearch删除文档的方式(通过使用bitset标记文档进行删除),迭代X个文档并将其标记为删除是不值得的。我相信当你刷新一个索引时,它将通过删除所有标记删除位集的文件来释放内存,这是一个昂贵的操作并且减慢了索引所在的分片。

希望这有帮助。

答案 4 :(得分:0)

根据最新的docs(截至本文6.2)更新Yehosef的答案:

POST <index>/_delete_by_query
{
  "query" : {
    "match_all": {}
  }
}