From f41e4cfe45dccb3bdd7b127eb7ecd081c8265e8e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?France=CC=81=20Wilke?= <francewilke@gmail.com>
Date: Mon, 13 Dec 2021 14:05:11 +0200
Subject: [PATCH] Api-logs return if response is empty

---
 search/opensearch_types.go | 6 +++---
 search/time_series.go      | 9 ++++++---
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/search/opensearch_types.go b/search/opensearch_types.go
index 673ab09..a8d1880 100644
--- a/search/opensearch_types.go
+++ b/search/opensearch_types.go
@@ -34,10 +34,10 @@ type MappingKeyword struct {
 }
 
 type SearchRequestBody struct {
-	Size  int64               `json:"size,omitempty"`
+	Sort  []map[string]string `json:"sort,omitempty"` // order and order_by
+	Size  int64               `json:"size,omitempty"` // limit
+	From  int64               `json:"from,omitempty"` // offset
 	Query Query               `json:"query"`
-	Sort  []map[string]string `json:"sort,omitempty"`
-	From  int64               `json:"from,omitempty"`
 }
 
 type Query struct {
diff --git a/search/time_series.go b/search/time_series.go
index 54d56b5..e3542d0 100644
--- a/search/time_series.go
+++ b/search/time_series.go
@@ -398,7 +398,7 @@ type IndexSettings struct {
 //Search
 //Return:
 //	docs will be a slice of the TimeSeries data type
-func (ts *timeSeries) Search(query Query, sortQuery []map[string]string, limit int64, offset int64) (docs map[string]interface{}, totalCount int, err error) {
+func (ts *timeSeries) Search(query Query, sort []map[string]string, limit int64, offset int64) (docs map[string]interface{}, totalCount int, err error) {
 	if ts == nil {
 		return nil, 0, errors.Errorf("time series == nil")
 	}
@@ -418,10 +418,10 @@ func (ts *timeSeries) Search(query Query, sortQuery []map[string]string, limit i
 	//    	}
 	//  }
 	body := SearchRequestBody{
+		Sort:  sort,
 		Size:  limit,
-		Query: query,
-		Sort:  sortQuery,
 		From:  offset,
+		Query: query,
 	}
 
 	jsonBody, _ := json.Marshal(body)
@@ -471,6 +471,9 @@ func (ts *timeSeries) Search(query Query, sortQuery []map[string]string, limit i
 	}
 
 	docs = map[string]interface{}{}
+	if !hits.IsValid() {
+		return
+	}
 	for i := 0; i < hits.Len(); i++ {
 		hit := hits.Index(i)
 		index := hit.Field(0).Interface().(string)    // HitDoc.Index
-- 
GitLab