From cfe7a39f2584b14dd81035d441ed2af435d867c2 Mon Sep 17 00:00:00 2001
From: Jan Semmelink <jan@uafrica.com>
Date: Thu, 4 Nov 2021 14:36:49 +0200
Subject: [PATCH] Update query to fix text search

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

diff --git a/search/opensearch_types.go b/search/opensearch_types.go
index b46a9ff..72c048a 100644
--- a/search/opensearch_types.go
+++ b/search/opensearch_types.go
@@ -26,6 +26,7 @@ type MappingProperty struct {
 	Enabled    bool                              `json:"enabled,omitempty"`
 	Fields     map[string]MappingFieldProperties `json:"fields,omitempty"`
 	Properties map[string]MappingProperty        `json:"properties,omitempty"`
+	//Index      bool                              `json:"index,omitempty"` //set true to make text field searchable
 }
 
 type MappingFieldProperties struct {
@@ -53,7 +54,7 @@ type Query struct {
 
 type QueryMultiMatch struct {
 	Query  string   `json:"query" doc:"Text search in below fields"`
-	Fields []string `json:"fields" doc:"List of fields"`
+	Fields []string `json:"fields,omitempty" doc:"List of fields"`
 }
 
 //https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
@@ -85,7 +86,7 @@ func QueryValueTime(t time.Time) QueryValue {
 
 type QueryRange map[string]QueryExpr
 
-type QueryExpr map[string]QueryValue //<oper>:<value> e.g. "gte":"10"
+type QueryExpr map[string]string //<oper>:<value> e.g. "gte":"10"
 
 //example of search response body:
 //	{
diff --git a/search/time_series.go b/search/time_series.go
index 46cc049..3a13720 100644
--- a/search/time_series.go
+++ b/search/time_series.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"context"
 	"encoding/json"
+	"io/ioutil"
 	"net/http"
 	"reflect"
 	"strings"
@@ -15,6 +16,8 @@ import (
 	"gitlab.com/uafrica/go-utils/reflection"
 )
 
+const TimeFormat = "2006-01-02T15:04:05+07:00"
+
 //embed this into your log struct
 type TimeSeriesHeader struct {
 	StartTime  time.Time `json:"@timestamp"`
@@ -339,7 +342,8 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs interface{}, totalC
 	switch searchResponse.StatusCode {
 	case http.StatusOK:
 	default:
-		err = errors.Errorf("Search failed with HTTP status %v", searchResponse.StatusCode)
+		resBody, _ := ioutil.ReadAll(searchResponse.Body)
+		err = errors.Errorf("Search failed with HTTP status %v: %s", searchResponse.StatusCode, string(resBody))
 		return
 	}
 
-- 
GitLab