pythonpython-3.xelasticsearchelasticsearch-pypyelasticsearch

Elasticsearch Python API results in "search() missing 1 required positional argument" on a simple query


I am desperately trying to perform a simple search operation on elasticsearch, but fail since hours. This is my code:

res = es.search(index="people", doc_type="test", body={"query":{"match":{"name": "john"}}})

I saw this many times on the internet, but I get always an error and have no clue what is wrong with my code snippet.

Traceback (most recent call last):
  File "./es_request.py", line 14, in <module>
    res = es.search(index="people", doc_type="test", body={"query":{"match":{"name": "john"}}})
  File "/usr/local/lib/python3.4/dist-packages/pyelasticsearch/client.py", line 93, in decorate
    return func(*args, query_params=query_params, **kwargs)
TypeError: search() missing 1 required positional argument: 'query'

Where do I have to write the required 'query'? I am trying to perform the search for name and last name.


Solution

  • The search method from pyelasticsearch library (which you are using) calls the query argument query, not body:

     query = {"query": {"match": {"name": "john"}}}
     es.search(index="people", doc_type="test", query=query)
     es.search(query, index="people", doc_type="test")  # you can also do this
    

    I think you meant to use the official elasticsearch library, with which your code works perfectly fine (to add more confusion, its documentation is located at elasticsearch-py.readthedocs.io).

    Both libraries export the same module name, so things can sometimes sort of work when you use the wrong one. This is not a coincidence, see this article on their history and differences. Installing the official client is easy as:

    pip uninstall pyelasticsearch
    pip install elasticsearch