簡體   English   中英

帶有Elasticsearch DSL的Python金字塔(檐口)

[英]Python Pyramid(cornice) with Elasticsearch DSL

使用python pyramid和ElastiSearch。 我看了pythonelasticsearch-dsl ,它提供了一個不錯的ORM,但我不確定如何將其與金字塔集成。

到目前為止,我按照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 ...

如果還有其他方法,我將不勝感激。-謝謝。

有些事情看起來很奇怪,但我想它來自您項目中的復制/粘貼(缺少在設置中強制轉換的類型,未定義的連接等)。

您嘗試執行的操作與使用SQLAlchemy執行的操作非常相似: https : //docs.pylonsproject.org/projects/pyramid_cookbook/zh-CN/latest/database/sqlalchemy.html

但是根據pythonelasticsearch-dsl的文檔,您甚至都不必費心,因為lib允許您定義全局默認連接: https : //elasticsearch-dsl.readthedocs.io/en/latest/configuration.html #default-connection

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM