使用elasticserch ds更新elasticsearch数据

2024-04-25 13:34:37 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用elasticsearch-dsl包更新elasticsearch数据?有可能吗?

我找到了elasticsearch update api,但似乎有点困难。我要找的是,

searchObj = Search(using=logserver, index=INDEX)
searchObj=searchObj.query("term",attribute=value).update(attribute=new_value)
response = searchObj.execute()

Tags: 数据apisearchindexvalueupdateattributeelasticsearch
3条回答

如果我没说错,elasticsearch_dsl没有更新/批量更新的选项。
因此,如果您愿意,您可以使用elasticsearch-pypckage进行相同的操作。

示例

from elasticsearch import Elasticsearch

INDEX = 'myindex'
LOG_HOST = 'myhost:myport'
logserver = Elasticsearch(LOG_HOST)

script = "ctx._source.attribute = new_value"

update_body = {

    "query": {
        "bool": {
            "filter": {
                "term": {"attribute": "value"}
            }
        }
    },

    "script": {
        "source": script,
        "lang": "painless"

    }

}

update_response = logserver.update_by_query(index=INDEX, body=update_body)



有关详细信息,请参阅official documentation

@kingArther的回答不正确。在

elasticsearch-dsl非常支持更新!在

通过将索引映射到一个对象(DocType),它允许 您可以轻松地保存和更新,而不需要任何JSON rest请求。在

您可以找到一个API here示例

可能已经晚了,但我将此回复留给那些仍然有同样问题的人: elasticsearch dsl提供了更新函数,可以在扩展elasticsearch dsl文档类的类上调用该函数。代码如下:

data = yourIndexClass.get(id=documentIdInIndex)
data.update(key=NewValue)

就这样。简单。Find details Here

相关问题 更多 >