使用python pyramid和ElastiSearch。我看过pythonelasticsearch-dsl,它提供了一个很好的对象关系模型,但我不确定如何将它与金字塔集成。
到目前为止,我按照pythonelasticsearch-dsl建立了一个“全局连接”,并通过一个属性将该连接暴露到金字塔的请求中。
你看到这段代码有什么问题吗?!
from elasticsearch_dsl import connections
def _create_es_connection(config):
registry = config.registry
settings = registry.settings
es_servers = settings.get('elasticsearch.' + 'servers', ['localhost:9200'])
es_timeout = settings.get('elasticsearch.' + 'timeout', 20)
registry.es_connection = connections.create_connection(
hosts=es_servers,
timeout=es_timeout)
def get_es_connection(request):
return getattr(request.registry, 'es_connection',
connections.get_connection())
# main
def main(global_config, **settings):
...
config = Configurator(settings=settings)
config.add_request_method(
get_es_connection,
'es',
reify=True)我将该连接用作
#view
request.es ...如果有任何其他方法,我将不胜感激-谢谢。
发布于 2018-04-26 16:20:15
有一些东西看起来很奇怪,但我猜它来自于项目的复制/粘贴(设置中缺少类型转换,连接未定义等)。
您正在尝试做的事情与您使用SQLAlchemy所做的非常相似:https://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/database/sqlalchemy.html
但是根据pythonelasticsearch-dsl的文档,您甚至不必为所有这些而烦恼,因为库允许您定义一个全局默认连接:https://elasticsearch-dsl.readthedocs.io/en/latest/configuration.html#default-connection
https://stackoverflow.com/questions/49943280
复制相似问题