Elasticsearch-dsl-py: Search returning Document

Created on 20 Feb 2019  路  8Comments  路  Source: elastic/elasticsearch-dsl-py

Given a Doc that is a Document, I expected Doc.search()... to return Docs rather than generic Hits, but this doesn't seem to be the case. My goal is to dump a list of returned documents as JSON, and I would like to filter empty fields, just like doc.to_dict().

Is there a way to get the same behavior from Hit.to_dict() as Document.to_dict()? Is there a way to convert search responses into documents? Is there a more natural way to return search results as JSON, while filtering empty fields (including in nested documents)?

(This may all be related to #926. It's possible that I've misconfigured doc_type.)

Most helpful comment

@hongyuan1306 definitely, have a look at the alias_migration example here - https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/alias_migration.py#L38-L42

All 8 comments

That should work by default and all tests confirm that (0), could you please share the code you are using that leads to this behavior? Thanks!

0 - https://github.com/elastic/elasticsearch-dsl-py/blob/master/test_elasticsearch_dsl/test_integration/test_document.py#L352-L358

Closing this as there is no additional information. Please feel free to reopen with additional information.

Hi, @HonzaKral

Recently I stumbled upon the same problem and after some trials found out that once an index is reindexed and then aliased with the original name, as the cluster upgrade assistant often does, the Doc.search() won't return the result as Document types any more but rather generic Hit object.

Is there any way to work around this?

@hongyuan1306 definitely, have a look at the alias_migration example here - https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/alias_migration.py#L38-L42

That's exactly what I am looking for. Thanks a lot.

s.scan() returns Hits

I remember s.execute() gives Documents

Use doc_type argument for Search, eg:
Search(doc_type=[Doc]).using ....
and Doc is subclass of Document class

@hongyuan1306 definitely, have a look at the alias_migration example here - https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/alias_migration.py#L38-L42

For the record (the script was edited since that reply), the magic happens in BlogPost._matches https://github.com/elastic/elasticsearch-dsl-py/blob/58c02732658018333cfd5fa2f7b70d942773b0be/examples/alias_migration.py#L60

It is called from elasticsearch_dsl.search.Request._get_result
https://github.com/elastic/elasticsearch-dsl-py/blob/58c02732658018333cfd5fa2f7b70d942773b0be/elasticsearch_dsl/search.py#L235

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beanaroo picture beanaroo  路  4Comments

gabrielpjordao picture gabrielpjordao  路  3Comments

primoz-k picture primoz-k  路  4Comments

rokcarl picture rokcarl  路  4Comments

mortada picture mortada  路  3Comments