【问题标题】:How change the syntax in Elasticsearch 8 where 'body' parameter is deprecated?如何更改不推荐使用“body”参数的 Elasticsearch 8 中的语法?
【发布时间】:2022-07-05 16:38:25
【问题描述】:

将Python包elasticsearch从7.6.0更新到8.1.0后,在这行代码开始报错:

count = es.count(index=my_index, body={'query': query['query']} )["count"]

收到以下错误消息:

DeprecationWarning:“body”参数已弃用,将 在未来的版本中删除。而是使用单个参数。
count = es.count(index=ums_index, body={'query': query['query']} )["计数"]

我不明白如何使用上述“个别参数”。 这是我的查询:

query = {
    "bool": {
        "must": 
        [
                {"exists" : { "field" : 'device'}},
                {"exists" : { "field" : 'app_version'}},                    
                {"exists" : { "field" : 'updatecheck'}},
                {"exists" : { "field" : 'updatecheck_status'}},
                {"term" : { "updatecheck_status" : 'ok'}},
                {"term" : { "updatecheck" : 1}},
                {
                    "range": {
                    "@timestamp": {
                        "gte": from_date,
                        "lte": to_date,
                        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
                        }
                    }
                }
        ],
        "must_not":
        [
                {"term" : { "device" : ""}},
                {"term" : { "updatecheck" : ""}},
                {"term" : { "updatecheck_status" : ""}},
                {
                    "terms" : { 
                        "app_version" : ['2.2.1.1', '2.2.1.2', '2.2.1.3', '2.2.1.4', '2.2.1.5',
                                        '2.2.1.6', '2.2.1.7', '2.1.2.9', '2.1.3.2', '0.0.0.0', '']
                    }
                }
        ]
    }
}

在官方文档中,我找不到任何机会找到如何在新版本的 Elasticsearch 中传递我的查询的示例。

除了恢复到以前版本的 Elasticsearch 之外,可能有人对此情况有解决方案吗?

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    根据文档,现在按如下方式完成:

    # ✅ New usage:
    es.search(query={...})
    
    # ❌ Deprecated usage:
    es.search(body={"query": {...}})
    

    所以查询直接在同一行代码中完成,没有“body”,用您需要使用的 api 替换,在您的情况下,“count”代替“search”。 您可以尝试以下方法:

    # ✅ New usage:
    es.count(query={...})
    
    # ❌ Deprecated usage:
    es.count(body={"query": {...}})
    enter code here
    

    您可以点击以下链接了解更多信息:

    https://github.com/elastic/elasticsearch-py/issues/1698

    例如,如果查询是:

    GET index-00001/_count
    {
        "query" : {
            "match_all": {
            }
        }
    }
    

    Python 客户端将是下一个:

    my_index = "index-00001"
    query =  {
               "match_all": {
                }
              }
    hits = en.count(index=my_index, query=query)
    

    hits = en.count(index=my_index, query={"match_all": {}})
    

    【讨论】:

    • 当我尝试按照建议修改参数时:"""count = en.count(index= my_index, query=query['query'] )["count"]""" 而不是"""count = en.count(index= my_index, body={'query': query['query']} )["count"]""" - 我收到这个错误“客户端注意到服务器不是Elasticsearch,我们不支持这个未知产品”
    • 我已经用一个例子更新了答案。我希望它有效或有用
    • 我认为错误消息表明您的弹性搜索集群存在连接问题,而不是查询您可以试试这个:client = Elasticsearch(cloud_id=CLOUD_ID,basic_auth=("elastic", ELASTIC_PASSWORD) ) 然后:client.info()。 elastic.co/guide/en/elasticsearch/client/python-api/master/…
    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多