在Python中测试elasticsearch连接

vptzau2j  于 2023-03-22  发布在  ElasticSearch
关注(0)|答案(4)|浏览(144)

在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()在打印Traceback错误后返回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)
yptwkmov

yptwkmov1#

**“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

mqxuamgl

mqxuamgl2#

由于stacktrace是由urllib3生成的,因此可以像这样禁用它:

import logging

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

lnxxn5zx3#

在当前场景中,你无法避免堆栈跟踪。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
    )

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

n6lpvg4x

n6lpvg4x4#

试试看

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

相关问题