I am working on using an ElasticSearch database to store data I am pulling from online. However, when I try to index the data in the database I receive an error.
Here is my code for creating and indexing the data:
JavaScript
x
4
1
es = Elasticsearch()
2
3
es.index(index='weather', doc_type='data', body=doc)
4
However when I run this program, the second of those lines causes an error, here is the complete traceback:
JavaScript
1
17
17
1
Traceback (most recent call last):
2
File "weatherScraper.py", line 79, in <module>
3
main()
4
File "weatherScraper.py", line 73, in main
5
es.index(index='weather', doc_type='data', body=doc)
6
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
7
return func(*args, params=params, **kwargs)
8
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 298, in index
9
_make_path(index, doc_type, id), params=params, body=body)
10
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/transport.py", line 312, in perform_request
11
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
12
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 128, in perform_request
13
self._raise_error(response.status, raw_data)
14
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
15
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
16
elasticsearch.exceptions.AuthenticationException: TransportError(401, u'security_exception', u'missing authentication token for REST request [/weather/data]')
17
Advertisement
Answer
”missing authentication token’ means you need to authenticate before you can talk to this Elasticsearch instance. To index documents, the user must have write access. You can include a username and password in a URL like this: http://user:password@hostname:port
For example, in a shell:
JavaScript
1
2
1
export ES_ENDPOINT="http://usernameWithWriteAccess:password@localhost:9200"
2
Then in python:
JavaScript
1
2
1
es = Elasticsearch(os.environ['ES_ENDPOINT'])
2