我正在尝试为Python Elasticsearch运行'example usage'脚本:
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])
es.indices.refresh(index="test-index")
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
但是,我收到以下错误:
Traceback (most recent call last):
File "elasticsearch_example_usage.py", line 10, in <module>
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 279, in index
_make_path(index, doc_type, id), params=params, body=body)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 327, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py", line 105, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f0be9c14890>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f0be9c14890>: Failed to establish a new connection: [Errno 111] Connection refused)
有人可以澄清为什么这不起作用?我需要做什么额外的命令才能“设置”Elasticsearch吗?引用的网站不提供任何额外的指示或澄清。
答案 0 :(得分:2)
Python Elasticsearch客户端只是其中的一部分,需要与Elasticsearch服务器配合使用。当你这样称呼时:
es = Elasticsearch()
您正在设置与Elasticsearch主机的客户端连接。不带参数调用它会使用default values,尝试在localhost上命中端口9200。
要设置Elasticsearch服务器,最有用的方法是点击Elasticsearch site并查看有关本地或云端运行的文档(无论哪种方式对您有吸引力)。遵循download说明将使您在localhost端口9200上运行Elasticsearch,这将使您的示例正常工作。