[英]Removing Data From ElasticSearch
我想从 ElasticSearch 中删除数据。我已经删除了我的索引。 但是,这似乎并没有真正删除数据本身。 我看到的其他内容指向“ 按查询删除”功能。 但是,我什至不确定要查询什么。 我知道我的指标。 本质上,我想弄清楚如何做
DELETE FROM [Index]
来自 Chrome 中的 PostMan。 但是,我没有任何运气。 似乎无论我做什么,数据都在附近。 到目前为止,我已经通过在 PostMan 中使用 DELETE HTTP 动词并使用 url 成功删除了索引,例如:
http://localhost:9200/[indexName]
但是,这似乎并没有真正删除数据(又名文档)本身。
如果您需要删除所有索引,这可能会派上用场:
curl -X DELETE 'http://localhost:9200/_all'
电源外壳:
Invoke-WebRequest -method DELETE http://localhost:9200/_all
您可以使用cURL
删除,也可以使用开源爱好者为 Elasticsearch 创建的众多工具之一进行可视化删除。
使用卷曲
curl -XDELETE localhost:9200/index/type/documentID
例如
curl -XDELETE localhost:9200/shop/product/1
然后,您将收到有关此操作是否成功的回复。 您也可以删除整个索引或带有索引的类型,您可以通过省略文档 ID 来删除类型,如下所示 -
curl -XDELETE localhost:9200/shop/product
如果您想删除索引 -
curl -XDELETE localhost:9200/shop
如果您希望删除多个遵循特定命名约定的索引(注意*
,一个通配符), -
curl -XDELETE localhost:9200/.mar*
视觉上
上面提到了各种工具,我不会在此处列出它们,但我会将您链接到一个可以让您立即开始使用的工具,位于此处。 这个工具叫做 KOPF,要连接到你的主机,请点击左上角的标志并输入你的集群的 URL。
连接后,您将能够管理整个集群、删除、优化和调整集群。
文档(或The Definitive Guide )说,您还可以使用下一个查询来删除所有索引:
curl -XDELETE 'http://localhost:9200/*'
还有一个重要的说明:
对于某些人来说,使用单个命令删除所有数据的能力是一个非常可怕的前景。 如果要消除意外大量删除的可能性,可以在
elasticsearch.yml
中将以下设置为true
:
action.destructive_requires_name: true
您必须发送DELETE
请求到
http://[your_host]:9200/[your_index_name_here]
您还可以删除单个文档:
http://[your_host]:9200/[your_index_name_here]/[your_type_here]/[your_doc_id]
我建议你使用elastichammer 。
删除后,您可以使用以下 URL 查找索引是否仍然存在: http://[your_host]:9200/_stats/
祝你好运!
#list all index: curl -XGET http://localhost:9200/_cat/indices?v
#delete index: curl -XDELETE 'localhost:9200/index_name'
#delete all indices: curl -XDELETE 'localhost:9200/_all'
#delete document : curl -XDELETE 'localhost:9200/index_name/type_name/document_id'
安装kibana 。 Kibana 有一个更智能的开发工具,可以帮助轻松构建查询。
删除索引将删除映射和类型。 您可以通过以下查询删除所有行
curl -XDELETE 'localhost:9200/twitter/tweet/_query?pretty' -d'
{
"query": {
"match_all":
}
}'
但是对于上述查询,您需要安装按查询删除插件,因为 Elasticsearch 的 2.0.0-beta1 按查询删除已从主 api 中删除
Install delete-by-query plugin
sudo bin/plugin install delete-by-query
更多
http://blog.applieinformaticsinc.com/how-to-delete-elasticsearch-data-records-by-dsl-query/
curl -X DELETE 'https://localhost:9200/_all'
如果您在应用程序中使用 SSL 证书,请将http
更改为https
您可以按如下方式在python中删除索引
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host':'localhost', 'port':'9200'}])
es.index(index='grades',doc_type='ist_samester',id=1,body={
"Name":"Programming Fundamentals",
"Grade":"A"
})
es.indices.delete(index='grades')
最简单的方法!
Endpoint :
http://localhost:9201/twitter/_delete_by_query
Payload :
{
"query": {
"match": {
"message": "some message"
}
}
}
其中twitter
是弹性搜索中的索引
参考; https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
列出索引curl -L localhost:9200/_cat/indices
9200
默认端口[如果使用其他端口,请更改端口]
您可能会发现所有以logstash-yyyy-mm-dd
格式(logstash-*)开头的索引
您可以查看所有索引并使用
要删除索引和数据触发以下命令。
curl -XDELETE localhost:9200/index_name
(这将同时删除数据和索引)。
对于按查询批量删除,您可以使用特殊的按查询 API 删除:
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d '{
"query" : {
"term" : { "user" : "kimchy" }
}
}
谁有趣它有悠久的历史。
您可以删除整个索引、文档类型或特定的 id 数据。 这是三种方式:
curl -XDELETE localhost:9200/index_name
curl -XDELETE localhost:9200/index_name/doc-type
curl -XDELETE localhost:9200/index_name/doc-type/documentId
如果您想删除所有索引,请使用通配符。
我想删除logstash索引并搜索了很多关于curl等不同工具的信息。 但最终找到了解决方案。 登录到 Kibana。 转到开发工具选项卡并在查询字段中键入DELETE /logstash-*
并点击绿色箭头按钮。 如果您得到“确认”:响应为 true,这意味着数据已被清除。
1.删除API
从指定索引中删除文档。
DELETE /<index>/_doc/<_id>
例子:
DELETE http://localhost:9200/my-index-000001/_doc/1
参考: ES指南>>删除API
2.通过查询API删除
删除与指定查询匹配的文档。
例子:
POST http://localhost:9200/my-index-000001/_delete_by_query
{
"query": {
"match": {
"user.id": "elkbee"
}
}
}
参考: ES指南>>通过查询API删除
这里有很多很好的答案,但我还想补充一点:
您还可以使用“elasticsearch head”( Chrome 插件)中的 DELETE 操作删除索引。 将其添加到您的 chrome 并将其连接到您的主机。 在那里您会找到所有索引,如果您单击要删除的索引下方的操作按钮,您将在下拉列表中找到删除选项。 单击它并在弹出窗口中输入 DELETE。 您的索引将被删除。 “Elasticsearch head”扩展是一种查看和管理索引和数据的简单方法。
您可以删除一个或多个索引,这实际上会从磁盘中删除它们的文件。 例如:
curl -XDELETE localhost:9200/$INDEXNAME
其中$INDEXNAME
可以是索引名称(例如users_v2
),N 个以逗号分隔的索引(例如users_v2,users_v3
)。 索引模式(例如users_*
)或_all
也可以使用,除非它在配置中通过 action.破坏性_requires_name action.destructive_requires_name: true
被阻止。
可以删除单个文档,但这不会立即清除它们。 删除只是软删除,文档在段合并期间真正被删除。 您将在此演示文稿中找到有关分段和合并的大量详细信息。 这是关于 Solr,但合并来自 Lucene,因此您在 Elasticsearch 中具有相同的选项。
回到 API,您可以按 ID 删除单个文档(如果您使用路由进行索引,请提供路由值):
curl -XDELETE localhost:9200/users_v2/_doc/user1
或通过查询:
curl -XPOST -H 'Content-Type: application/json' localhost:9200/users_v2/_delete_by_query -d '{
"query": {
"match": {
"description_field": "bad user"
}
}
}'
您还可以使用 chrome 扩展 elasticsearch-head 删除索引
假设我需要删除一个索引filebeat-7.6.2-2020.04.30-000001
并使用 curl DELETE 选项( curl -X DELETE "localhost:9200/filebeat-7.6.2-2020.04.30-000001?pretty"
) 并导致如下身份验证问题;
{
"error" : {
"type" : "security_exception",
"reason" : "missing authentication credentials for REST request [/filebeat-7.6.2-2020.04.30-000001?pretty]"
},
"status" : 401
}
在这里,您应该使用您为 Elasticsearch 提供的用户名和密码来验证 curl 请求。 那就试试
curl -X DELETE -u myelasticuser:myelasticpassword "localhost:9200/filebeat-7.6.2-2020.04.30-000001?pretty"
将导致{ "acknowledged" : true } 。
添加到delete_by_query
建议中,如果您想快速删除给定索引中的所有记录,设置scroll_size
和conflicts
参数可能也很重要。
POST http://localhost:9200/my-index-000001/_delete_by_query?scroll_size=100&conflicts=proceed
{
"query": {
"match_all": {}
}
}
你可以试试这个curl
:
curl --location --request DELETE 'http://<username>:<password>@<url>:9200/<index name>/<doc type>/<document id>
或者如果你不想在 URL 设置用户名和密码,那么也可以试试这个 curl:
curl --location --request DELETE 'http://<url>:9200/<index name>/<doc type>/<document id>' --header 'Authorization: Basic <Base64 encoded username:password>'
执行后响应正文将包含一个result
字段。 如果该字段的值为deleted
,则表示文档删除成功。
在本curl
中,我假设您已将弹性配置为使用http
。 如果您使用的是https
,只需将协议更改为https
。
如果你觉得在终端上操作这些东西不方便,你可以使用 Postman 以非常简单的方式生成删除请求来处理这个问题。
用于删除所有索引的python脚本:
import requests
import json
ES_HOST = "http://localhost:9200"
ES_URL = f"{ES_HOST}/_cat/indices?format=json"
indexes = requests.get(ES_URL).content
indexes = json.loads(index.decode())
for i in index:
index_name = i['index']
content = requests.delete(f"{ES_HOST}/{index_name}").content
print(content)
我使用开发工具删除数据
POST <index_name>/_delete_by_query
{
"query": {
"match_all": {}
}
}
例子
POST vehicle-data/_delete_by_query
{
"query": {
"match_all": {}
}
}
我正在使用Kibana作为查看和搜索弹性搜索数据的工具,我基于良好的用户体验推荐它。
以下查询将有助于从弹性搜索中删除数据 -
Case 1:
如果您知道该数据行的 ID:
DELETE /index_name/_doc/{id}
Case 2:
根据列值删除:假设我有一个名为abc
的列。
现在我必须获取基于 column_name 的_id
字段以最终删除该行。
GET /uts_checkout_configurations/_search { "query" : { "constant_score" : { "filter" : { "bool": { "must": {"exists": {"field": "ABC"}} } } } } }
最后,您将获得带有_id
作为其中一个字段的数据。
基于此删除 -
DELETE /index_name/_doc/{_id}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.