question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

create(**kwargs) doesn't work if document id is not provided

See original GitHub issue

Elasticsearch version (bin/elasticsearch --version): 7.6.2

elasticsearch-py version (elasticsearch.__versionstr__): 7.7.1

Please make sure the major version matches the Elasticsearch server you are running.

Description of the problem including expected versus actual behavior: Calling create(**kwargs) without providing a document Id causes an error when it should create a new document with autogenerated Id using POST method (instead of PUT).

Issue in init.py:

    for param in (index, id, body): # id should be removed from here
        if param in SKIP_IN_PATH:
            raise ValueError("Empty value passed for a required argument.")

    if doc_type in SKIP_IN_PATH:
        path = _make_path(index, "_create", id)
    else:
        path = _make_path(index, doc_type, id)

    return self.transport.perform_request(
        "POST" if id in SKIP_IN_PATH else "PUT", # calling "POST" instead of "PUT" is already implemented
        path,
        params=params,
        headers=headers,
        body=body,
    )

Steps to reproduce: Call create method without providing a document Id and it will throw a ValueError

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sethmlarsoncommented, May 29, 2020

@DuoPegla You’re able to create documents without specifying the id via .index():

from elasticsearch import Elasticsearch
es = Elasticsearch()
resp = es.index("test-index", body={"key": "value"})

print(resp)
{
  "_id": "GjlAYXIBW0qL5IyTJ2x1",  # Auto-generated ID
  "_index": "test-index",
  "_primary_term": 1,
  "_seq_no": 3,
  "_shards": {
    "failed": 0,
    "successful": 1,
    "total": 2
  },
  "_version": 1,
  "result": "created"
}

You can think of create as just for “create a document w/ ID without the option of updating it”. .create() requires an ID to verify that the document doesn’t already exist (will error in this case). .index() when given an ID will either create the document or update it if it already exists. Basically .index() is what you’re looking for here. 😃

0reactions
DuoPeglacommented, May 29, 2020

Thanks a lot! I must have missed that in the docs, just assumed given the name that create is the one I’m looking for.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is is safe to use a function accepts kwargs keyword arguments ...
So, the documentation indicates that the mapping given to ** in a function call should only contain valid identifiers as keys, but CPython...
Read more >
Supporting (or not) invalid identifiers in **kwargs - Ideas
I don't see why a string key that's not a valid identifier would open up an attack vector. Maybe you don't know how...
Read more >
Make any callable compatible with (*args: Any, **kwargs: Any)?
Mypy lets you call such callable values with arbitrary arguments, without any checking – in this respect they are treated similar to a...
Read more >
*args and **kwargs in Python - GeeksforGeeks
The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is...
Read more >
API — Flask Documentation (2.2.x)
This part of the documentation covers all the interfaces of Flask. ... If the import name is not properly set up, that debugging...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found