如何更改 Elasticsearch 8 中不推荐使用 'body' 参数的语法?
How change the syntax in Elasticsearch 8 where 'body' parameter is deprecated?
将 Python 包 elasticsearch 从 7.6.0 更新到 8.1.0 后,我开始在这行代码收到错误:
count = es.count(index=my_index, body={'query': query['query']} )["count"]
收到以下错误消息:
DeprecationWarning: The 'body' parameter is deprecated and will be
removed in a future version. Instead use individual parameters.
count = es.count(index=ums_index, body={'query': query['query']}
)["count"]
我不明白上面提到的“个别参数”怎么用。
这是我的查询:
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 之外,对于这种情况可能有人有解决方案?
strong text根据文档,现在要做的如下:
# ✅ 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
点击以下link可以了解更多:
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": {}})
将 Python 包 elasticsearch 从 7.6.0 更新到 8.1.0 后,我开始在这行代码收到错误:
count = es.count(index=my_index, body={'query': query['query']} )["count"]
收到以下错误消息:
DeprecationWarning: The 'body' parameter is deprecated and will be removed in a future version. Instead use individual parameters.
count = es.count(index=ums_index, body={'query': query['query']} )["count"]
我不明白上面提到的“个别参数”怎么用。 这是我的查询:
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 之外,对于这种情况可能有人有解决方案?
strong text根据文档,现在要做的如下:
# ✅ 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
点击以下link可以了解更多:
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": {}})