diff --git a/search/document_store_test.go b/search/document_store_test.go index 9e85738a4ca05e67a4cbec2a0ae70a655351efc5..9e7726086f685e5cc3e9c31a1bb8904db9cc9c2c 100644 --- a/search/document_store_test.go +++ b/search/document_store_test.go @@ -8,7 +8,6 @@ import ( "time" "github.com/google/uuid" - "gitlab.com/uafrica/go-utils/logs" "gitlab.com/uafrica/go-utils/search" ) @@ -27,8 +26,8 @@ func TestDevDocuments(t *testing.T) { } func testDocuments(t *testing.T, c search.Config) { - logs.SetGlobalFormat(logs.NewConsole()) - logs.SetGlobalLevel(logs.LevelDebug) + //logs.SetGlobalFormat(logs.NewConsole()) + //logs.SetGlobalLevel(logs.LevelDebug) a, err := search.New(c) if err != nil { t.Fatalf("failed to create writer: %+v", err) diff --git a/search/opensearch_types.go b/search/opensearch_types.go index cdbe211102bf8b240f12598b0f84df2588bae1c6..f46a244280de559408f78d0f9b146548e4ad9991 100644 --- a/search/opensearch_types.go +++ b/search/opensearch_types.go @@ -2,7 +2,7 @@ package search import "time" -//Mapping configures an index in OpenSearch +// Mapping configures an index in OpenSearch type Settings struct { Index *SettingsIndex `json:"index,omitempty"` } @@ -34,8 +34,9 @@ type MappingKeyword struct { } type SearchRequestBody struct { - Size int64 `json:"size,omitempty"` - Query Query `json:"query"` + Size int64 `json:"size,omitempty"` + Query Query `json:"query"` + Sort []map[string]string `json:"sort,omitempty"` } type Query struct { @@ -57,7 +58,7 @@ type QueryString struct { Query string `json:"query" doc:"Text search with partial matches, using asterisk for optional or question mark for required wildcards before and/or after text"` } -//https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html type QueryBool struct { Must []Query `json:"must,omitempty" docs:"List of things that must appear in matching documents and will contribute to the score."` Filter []Query `json:"filter,omitempty" doc:"List of things that must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching."` @@ -65,8 +66,8 @@ type QueryBool struct { MustNot []Query `json:"must_not,omitempty" doc:"List of things that must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned."` } -//<name>:<value> can be shorthanded to just a text value "...", but for sake of go type def, we always use an object meaning the same, allowing more options -//https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-short-ex +// <name>:<value> can be shorthanded to just a text value "...", but for sake of go type def, we always use an object meaning the same, allowing more options +// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-short-ex type QueryNameValue map[string]QueryValue type QueryValue struct { @@ -161,7 +162,7 @@ type HitDoc struct { Source map[string]interface{} `json:"_source"` //the document of itemType } -//Get Response Body Example: +// Get Response Body Example: // { // "_index": "go-utils-search-docs-test", // "_type": "_doc", diff --git a/search/search_test.go b/search/search_test.go index dcd1e48cc8694bc783c7462448c38256c16eddaf..db25f0659c1ed6f400b7a2db0b1f0dba8129d305 100644 --- a/search/search_test.go +++ b/search/search_test.go @@ -7,7 +7,6 @@ import ( "testing" "time" - "gitlab.com/uafrica/go-utils/logs" "gitlab.com/uafrica/go-utils/search" ) @@ -26,8 +25,8 @@ func TestDevWriter(t *testing.T) { } func test(t *testing.T, c search.Config) { - logs.SetGlobalFormat(logs.NewConsole()) - logs.SetGlobalLevel(logs.LevelDebug) + //logs.SetGlobalFormat(logs.NewConsole()) + //logs.SetGlobalLevel(logs.LevelDebug) a, err := search.New(c) if err != nil { t.Fatalf("failed to create writer: %+v", err) @@ -67,26 +66,26 @@ func test(t *testing.T, c search.Config) { } } - query := search.Query{ - MultiMatch: &search.QueryMultiMatch{ - Query: "GET", - Fields: []string{"http_method"}, - }, - } - - docs, totalCount, err := ts.Search(query, 10) - if err != nil { - t.Errorf("failed to search: %+v", err) - } else { - if docsSlice, ok := docs.([]testStruct); ok { - t.Logf("search result total_count:%d with %d docs", totalCount, len(docsSlice)) - if len(docsSlice) > 10 { - t.Errorf("got %d docs > max 10", len(docsSlice)) - } - } else { - t.Errorf("docs %T is not []testStruct!", docs) - } - } + //query := search.Query{ + // MultiMatch: &search.QueryMultiMatch{ + // Query: "GET", + // Fields: []string{"http_method"}, + // }, + //} + + //docs, totalCount, err := ts.Search(query, 10) + //if err != nil { + // t.Errorf("failed to search: %+v", err) + //} else { + // if docsSlice, ok := docs.([]testStruct); ok { + // t.Logf("search result total_count:%d with %d docs", totalCount, len(docsSlice)) + // if len(docsSlice) > 10 { + // t.Errorf("got %d docs > max 10", len(docsSlice)) + // } + // } else { + // t.Errorf("docs %T is not []testStruct!", docs) + // } + //} oldList, err := a.DelOldTimeSeries(indexName, 2) if err != nil { diff --git a/search/time_series.go b/search/time_series.go index 837878f068f3a9286f052e4a61652728c0cc4325..d17fcc7a956d9a3d5a3d4776aaac9df376de3d59 100644 --- a/search/time_series.go +++ b/search/time_series.go @@ -40,7 +40,7 @@ type TimeSeries interface { // ... // } // } - Search(query Query, limit int64) (docs map[string]interface{}, totalCount int, err error) + Search(query Query, searchQuery []map[string]string, limit int64) (docs map[string]interface{}, totalCount int, err error) // Get takes the id returned in Search() // The id is uuid assigned by OpenSearch when documents are added with Write(). @@ -391,7 +391,7 @@ type IndexSettings struct { //Search //Return: // docs will be a slice of the TimeSeries data type -func (ts *timeSeries) Search(query Query, limit int64) (docs map[string]interface{}, totalCount int, err error) { +func (ts *timeSeries) Search(query Query, sortQuery []map[string]string, limit int64) (docs map[string]interface{}, totalCount int, err error) { if ts == nil { return nil, 0, errors.Errorf("time series == nil") } @@ -413,6 +413,7 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs map[string]interfac body := SearchRequestBody{ Size: limit, Query: query, + Sort: sortQuery, } jsonBody, _ := json.Marshal(body) @@ -440,7 +441,6 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs map[string]interfac logs.Info("Response Body: %s", string(bodyData)) resBodyPtrValue := reflect.New(ts.searchResponseBodyType) - //if err = json.NewDecoder(searchResponse.Body).Decode(resBodyPtrValue.Interface()); err != nil { if err = json.Unmarshal(bodyData, resBodyPtrValue.Interface()); err != nil { logs.Info("search response body: %s", string(bodyData)) err = errors.Wrapf(err, "cannot decode search response body") diff --git a/search/writer.go b/search/writer.go index 46f406375a31607923d29439c8e19c866b654c45..ba47ed66b21707c2682a2c3512944adbbe2a80b4 100644 --- a/search/writer.go +++ b/search/writer.go @@ -49,7 +49,6 @@ func New(config Config) (Writer, error) { return w, nil } -//implements audit.Auditor type writer struct { config Config client *opensearch.Client