在Elasticsearch中删除特定类型的文档

20
我想使用HTTP/REST api删除Elasticsearch中一个类型下已经索引的所有文档,但我不想删除该类型的映射。您可以如何构建URL查询来实现此操作?
7个回答

17
在执行命令之前,先查看索引/映射状态;(截图来自于elasticsearch head插件的Web界面) enter image description here enter image description here enter image description here 命令:
curl -XDELETE 'http://localhost:9200/publishercategoryeu/autocomplete/_query' -d '
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ]
    }
  }
}
'

结果;

enter image description here

执行命令后,索引/映射状态;

enter image description here

enter image description here

enter image description here

正如我们所看到的,我们删除了所有已经在一个类型(映射)中索引的文档,而不需要删除索引或类型(映射)。


"twitter" 是索引名称,"tweet" 是映射名称在该命令中。您是否已经正确替换它们? - csonuryilmaz
当然。我认为这取决于Elasticsearch的版本。我还从Elasticsearch指南中提取了一些查询,并得到了一个无法解析的异常。 - localhost
我会在今晚检查/测试我的帖子并通知您。请耐心等待。 :) - csonuryilmaz
我测试并详细说明了我的帖子。正如你所说,elasticsearch版本对于此操作非常重要。在文档(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html)中有一条注释提到了这一点。 - csonuryilmaz
6
注意:自 1.5.3 版本起,删除查询已被弃用且在 2.0 版本中不再可用。 - Rene Wooller

8
一个简单的匹配所有查询的删除操作应该就能解决问题。你可以在这里获取更多信息: delete by query api 另外,你也可以删除整个类型并利用模板api。只需在config/templates/文件夹中放置一个包含你的模板的文件,你就永远不会失去它。当你删除映射时,映射确实会丢失,但是一旦你再次索引某些内容,模板将被重复使用。这里有更多信息: template api 编辑:新的删除api:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html

2
似乎这已经过时了。 - Tjorriemorrie

4
通过 Elasticsearch Head 插件中的以下命令,我能够删除名为 logstash 的索引中所有类型为 logs 的文档,而不会删除映射:
{"query":{"match_all":{}}}

使用 Elasticsearch head 插件删除文档:
为了释放磁盘空间,在删除文档后,您还必须优化索引(在 head 插件中的操作菜单中选择“Optimize for index logstash”)。

1

以前的答案在最新版本的 Elasticsearch 上不起作用。从 Elasticsearch 2.0 开始,“按查询删除”已被弃用。 Elasticsearch 文档表示,在并发索引期间它可能会导致 OutOfMemoryError,并可能导致主副本不一致。如果您想要关注此问题的历史记录,请访问 Github

现在需要多个步骤才能从 type 中删除所有文档。

  1. 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.

  2. 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"}'
    
请注意,如果您要向curl提供文本文件输入,则必须使用--data-binary标志,而不是普通的-d

0
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query?q=user:kimchy'

$ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d '{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

0

如果你想在golang中完成这个任务,可以使用"olviere/elastic"库来编写代码。假设你已经有了一个客户端yourClient,以及yourIndexyourType的索引和类型:

    bq := elastic.NewBoolQuery()
    bq.Must(elastic.NewMatchAllQuery())
    _, err := elastic.NewDeleteByQueryService(yourClient).
        Index(yourIndex).
        Type(yourType).
        Query(bq).
        Do()

-3

使用:

curl -XDELETE 'http://{server}/{index_name}/{type_name}/'

(如文档中所示)


虽然这似乎可以工作,但它不是 delete_by_query 文档的一部分。但是,这也会删除映射,因此您应该使用完整的 delete_by_query,并配合一个匹配所有查询。 - rakensi
4
这将删除整个文档类型,而不仅仅是其中的文档。 - Oliver

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接