簡體   English   中英

當使用 search.from_dict() 方法從字典語法構造查詢時,python 中的 elasticsearch-dsl 庫給出雙重結果

[英]elasticsearch-dsl library in python giving double results when using search.from_dict() method to construct query from dictionary syntax

請在我的 iPython shell 語句下面找到:

In [90]: s = Search(using=client, index='institutes')

In [91]: s = s.filter('match_phrase',country__uri='canada').filter("nested", path="institutecourse_set", query=Q("match_phrase", **{'institutecourse_set.stream.uri': 'sciences'}))

In [92]: di = s.to_dict()

In [93]: s1 =  Search(using=client, index='institutes')

In [94]: s1 = s1.from_dict(di)

In [95]: s1.count()

Out[95]: 84

In [96]: s.count()

Out[96]: 42

我的基本要求是在 django 項目中使用高級庫 elasticsearch-dsl-py 基於多個輸入和過濾條件構建查詢。

為了易於解釋和易於構建代碼,我將查詢組成了字典(json)樣式,就像基本的 elasticsearch 查詢一樣,然后我利用 elasticsearch_dsl 庫中的“from_dict(dictionary_object)”方法並調用 count() 和 execute( ) 關於我的目的的結果。

上面顯示的是一個較小的示例,但問題很明顯。 當我使用高級語法時,使用 from_dict 得到雙倍的結果(上面的答案 42 是正確的),誰能解釋為什么會這樣?

在 Honza 的幫助下,我找到了答案。 方法“from_dict”是一個 class 方法,它重置搜索 object,因此沒有任何索引可供搜索,並且可能會給出比預期更多的結果,因為它會在所有可能的索引中執行搜索。

相反,我們只需要再次提及索引,例如: s.from_dict(dict_qry),using(client).index() 客戶端所在的位置,從 elasticsearch import Elasticsearch client = Elasticsearch()

在這里閱讀

暫無
暫無

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

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