尝试使用最近创建的elasticsearch索引时的TransportError(503,u'')

时间:2015-05-06 16:18:35

标签: python elasticsearch

我正在使用Python API创建一个Elasticsearch索引,如下所示:

from elasticsearch import Elasticsearch

es = Elasticsearch()

index_body = {"mappings": {".percolator": {"properties": {"message": {"type": "string", "analyzer": "english"}}}}}
# Creates the index if it doesn't exist
if not es.indices.exists('test'):
    es.indices.create(index='test', body=index_body)

print es.exists(index='test', id='1')

索引已成功创建,但当我检查索引中是否存在文档时,它会失败并显示以下错误:

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    print es.exists(index='test', id='1')
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 68, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 282, in exists
    self.transport.perform_request('HEAD', _make_path(index, doc_type, id), params=params)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 307, 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 86, in perform_request
    self._raise_error(response.status, raw_data)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py", line 102, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.TransportError: TransportError(503, u'')

如果我第二次运行此脚本,并且已经创建了索引,那么它的工作正常。 有没有人知道会出现什么问题?

2 个答案:

答案 0 :(得分:1)

创建新索引时,需要等到分配完所有分片。

我知道如何做的最好方法是:

  1. 获取scrollPane.setViewportView( table ); //frame.getContentPane().add(table,BorderLayout.CENTER); //frame.revalidate(); //frame.repaint();
  2. 遍历所有<your_index>/_status并验证indices.<your_index>.shards无处不在
  3. 转到1)除非所有分片都已启动
  4. Here's a (PHP) project为单元测试做了这个:

    routing.state = STARTED

    错误回答:

    在继续之前,您已经为ES 提供了一些空间。 ES 接近实时,因此可以预期延迟。特别是当您按顺序依次运行代码时,无需延迟。

    我认为您只需致电_refresh endpoint即可获得保障。

    在实践中,我在单元测试中也是如此。它们执行速度非常快,创建/抽取数据/销毁索引需要花费时间,因此在我protected function _waitForAllocation(Index $index) { do { $settings = $index->getStatus()->get(); $allocated = true; foreach ($settings['shards'] as $shard) { if ($shard[0]['routing']['state'] != 'STARTED') { $allocated = false; } } } while (!$allocated); } 之前调用setUp()之前,我需要切换到相应的_refresh方法。在某些测试中,我将数据编入索引,我还必须进行test*()次调用。

    通常,在正常操作期间,您不应该打电话给它。请注意,默认_refreshrefresh_interval。如果您定期更新索引并希望反映亚秒更新(我正在谈论例如1s),那就是您需要开始的地方。

答案 1 :(得分:0)

您可以像这样添加参数wait_for_active_shards(默认为1):

es.indices.create(index ='test',body = index_body,wait_for_active_shards = all)

另请参见:[{https://elasticsearch-py.readthedocs.io/en/master/api.html],create(** kwargs)