简体   繁体   中英

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

Please find below my iPython shell statements:

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

My basic requirement was to construct a query using high level library elasticsearch-dsl-py in django project based on several inputs and filter criteria.

For being self-explainable and easy to construct code, I made up the query into dictionary(json) style like basic elasticsearch queries are, then I utilize the "from_dict(dictionary_object)" method from elasticsearch_dsl library and call count() and execute() on the result for my purposes.

The above shown is a smaller example, but the problem is clearly visible. Using the from_dict am getting double the results as to when I use the high level syntax(the answer 42 above is correct), can anyone explain why so?

I found the answer with Honza's help. The method "from_dict" is a class method and it resets the search object, thus leaving it without any index to search in and may give more results than expected as it would execute search across all possible indexes.

Instead we just have to mention the index again, something like: s.from_dict(dict_qry),using(client).index() where client is, from elasticsearch import Elasticsearch client = Elasticsearch()

Read about it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM