From 4a7a3f9e2a2a40320b6cde4b0e00bb4bf9a2781e Mon Sep 17 00:00:00 2001 From: Andreas Christou Date: Mon, 19 May 2025 18:08:38 +0100 Subject: [PATCH] ElasticSearch: Fallback parse total hits as int (#105615) * Attempt int parsing * Update total hits logic - Do not allow the error to fail the request - Add some comments * Mark log as debug --- pkg/tsdb/elasticsearch/client/client.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/tsdb/elasticsearch/client/client.go b/pkg/tsdb/elasticsearch/client/client.go index b9ddc051469..9b5cf6dc54b 100644 --- a/pkg/tsdb/elasticsearch/client/client.go +++ b/pkg/tsdb/elasticsearch/client/client.go @@ -353,7 +353,18 @@ func processHits(dec *json.Decoder, sr *SearchResponse) error { var total *SearchResponseHitsTotal err := dec.Decode(&total) if err != nil { - return err + // It's possible that the user is using an older version of Elasticsearch (or one that doesn't return what is expected) + // Attempt to parse the total value as an integer in this case + totalInt := 0 + err = dec.Decode(&totalInt) + if err == nil { + total = &SearchResponseHitsTotal{ + Value: totalInt, + } + } else { + // Log the error but do not fail the query + backend.Logger.Debug("failed to decode total hits", "error", err) + } } sr.Hits.Total = total default: