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.)
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!
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_migrationexample 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
Most helpful comment
@hongyuan1306 definitely, have a look at the
alias_migrationexample here - https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/alias_migration.py#L38-L42