From e4bdf5f7df7d5942badfe911366d551ad9e4da53 Mon Sep 17 00:00:00 2001 From: HollererJ Date: Tue, 22 Oct 2024 13:14:22 +0200 Subject: [PATCH 1/2] Changed: added functions for handling the index --- .../openSearchClient/indexFunctions.go | 81 +++++++++++++++++++ .../openSearchClient/indexFunctions_test.go | 16 ++++ .../openSearchQuery/boolQueryBuilder_test.go | 2 +- 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/pkg/openSearch/openSearchClient/indexFunctions.go b/pkg/openSearch/openSearchClient/indexFunctions.go index 3de7a1f..f6ff65b 100644 --- a/pkg/openSearch/openSearchClient/indexFunctions.go +++ b/pkg/openSearch/openSearchClient/indexFunctions.go @@ -12,6 +12,7 @@ import ( "io" "net/http" "sort" + "strings" "github.com/opensearch-project/opensearch-go/v4/opensearchapi" "github.com/rs/zerolog/log" @@ -266,3 +267,83 @@ func (i *IndexFunction) createIndexRemovalActions(indexesToRemove []string, alia } return wrappedActions } + +func (i *IndexFunction) RefreshIndex(index string) error { + log.Debug().Msgf("Start refreshing index: %s", index) + ctx := context.Background() + refreshResp, err := i.openSearchProjectClient.Indices.Refresh( + ctx, + &opensearchapi.IndicesRefreshReq{ + Indices: []string{index}, + }, + ) + if err != nil { + return err + } + log.Debug().Msgf("Index %s refreshed with staus code: %d", index, + refreshResp.Inspect().Response.StatusCode) + return nil +} + +func (i *IndexFunction) GetIndexSettings(index string) (map[string]interface{}, error) { + body := strings.NewReader(``) + urlString := "/" + index + "/_settings?include_defaults=true" + settingsRequest, err := http.NewRequest("GET", urlString, body) + if err != nil { + return nil, err + } + settingsRequest.Header.Set("Content-Type", "application/json") + searchResp, err := i.openSearchProjectClient.Client.Perform(settingsRequest) + if err != nil { + return nil, err + } + searchRespBody, err := io.ReadAll(searchResp.Body) + if err != nil { + return nil, err + } + var settings map[string]interface{} + if err := json.Unmarshal(searchRespBody, &settings); err != nil { + return nil, err + } + + // Log and return the settings for inspection + log.Trace().Msgf("Retrieved settings for index %s: %+v", index, settings) + return settings, nil +} + +func (i *IndexFunction) SetIndexSettings(index string, settingsBody io.Reader) error { + ctx := context.Background() + + // Apply the settings to the index + settingsPutResp, err := i.openSearchProjectClient.Indices.Settings.Put( + ctx, + opensearchapi.SettingsPutReq{ + Indices: []string{index}, + Body: settingsBody, + }, + ) + if err != nil { + return err + } + + log.Debug().Msgf("Settings applied to index %s: %t", index, settingsPutResp.Acknowledged) + return nil +} + +func (i *IndexFunction) ForceMerge(index string, maximumNumberOfSegments int) error { + ctx := context.Background() + forceMergeResponse, err := i.openSearchProjectClient.Indices.Forcemerge( + ctx, + &opensearchapi.IndicesForcemergeReq{ + Indices: []string{index}, + Params: opensearchapi.IndicesForcemergeParams{ + MaxNumSegments: &maximumNumberOfSegments, + }, + }, + ) + if err != nil { + return err + } + log.Debug().Msgf("Forcemerge applied to index %s: with status %+v", index, forceMergeResponse.Inspect().Response) + return nil +} diff --git a/pkg/openSearch/openSearchClient/indexFunctions_test.go b/pkg/openSearch/openSearchClient/indexFunctions_test.go index 47a0d39..50dc22d 100644 --- a/pkg/openSearch/openSearchClient/indexFunctions_test.go +++ b/pkg/openSearch/openSearchClient/indexFunctions_test.go @@ -6,6 +6,9 @@ package openSearchClient import ( "context" + "encoding/json" + "fmt" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -94,6 +97,19 @@ func TestIndexCheck(t *testing.T) { err = iFunc.CreateIndex("testindex2", []byte(testIndex)) assert.NoError(t, err) + // Now check if we can get the settings from the index + settings, err := iFunc.GetIndexSettings("testindex2") + jsonData, err := json.Marshal(settings) + assert.NoError(t, err) + assert.Contains(t, string(jsonData), "settings") + assert.Contains(t, string(jsonData), "number_of_replicas") + + // Lets write a settings back to the Index + updatedSettings := `{"index": {"number_of_replicas": 1}}` + fmt.Println(updatedSettings) + err = iFunc.SetIndexSettings("testindex2", strings.NewReader(updatedSettings)) + assert.NoError(t, err) + err = iFunc.CreateOrPutAlias("aliasName", "testindex", "testindex2") assert.NoError(t, err) diff --git a/pkg/openSearch/openSearchQuery/boolQueryBuilder_test.go b/pkg/openSearch/openSearchQuery/boolQueryBuilder_test.go index a32f36a..32d5d12 100644 --- a/pkg/openSearch/openSearchQuery/boolQueryBuilder_test.go +++ b/pkg/openSearch/openSearchQuery/boolQueryBuilder_test.go @@ -322,7 +322,7 @@ func TestFilterQueryOperatorOrMultiValue(t *testing.T) { }, }) - //then + // then require.NoError(t, err) json, err := query.toJson() require.NoError(t, err) From 502c605c519c15065070904c5ef1422e991c503b Mon Sep 17 00:00:00 2001 From: HollererJ Date: Tue, 22 Oct 2024 15:35:35 +0200 Subject: [PATCH 2/2] Update pkg/openSearch/openSearchClient/indexFunctions.go Co-authored-by: Roxana Meixner --- pkg/openSearch/openSearchClient/indexFunctions.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/openSearch/openSearchClient/indexFunctions.go b/pkg/openSearch/openSearchClient/indexFunctions.go index f6ff65b..95f3ce5 100644 --- a/pkg/openSearch/openSearchClient/indexFunctions.go +++ b/pkg/openSearch/openSearchClient/indexFunctions.go @@ -297,6 +297,8 @@ func (i *IndexFunction) GetIndexSettings(index string) (map[string]interface{}, if err != nil { return nil, err } + defer searchResp.Body.Close() + searchRespBody, err := io.ReadAll(searchResp.Body) if err != nil { return nil, err