我正在研究使用 ElasticSearch 数据库来存储我从网上提取的数据。但是,当我尝试为数据库中的数据编制索引时,我收到错误消息。
这是我创建和索引数据的代码:
es = Elasticsearch()
es.index(index='weather', doc_type='data', body=doc)
但是当我运行这个程序时,第二行会导致错误,这里是完整的回溯:
Traceback (most recent call last):
File "weatherScraper.py", line 79, in <module>
main()
File "weatherScraper.py", line 73, in main
es.index(index='weather', doc_type='data', body=doc)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
return func(*args, params=params, **kwargs)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 298, in index
_make_path(index, doc_type, id), params=params, body=body)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/transport.py", line 312, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 128, in perform_request
self._raise_error(response.status, raw_data)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.AuthenticationException: TransportError(401, u'security_exception', u'missing authentication token for REST request [/weather/data]')
请您参考如下方法:
''缺少身份验证 token '意味着您需要先进行身份验证才能与此 Elasticsearch 实例通信。要索引文档,用户必须具有写入权限。您可以在 URL 中包含用户名和密码,如下所示:http://user:password@hostname:port
例如,在 shell 中:
export ES_ENDPOINT="http://usernameWithWriteAccess:password@localhost:9200"
然后在 python 中:
es = Elasticsearch(os.environ['ES_ENDPOINT'])
