@@ -84,13 +84,26 @@ class ESResultFormatter(ResultFormatter):
8484 class _Hits (Hits ):
8585 def __init__ (self , * args , ** kwargs ):
8686 super ().__init__ (* args , ** kwargs )
87+ # Check if this is an error response from Elasticsearch
88+ if "error" in self .data :
89+ logger .error ("ES returned error response: %s" , self .data )
90+ raise ValueError ("Invalid response format" )
91+
8792 # make sure the document is coming from
8893 # elasticsearch at initialization time
89- assert "hits" in self .data
90- assert "total" in self .data ["hits" ]
91- assert "hits" in self .data ["hits" ]
94+ if "hits" not in self .data :
95+ logger .error ("ES response missing 'hits' field. Response data: %s" , self .data )
96+ raise ValueError ("Response missing 'hits' field" )
97+ if "total" not in self .data ["hits" ]:
98+ logger .error ("ES response missing 'hits.total' field. Response data: %s" , self .data )
99+ raise ValueError ("Response missing 'hits.total' field" )
100+ if "hits" not in self .data ["hits" ]:
101+ logger .error ("ES response missing 'hits.hits' field. Response data: %s" , self .data )
102+ raise ValueError ("Response missing 'hits.hits' field" )
92103 for hit in self .data ["hits" ]["hits" ]:
93- assert "_source" in hit
104+ if "_source" not in hit :
105+ logger .error ("ES hit missing '_source' field. Hit data: %s" , hit )
106+ raise ValueError ("Hit missing '_source' field" )
94107
95108 class _Doc (Doc ):
96109 pass
0 commit comments