Skip to content

Commit

Permalink
Changed: added functions for handling the index (#120)
Browse files Browse the repository at this point in the history
## What
added functions for get/set the settings of an index
added function to force a merge on opensearch (reindex)

## Why
Needed for better bulk update speed
  • Loading branch information
HollererJ authored Oct 22, 2024
2 parents c5c3185 + 502c605 commit a76915e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
83 changes: 83 additions & 0 deletions pkg/openSearch/openSearchClient/indexFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"net/http"
"sort"
"strings"

"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -266,3 +267,85 @@ 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
}
defer searchResp.Body.Close()

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
}
16 changes: 16 additions & 0 deletions pkg/openSearch/openSearchClient/indexFunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package openSearchClient

import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pkg/openSearch/openSearchQuery/boolQueryBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func TestFilterQueryOperatorOrMultiValue(t *testing.T) {
},
})

//then
// then
require.NoError(t, err)
json, err := query.toJson()
require.NoError(t, err)
Expand Down

0 comments on commit a76915e

Please sign in to comment.