What is the correct call for the create() method in the python elasticsearch module?

Viewed 885

I am using the latest version, 7.15.0, of the python elasticsearch module (but version 6.2.4 for an Elasticsearch server running locally, as this is the version required by the customer I am developing for). But the following create() call:

        es.indices.create(   
            index=es_index_name,
            body=configs
        )

outputs the deprecation warning:

The 'body' parameter is deprecated for the 'create' API and will be removed in 8.0.0. Instead use API parameters directly. See https://github.com/elastic/elasticsearch-py/issues/1698 for more information

However when I change "body=configs" to just "configs", another deprecation warning pops up as follows:

Using positional arguments for APIs is deprecated and will be disabled in 8.0.0. Instead use only keyword arguments for all APIs. See https://github.com/elastic/elasticsearch-py/issues/1698 for more information

HUH?! Surely it must be one or the other!

So what is the correct incantation to use for this call and not see any deprecation warning? Most examples I have seen use the keyword argument, but they could be out of date.

In case it is relevant, configs is a dictionary comprising:

configs = {
"index": {
    "number_of_shards": "1",
    "analysis": {
        "analyzer": {
            "custom_keyword_analyzer" : {
                "filter" : [
                    "lowercase"
                ],
                # "type" : "custom",
                "tokenizer" : "keyword"
            },
            "quicksearch_search_analyzer" : {
                "filter" : [
                    "lowercase"
                ],
                # "type" : "custom",
                "tokenizer" : "whitespace"
            },
            "quicksearch_analyzer" : {
                "filter" : [
                    "lowercase"
                ],
                # "type" : "custom",
                "tokenizer" : "ngram_tokenizer"
            }
        },
        "tokenizer" : {
            "ngram_tokenizer" : {
                "type" : "ngram",
                "min_gram" : "2",
                "max_gram" : "3",
                "token_chars" : [
                    "digit",
                    "letter",
                    "punctuation",
                    "symbol"
                ]
            }
        }
    }
}

}

1 Answers

A bit late but I had the same warning today, I found this issue on GitHub(there you can find your same issue with create()), in my case I had this warning with the search() method.

To fix this I had to remove the key "query" from my dict and change the parameter from "body" to "query".

As example I changed following:

client.search(index="feed", body=body)

where body contains:

{'query': {'bool': {'must': [{'match_phrase': {'title': {'query': '**Confucious**'}}}, {'nested': {'path': 'indicators', 'query': {'term': {'indicators.role': {'value': 'maldoc'}}}}}]}}}

to:

client.search(index="feed", query=query)

where query contains

{'bool': {'must': [{'match_phrase': {'title': {'query': '**Confucious**'}}}, {'nested': {'path': 'indicators', 'query': {'term': {'indicators.role': {'value': 'maldoc'}}}}}]}}

Hope this helps.

Related