【问题标题】:How to delete documents from multiple indices using particular field name in elasticsearch python?如何使用elasticsearch python中的特定字段名称从多个索引中删除文档?
【发布时间】:2018-07-01 20:18:42
【问题描述】:

这是示例文档。

{
"_index": "mqtt-index-2018.01.23",
"_type": "iot_data",
"_id": "AWEjA7LNRU4cTUO-Lyj4",
"_score": null,
"_source": {
"message": "{\"datastream_name\": \"roshni\", \"value\": 12, 
\"context\": {\"latitude\": 0, \"elevation\": 0, \"longitude\": 0}, 
\"device_id\": 31}",
"@version": "1",
"@timestamp": "2018-01-23T12:34:59.687Z",
"host": "iot-elk",
"topic": "telemetry/f2a55827ef554475a41c3c96369957f0/roshni",
"datastream_name": "roshni",
"value": 12,
"context": {
  "latitude": 0,
  "elevation": 0,
  "longitude": 0
},
"device_id": 31,
"tstamp": "2018-01-23T12:34:59.687Z"
},
"fields": {
"tstamp": [
  1516710899687
],
"@timestamp": [
  1516710899687
]
},
"sort": [
 1516710899687
]
}

我想使用 device_id 字段删除文档。 如何使用 API 调用或使用 python 客户端删除它?我已经尝试使用 Document _id 和特定索引,但我想通过使用 device_id 字段或其他字段来删除它。

【问题讨论】:

    标签: python curl elasticsearch logstash pyelasticsearch


    【解决方案1】:

    使用以下 DELETE API 调用从 elasticsearch 中的多个索引中删除文档。

    curl -XDELETE 'http://localhost:9200/mqtt-index-*/logs/_query' -d '{
    "query" : {
        "match" : {"device_id": 31}
    }
    }' -i
    

    【讨论】:

      【解决方案2】:

      没有示例代码的简答:

      • 将 json 转换为 dict(参见 Converting JSON String to Dictionary Not List
      • 如果您有许多这样的数据结构,如果它们符合您的搜索条件(例如if data["parsed_message"]["device_id"] not in list_of_forbidden_ids):),请构建它们的列表

      【讨论】:

        【解决方案3】:

        在使用原始API方面,我相信this is what you are looking for

        根据你使用的python库,在python中实际上更容易。我使用elasticesearch-dsl-py,您可以在其中构建查询对象。 You can call delete on these query objects.

        关于跨越多个索引,ElasticSearch 确实支持这一点,可以使用通配符 *,或者用逗号分隔索引。

        【讨论】:

        • client = Elasticsearch(hosts= '[172.16.2.121]') s = Search(using=client, index="mqtt-index-2018.01.24").query("match", value =10) response = s.delete() 我已经执行了这段代码并得到了错误 - elasticsearch.exceptions.RequestError: TransportError(400, u'invalid_type_name_exception', u"Document mapping type name can't start with '_'" )
        • response = s.delete() 通过运行这个,文档不会被删除。作为响应它的显示:>>> 打印响应:{u'_type': u'delete_by_query', u'created': True, u'_shards':...}
        【解决方案4】:

        您还可以删除匹配多个字段的文档。

        curl -XDELETE 'http://localhost:9200/mqtt-index-*/logs/_query' -d '{
        "query" : {
            "bool": {
                "must":[
            {"match" : {"device_id":31}}, 
            {"match":  {"datastream_name": "test"}}
            ]
        }
        }' -i
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-24
          • 2019-05-03
          相关资源
          最近更新 更多