我想使用HTTP/REST api删除Elasticsearch中一个类型下已经索引的所有文档,但我不想删除该类型的映射。您可以如何构建URL查询来实现此操作?
curl -XDELETE 'http://localhost:9200/publishercategoryeu/autocomplete/_query' -d '
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
]
}
}
}
'
结果;
执行命令后,索引/映射状态;
正如我们所看到的,我们删除了所有已经在一个类型(映射)中索引的文档,而不需要删除索引或类型(映射)。
logstash
的索引中所有类型为 logs
的文档,而不会删除映射:{"query":{"match_all":{}}}
以前的答案在最新版本的 Elasticsearch
上不起作用。从 Elasticsearch 2.0
开始,“按查询删除”已被弃用。 Elasticsearch
文档表示,在并发索引期间它可能会导致 OutOfMemoryError,并可能导致主副本不一致。如果您想要关注此问题的历史记录,请访问 Github。
现在需要多个步骤才能从 type
中删除所有文档。
Find all the ids of the document that you need to delete. The most efficient way to perform this operation is to use the scroll/scan API to find all the matching ids for a given type.
Issue a bulk request to delete the documents by ids. An example provided below.
curl -XPOST 'http://localhost:9200/_bulk' -d '
{ "delete": { "_index": "index", "_type": "type", "_id": "1"}
{ "delete": { "_index": "index", "_type": "type", "_id": "2"}'
--data-binary
标志,而不是普通的-d
。$ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query?q=user:kimchy'
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d '{
"query" : {
"term" : { "user" : "kimchy" }
}
}
如果你想在golang中完成这个任务,可以使用"olviere/elastic"库来编写代码。假设你已经有了一个客户端yourClient
,以及yourIndex
和yourType
的索引和类型:
bq := elastic.NewBoolQuery()
bq.Must(elastic.NewMatchAllQuery())
_, err := elastic.NewDeleteByQueryService(yourClient).
Index(yourIndex).
Type(yourType).
Query(bq).
Do()