我在 java 中有一个项目,我使用 Elasticsearch 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
它给出错误:找不到 uri [/test_index/drama] 和方法 [DELETE] 的处理程序
DELETE/test_index/drama/_query?q=_id:*&analyze_wildcard=true
它也不起作用。
当我触发删除索引请求时,索引的 ID 对我们来说是未知的,我必须仅按名称和类型删除索引。
如何使用 java api 删除所需的索引?
最佳答案
这在 ES 2.0 之前使用删除映射 API 是可能的,但是因为 2.0 Delete Mapping
API 不再存在。
为此,您必须安装 Delete by Query插入。然后你可以简单地对你的索引和类型进行匹配所有查询,然后删除所有它们。
查询看起来像这样:
DELETE /test_index/drama/_query
{
"query": {
"query": {
"match_all": {}
}
}
}
另请记住,这将删除映射中的文档,而不是映射本身。如果您也想删除映射,则必须在没有映射的情况下重新编制索引。
This可能可以帮助您实现 java
关于spring - 在 java 中使用 elasticSearch 2.3.3 按索引名称和类型删除索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38605662/