Skip to content

Commit 3e4dd8c

Browse files
committed
plugins/ism: add refresh_search_analyzers action
Signed-off-by: Stefano Arlandini <[email protected]>
1 parent a71fcf0 commit 3e4dd8c

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77
### Added
88
- Adds `InnerHits` field to `SearchResp` ([#672](https://github.com/opensearch-project/opensearch-go/pull/672))
99
- Adds `FilterPath` param ([#673](https://github.com/opensearch-project/opensearch-go/pull/673))
10+
- Adds the action to reload the search analyzers to the ISM plugin ([#686](https://github.com/opensearch-project/opensearch-go/pull/686))
1011

1112
### Changed
1213
### Deprecated
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// The OpenSearch Contributors require contributions made to
4+
// this file be licensed under the Apache-2.0 license or a
5+
// compatible open source license.
6+
7+
package ism
8+
9+
import "strings"
10+
11+
// ReloadSearchAnalyzersParams represents possible parameters for the RefreshSearchAnalyzersReq
12+
type ReloadSearchAnalyzersParams struct {
13+
Pretty bool
14+
Human bool
15+
ErrorTrace bool
16+
FilterPath []string
17+
}
18+
19+
func (r ReloadSearchAnalyzersParams) get() map[string]string {
20+
params := make(map[string]string)
21+
22+
if r.Pretty {
23+
params["pretty"] = "true"
24+
}
25+
26+
if r.Human {
27+
params["human"] = "true"
28+
}
29+
30+
if r.ErrorTrace {
31+
params["error_trace"] = "true"
32+
}
33+
34+
if len(r.FilterPath) > 0 {
35+
params["filter_path"] = strings.Join(r.FilterPath, ",")
36+
}
37+
38+
return params
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// The OpenSearch Contributors require contributions made to
4+
// this file be licensed under the Apache-2.0 license or a
5+
// compatible open source license.
6+
7+
package ism
8+
9+
import (
10+
"context"
11+
"net/http"
12+
"strings"
13+
14+
"github.com/opensearch-project/opensearch-go/v4"
15+
)
16+
17+
// RefreshSearchAnalyzers executes a request to refresh search analyzers with the required RefreshSearchAnalyzersReq
18+
func (c Client) RefreshSearchAnalyzers(ctx context.Context, req RefreshSearchAnalyzersReq) (RefreshSearchAnalyzersResp, error) {
19+
var (
20+
data RefreshSearchAnalyzersResp
21+
err error
22+
)
23+
if data.response, err = c.do(ctx, req, &data); err != nil {
24+
return data, err
25+
}
26+
27+
return data, nil
28+
}
29+
30+
// RefreshSearchAnalyzersReq represents possible options for the /_plugins/_refresh_search_analyzers/<indices> request
31+
type RefreshSearchAnalyzersReq struct {
32+
Indices []string
33+
34+
Header http.Header
35+
Params ReloadSearchAnalyzersParams
36+
}
37+
38+
// GetRequest returns the *http.Request that gets executed by the client
39+
func (r RefreshSearchAnalyzersReq) GetRequest() (*http.Request, error) {
40+
indices := strings.Join(r.Indices, ",")
41+
42+
var path strings.Builder
43+
path.Grow(len("/_plugins/_refresh_search_analyzers/") + len(indices))
44+
path.WriteString("_plugins/_refresh_search_analyzers")
45+
if len(r.Indices) > 0 {
46+
path.WriteString("/")
47+
path.WriteString(indices)
48+
}
49+
50+
return opensearch.BuildRequest(
51+
http.MethodPost,
52+
path.String(),
53+
nil,
54+
r.Params.get(),
55+
r.Header,
56+
)
57+
}
58+
59+
// RefreshSearchAnalyzersResp represents the returned struct of the refreshed search analyzers response
60+
type RefreshSearchAnalyzersResp struct {
61+
Shards RefreshSearchAnalyzersShards `json:"_shards"`
62+
SuccessfulRefreshDetails []RefreshSearchAnalyzersSuccessfulRefreshDetails `json:"successful_refresh_details"`
63+
response *opensearch.Response
64+
}
65+
66+
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
67+
func (r RefreshSearchAnalyzersResp) Inspect() Inspect {
68+
return Inspect{Response: r.response}
69+
}
70+
71+
// RefreshSearchAnalyzersShards is a subtype of RefreshSearchAnalyzersResp representing information about the updated shards
72+
type RefreshSearchAnalyzersShards struct {
73+
Total int `json:"total"`
74+
Successful int `json:"successful"`
75+
Failed int `json:"failed"`
76+
}
77+
78+
// RefreshSearchAnalyzersSuccessfulRefreshDetails is a subtype of RefreshSearchAnalyzersResp representing information about the analyzers
79+
type RefreshSearchAnalyzersSuccessfulRefreshDetails struct {
80+
Index string `json:"index"`
81+
RefreshedAnalyzers []string `json:"refreshed_analyzers"`
82+
}

plugins/ism/api_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,23 @@ func TestClient(t *testing.T) {
209209
},
210210
},
211211
},
212+
{
213+
Name: "ReloadSearchAnalyzers",
214+
Tests: []clientTests{
215+
{
216+
Name: "with request",
217+
Results: func() (osismtest.Response, error) {
218+
return client.RefreshSearchAnalyzers(nil, ism.RefreshSearchAnalyzersReq{Indices: testIndex})
219+
},
220+
},
221+
{
222+
Name: "inspect",
223+
Results: func() (osismtest.Response, error) {
224+
return failingClient.RefreshSearchAnalyzers(nil, ism.RefreshSearchAnalyzersReq{Indices: []string{"*"}})
225+
},
226+
},
227+
},
228+
},
212229
}
213230
for _, value := range testCases {
214231
t.Run(value.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)