在 Python 中测试弹性搜索连接



我在Python中运行以下代码:

es = Elasticsearch([{'host': 'localhost', 'port': 9200}]).ping()
print (es)
if es:
    r = requests.get('http://localhost:9200')
    es.indices.delete(index='test_twitter', ignore=[400, 404])
    connected = True
    print('Connected to ES..')
else:
    print('Not connected to ES...')

当 Elasticsearch 运行时,ping 没有问题。问题是当 ES 未运行时,ping(( 在打印回溯错误后返回 FALSE,如下所示:

        HEAD http://localhost:9200/ [status:N/A request:2.007s]
Traceback (most recent call last):
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001E2ECBD45F8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
HEAD http://localhost:9200/ [status:N/A request:2.003s]
Traceback (most recent call last):
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001E2ECBD44E0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
HEAD http://localhost:9200/ [status:N/A request:2.016s]
Traceback (most recent call last):
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001E2ECBD4668>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
False
Not connected to ES...

即使我尝试 - 除了下面这样,它不起作用:

from elasticsearch.exceptions import ConnectionError
try:
    es = Elasticsearch([{'host': 'localhost', 'port': 9200}]).ping()
except ConnectionRefusedError:
    print ('Connection Errrorrr!')
print (es)

我希望 ping(( 返回 false 但不打印错误日志,并知道如何做到这一点?

完整的代码如下:

from __future__ import unicode_literals
import hug
from hug_middleware_cors import CORSMiddleware
import spacy
from elasticsearch import Elasticsearch
import json
import requests
import sys
sys.tracebacklimit = 0
MODELS = {
    'en_core_web_sm': spacy.load('en_core_web_sm'),
    # 'de_core_news_sm': spacy.load('de_core_news_sm'),
    # 'es_core_news_sm': spacy.load('es_core_news_sm'),
    # 'pt_core_news_sm': spacy.load('pt_core_news_sm'),
    # 'fr_core_news_sm': spacy.load('fr_core_news_sm'),
    # 'it_core_news_sm': spacy.load('it_core_news_sm'),
    # 'nl_core_news_sm': spacy.load('nl_core_news_sm')
}

def get_model_desc(nlp, model_name):
    """Get human-readable model name, language name and version."""
    lang_cls = spacy.util.get_lang_class(nlp.lang)
    lang_name = lang_cls.__name__
    model_version = nlp.meta['version']
    return '{} - {} (v{})'.format(lang_name, model_name, model_version)

@hug.get('/models')
def models():
    return {name: get_model_desc(nlp, name) for name, nlp in MODELS.items()}

@hug.post('/dep')
def dep(text: str, model: str, collapse_punctuation: bool=False,
        collapse_phrases: bool=False):
    """Get dependencies for displaCy visualizer."""
    nlp = MODELS[model]
    doc = nlp(text)
    if collapse_phrases:
        for np in list(doc.noun_chunks):
            np.merge(tag=np.root.tag_, lemma=np.root.lemma_,
                     ent_type=np.root.ent_type_)
    options = {'collapse_punct': collapse_punctuation}
    return spacy.displacy.parse_deps(doc, options)

@hug.post('/ent')
def ent(text: str, model: str):
    """Get entities for displaCy ENT visualizer."""
    nlp = MODELS[model]
    doc = nlp(text)
    for ent in doc.ents:
        if connected:
            es.index(index='test_twitter', doc_type='words', body={'tag': ent.text})
        else:
            print('text :')
            print(ent.text)
            print(ent.label_)
    # return [{'text': ent.text, 'start': ent.start_char, 'end': ent.end_char, 'label': ent.label_}
    #         for ent in doc.ents]

if __name__ == '__main__':
    try:
        es = Elasticsearch([{'host': 'localhost', 'port': 9200}]).ping()
    except ConnectionRefusedError:
        print ('Connection Errrorrr!')
    if es:
        es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
        r = requests.get('http://localhost:9200')
        es.indices.delete(index='test_twitter', ignore=[400, 404])
        connected = True
        print('Connected to ES..')
    else:
        print('Not connected to ES...')
    import waitress
    app = hug.API(__name__)
    app.http.add_middleware(CORSMiddleware(app))
    waitress.serve(__hug_wsgi__, port=8000)

"HonzaKral 于 2017 年 11 月 9 日发表评论":ping 方法故意捕获所有异常,并且仅返回 True/False 是否成功 [...]

"HonzaKral 于 2017 年 11 月 9 日评论">:回溯仅使用 python 的标准日志记录模块进行记录,要禁用它,请配置您的日志记录并为 elasticsearch 记录器设置正确的日志记录级别,快捷方式,禁用所有非错误级别的消息是:

import logging
logging.getLogger("elasticsearch").setLevel(logging.CRITICAL)

参考: https://github.com/elastic/elasticsearch-py/issues/666

由于堆栈跟踪是由 urllib3 生成的,您可以像这样禁用它:

import logging
logging.getLogger("urllib3").setLevel(logging.ERROR)

您无法避免当前情况下获得的堆栈跟踪。http_urllib3故意记录这一点。

self.log_request_fail(method, full_url, url, body, time.time() - start, exception=e)
def log_request_fail(self, method, full_url, path, body, duration, status_code=None, response=None, exception=None):
  """ Log an unsuccessful API call.  """
  # do not log 404s on HEAD requests
  if method == 'HEAD' and status_code == 404:
    return logger.warning(
      '%s %s [status:%s request:%.3fs]', method, full_url,
      status_code or 'N/A', duration, exc_info=exception is not None
    )

解决此问题的唯一方法是将日志记录级别更改为高于警告的任何级别

尝试

assert esCursor.ping(), "Elastic Search Connection establishment failed"

最新更新