【问题标题】:Delete Indexes by index name and type using elasticSearch 2.3.3 in java在java中使用elasticSearch 2.3.3按索引名称和类型删除索引
【发布时间】:2016-07-27 06:49:31
【问题描述】:

我在 java 中有一个项目,我使用弹性搜索 2.3.3 对数据进行索引。索引有两种类型。

我的索引文档看起来像:

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
  "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
        {
           "_index": "test_index",
           "_type": "movies",
           "_id": "uReb0g9KSLKS18sTATdr3A",
           "_score": 1,
           "_source": {
              "genre": "Thriller"
            }
       },
       {
           "_index": "test_index",
           "_type": "drama",
           "_id": "cReb0g9KSKLS18sTATdr3B",
           "_score": 1,
           "_source": {
              "genre": "SuperNatural"
            }
        },
        {
           "_index": "index1",
           "_type": "drama",
           "_id": "cReb0g9KSKLS18sT76ng3B",
           "_score": 1,
           "_source": {
              "genre": "Romance"
            }
         }
      ]
   }
}

我只需要删除特定名称和类型的索引。

例如:- 从上面的文档中,我想删除名称为 "test_index" 的索引并输入 "drama"

所以结果应该是这样的:

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
  "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
        {
           "_index": "test_index",
           "_type": "movies",
           "_id": "uReb0g9KSLKS18sTATdr3A",
           "_score": 1,
           "_source": {
              "genre": "Thriller"
            }
       },
       {
           "_index": "index1",
           "_type": "drama",
           "_id": "cReb0g9KSKLS18sT76ng3B",
           "_score": 1,
           "_source": {
              "genre": "Romance"
            }
         }
      ]
   }
}

尝试过的解决方案:

client.admin().indices().delete(new DeleteIndexRequest("test_index").actionGet();

但它会删除名称为“test_index”的两个索引

我还尝试了 sense beta 插件中的各种查询,例如:

删除 /test_index/drama

它给出了错误:No handler found for uri [/test_index/drama] and method [DELETE]

删除 /test_index/drama/_query?q=_id:*&analyze_wildcard=true

它也不起作用。

当我触发删除索引请求时,我们不知道索引的 ID,我必须仅按名称和类型删除索引。

如何使用 java api 删除所需的索引?

【问题讨论】:

  • 你安装了delete-by-query plugin吗?最新的查询对我有用,只删除 test_index/drama 中的文档

标签: spring elasticsearch java


【解决方案1】:

这在 ES 2.0 之前使用删除映射 API 是可能的,但是由于 2.0 Delete Mapping API 不再存在。 为此,您必须安装 Delete by Query 插件。然后,您可以简单地对您的索引和类型进行匹配所有查询,然后将它们全部删除。

查询将如下所示:

DELETE /test_index/drama/_query
{
  "query": { 
    "query": {
        "match_all": {}
    }
  }
}

另外请记住,这将删除映射中的文档,而不是映射本身。如果您也想删除映射,则必须在没有映射的情况下重新索引。

This 或许可以帮你实现java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多