如何在不删除索引映射的情况下从elasticsearch数据库中删除数据?
我是Tire gem并使用delete命令删除我的所有映射并再次运行create命令。我想避免一次又一次地运行create命令。
这个你能帮我吗。
发现它在http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
DELETE <index>/_query
{
"query" : {
"match_all": {}
}
}
您也可以通过将其更改为DELETE <index>/<type>/_query
来删除特定类型
这将删除数据并维护映射,设置等。
您可以使用index templates,它将应用于名称与模式匹配的索引。
这样你就可以使用delete index api删除索引(比删除其中的所有文档更好),当你重新创建相同的索引时,匹配的索引模板将应用于它,这样你就不需要重新创建它了映射,设置,保温器......
会发生的事情是,映射将被删除,因为它们引用您删除的索引,但由于它们也存储在索引模板中,因此稍后在重新创建相同的索引时不需要再次重新提交它们。
按查询删除在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" } }
'
由于ElasticSearch删除文档的方式(通过使用bitset标记文档进行删除),迭代X个文档并将其标记为删除是不值得的。我相信当你刷新一个索引时,它将通过删除所有标记删除位集的文件来释放内存,这是一个昂贵的操作并且减慢了索引所在的分片。
希望这可以帮助。
根据最新的docs更新Yehosef的答案(截至本文6.2):
POST <index>/_delete_by_query
{
"query" : {
"match_all": {}
}
}