簡體   English   中英

彈性搜索 DSL python 問題

[英]Elastic search DSL python issue

我一直在使用 ElasticSearch DSL python 包來查詢我的彈性搜索數據庫。 查詢方法非常直觀,但我在檢索文檔時遇到問題。 這是我嘗試過的:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
es = Elasticsearch(hosts=[{"host":'xyz', "port":9200}],timeout=400)
s = Search(using=es,index ="xyz-*").query("match_all")
response = s.execute()
for hit in response:
    print hit.title

我得到的錯誤:

AttributeError: 'Hit' object has no attribute 'title'

我用谷歌搜索了錯誤並找到了另一個 SO: How to access the response object using elasticsearch DSL for python

解決方案提到:

for hit in response:
     print hit.doc.firstColumnName

不幸的是,我對“doc”再次遇到了同樣的問題。 我想知道訪問我的文檔的正確方法是什么?

任何幫助將不勝感激!

我遇到了相同的問題,因為我發現了它的不同版本,但這似乎取決於您使用的 elasticsearch-dsl 庫的版本。 您可能會探索response對象,它是子對象。 例如,使用 5.3.0 版,我使用以下循環查看預期數據。

for hit in RESPONSE.hits._l_:
    print(hit)

for hit in RESPONSE.hits.hits:
    print(hit)

請注意,出於某種奇怪的原因,這些限制為 10 個數據元素。

print(len(RESPONSE.hits.hits))
10
print(len(RESPONSE.hits._l_))
10

如果我使用print('Total {} hits found.\\n'.format(RESPONSE.hits.total))命中數,這與總命中數不匹配

祝你好運!

從版本 6 開始,響應不再返回您填充的 ​​Document 類,這意味着您的字段只是一個AttrDict ,它基本上是一個字典。

要解決這個問題,您需要有一個Document類來表示要解析的文檔。 然后你需要使用.from_es()方法用你的文檔類解析命中字典。

就像我在這里回答的一樣。

https://stackoverflow.com/a/64169419/5144029

也看看這里的Document

https://elasticsearch-dsl.readthedocs.io/en/7.3.0/persistence.html

暫無
暫無

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

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