Tuesday, October 25, 2022

[FIXED] How change the syntax in Elasticsearch 8 where 'body' parameter is deprecated?

Issue

After updating Python package elasticsearch from 7.6.0 to 8.1.0, I started to receive an error at this line of code:

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

receive following error message:

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"]

I don't understand how to use the above-mentioned "individual parameters". Here is my 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', '']
                    }
                }
        ]
    }
}

In the official documentation, I can't find any chance to find examples of how to pass my query in new versions of Elasticsearch.

Possibly someone has a solution for this case other than reverting to previous versions of Elasticsearch?


Solution

According to the documentation, this is now to be done as follows:

# ✅ New usage:
es.search(query={...})

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

So the queries are done directly in the same line of code without "body", substituting the api you need to use, in your case "count" for "search". You can try the following:

# ✅ New usage:
es.count(query={...})

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

You can find out more by clicking on the following link:

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

For example, if the query would be:

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

Python client would be the next:

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

or

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


Answered By - Miguel Barrios
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.