我通过以下代码从elasticsearch docker检索一些数据:
def get_results(relation_predictions, entity_predictions):
request_body = {
"query": {
"bool": {
"must": [
{"match": {"description": entity_predictions}}
],
"filter": [
{"term": {"relation.keyword": relation_predictions}}
]
}
}
}
es = Elasticsearch(hosts=[{"local": '9200'}])
try:
res = es.search(index="main_index",
body=request_body,
size=1)['hits']['hits'][0]['_source']
return res
except IndexError:
d = {'subject': 'missing',
'description': entity_predictions,
'relation': relation_predictions}
return d
我用下面的代码调用上面的函数: rel_preds_2 = []
ent_preds_2 = []
for ent, rel in zip(entity_predictions, relation_predictions):
rel_preds_2.append(entity_linking.get_results(rel, ent)['relation'])
ent_preds_2.append(entity_linking.get_results(rel, ent)['subject'])
列表的entity_predictions和relationship_predictions都具有22000的长度。但是在迭代过程中,我遇到了错误。我有时会遇到错误,这会中断我的流程elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x000001CC54065B88>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x000001CC54065B88>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted)
您是否知道为什么会发生,为什么有时会发生但并非总是如此,我如何解决它并使我的过程可靠?提前致谢。
最佳答案
我通过添加time.sleep(0.1)
解决了
rel_preds_2 = []
ent_preds_2 = []
for ent, rel in zip(entity_predictions, relation_predictions):
rel_preds_2.append(entity_linking.get_results(rel, ent)['relation'])
ent_preds_2.append(entity_linking.get_results(rel, ent)['subject'])
time.sleep(0.1)
关于python - 使用Elastichsearch Python客户端时出现ConnectionError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63054225/