【问题标题】:Python ElasticSearch DSL Truncating ResultsPython ElasticSearch DSL 截断结果
【发布时间】:2016-02-09 03:46:55
【问题描述】:

我正在尝试在 python 中使用 elasticsearch dsl 库,但我似乎得到了某种截断的结果/元数据,而不是文档/响应对象。下面的净化查询:

from elasticsearch_dsl import Search, Q, F
from elasticsearch_dsl.query import MultiMatch
from elasticsearch import Elasticsearch
import requests
import json  

client = Elasticsearch("")

s = Search().using(client).query("match", id="1") 
response = s.execute()
test_response = response.json()

这给出了这个错误:

AttributeError: 'Response' object has no attribute 'json'

我希望使用较新的 DSL 来减少冗长。有没有人遇到过这样的事情或者有什么提示。非常感谢!

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    execute() 的结果已经是解析后的 JSON(结构与来自 HTTP API 的响应相同):

    print(response.hits.total)
    

    您还可以按如下方式迭代结果 (documentation):

    for h in response:
       print(h.title, h.body)
    

    【讨论】:

    • 有趣。您是否有任何预感,为什么我会看到以下内容,然后是 "u'request_attributes': {},...}" ?由于某种原因,我的结果似乎被截断了。我已经通过登录 plugin/head 来验证它们。
    • 您确定要按照建议为您的 Elasticsearch 版本 (elasticsearch-dsl.readthedocs.org/en/latest/…) 选择正确的库版本吗?
    • 嗯,这可能是问题所在。我在启动指南中看到了详细信息。当我最初通过 pip 安装 elasticsearch-dsl 时,它将 elasticsearch 从 2.2.0 降级到 1.9.0,我没有注意到。由于我目前只使用 jupyter 笔记本,因此我必须找到 setup.py/requirements.txt 需要的解决方法。
    • 我做了以下操作: pip install git+git://github.com/elastic/elasticsearch-dsl-py.git 这似乎解决了我的版本冲突“成功安装了elasticsearch-2.2.0 elasticsearch -dsl-2.0.0.dev0" 不幸的是,问题似乎仍然存在。
    • @Pylander 你有什么Elasticsearch("<here>")?您是直接连接到 Elasticsearch 还是通过某些代理连接到 Elasticsearch?
    猜你喜欢
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2015-04-27
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多