Léo SOHEILY KHAH
Léo SOHEILY KHAH

Reputation: 777

Python: elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"code":404,"message":"HTTP 404 Not Found"}')

I want to GET the data from a given URL (with a prefix) using ElasticSearch in Python. Here is my code:

if __name__ == '__main__':
    username = "xxxx"
    password = "xxxx"
    url = "http://xxxx.xxx:80/xxxx/xxxx/" 
  
    _es = Elasticsearch([url], http_auth=(username, password))
    if _es.ping():
        print('Connect')
        print(_es)
        res = _es.search(index='index1', body={"query": {"match_all": {}}})
        print(res)
    else:
        print('It could not connect!')

Actually, I can ping _e, and the _es will be an elastic object as:

<Elasticsearch([{'host': 'xxxx.xxx', 'url_prefix': 'xxxx/xxxx/', 'port': 80}])>

Also, to verify my URL, port and prefix, I checked it in Postman, I can get the data in a Json format correctly. But when I run the code in Python, I recieved the following error:

Connect
<Elasticsearch([{'host': 'xxxx.xxx', 'url_prefix': 'xxxx/xxxx/', 'port': 80}])>
Traceback (most recent call last):
  File "/----.py", line 29, in <module>
    res = _es.search(index='index1', body={"query": {"match_all": {}}})
  File "/usr/local/lib/python3.5/dist-packages/elasticsearch/client/utils.py", line 139, in _wrapped
    return func(*args, params=params, headers=headers, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/elasticsearch/client/__init__.py", line 1484, in search
    body=body,
  File "/usr/local/lib/python3.5/dist-packages/elasticsearch/transport.py", line 352, in perform_request
    timeout=timeout,
  File "/usr/local/lib/python3.5/dist-packages/elasticsearch/connection/http_urllib3.py", line 256, in perform_request
    self._raise_error(response.status, raw_data)
  File "/usr/local/lib/python3.5/dist-packages/elasticsearch/connection/base.py", line 288, in _raise_error
    status_code, error_message, additional_info
elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"code":404,"message":"HTTP 404 Not Found"}')

Any Idea?

Upvotes: 5

Views: 15155

Answers (3)

Natacha
Natacha

Reputation: 1250

In my case, the issue was solved using the port 9200 instead of the one in kibana's link.

Upvotes: 2

EMAI
EMAI

Reputation: 744

plz use try except and elasticsearch exceptions, as below:

from elasticsearch import Elasticsearch, exceptions

es = Elasticsearch()

try:
    res2 = es.delete(index='my_index', id='not exist id')
except exceptions.NotFoundError:
    pass

Upvotes: 11

sethmlarson
sethmlarson

Reputation: 941

Have you created the index you're trying to access yet? Try indexing a document (which creates the index automatically) or creating an index manually before searching it, otherwise you'll receive an HTTP 404.

_es.index(index="index-1", body={"key": "val"})
print(_es.search(index="index-1", body={"query": {"match_all": {}}}))

<disclosure: I'm maintainer of the Elasticsearch Python client and employed by Elastic>

Upvotes: 0

Related Questions