refactor(search): unify osu request and params naming

This commit is contained in:
fschade
2025-08-08 22:21:43 +02:00
parent 8c509263b7
commit ad866b8ce3
18 changed files with 109 additions and 109 deletions

View File

@@ -108,11 +108,11 @@ func (be *Backend) Search(ctx context.Context, sir *searchService.SearchIndexReq
Params: searchParams,
},
boolQuery,
osu.SearchReqOptions{
Highlight: &osu.HighlightOption{
osu.SearchBodyParams{
Highlight: &osu.BodyParamHighlight{
PreTags: []string{"<mark>"},
PostTags: []string{"</mark>"},
Fields: map[string]osu.HighlightOption{
Fields: map[string]osu.BodyParamHighlight{
"Content": {},
},
},
@@ -174,8 +174,8 @@ func (be *Backend) Upsert(id string, r engine.Resource) error {
}
func (be *Backend) Move(id string, parentID string, target string) error {
return be.updateSelfAndDescendants(id, func(rootResource engine.Resource) *osu.ScriptOption {
return &osu.ScriptOption{
return be.updateSelfAndDescendants(id, func(rootResource engine.Resource) *osu.BodyParamScript {
return &osu.BodyParamScript{
Source: `
if (ctx._source.ID == params.id ) { ctx._source.Name = params.newName; ctx._source.ParentID = params.parentID; }
ctx._source.Path = ctx._source.Path.replace(params.oldPath, params.newPath)
@@ -193,8 +193,8 @@ func (be *Backend) Move(id string, parentID string, target string) error {
}
func (be *Backend) Delete(id string) error {
return be.updateSelfAndDescendants(id, func(_ engine.Resource) *osu.ScriptOption {
return &osu.ScriptOption{
return be.updateSelfAndDescendants(id, func(_ engine.Resource) *osu.BodyParamScript {
return &osu.BodyParamScript{
Source: "ctx._source.Deleted = params.deleted",
Lang: "painless",
Params: map[string]any{
@@ -205,8 +205,8 @@ func (be *Backend) Delete(id string) error {
}
func (be *Backend) Restore(id string) error {
return be.updateSelfAndDescendants(id, func(_ engine.Resource) *osu.ScriptOption {
return &osu.ScriptOption{
return be.updateSelfAndDescendants(id, func(_ engine.Resource) *osu.BodyParamScript {
return &osu.BodyParamScript{
Source: "ctx._source.Deleted = params.deleted",
Lang: "painless",
Params: map[string]any{
@@ -265,7 +265,7 @@ func (be *Backend) DocCount() (uint64, error) {
return uint64(resp.Count), nil
}
func (be *Backend) updateSelfAndDescendants(id string, scriptProvider func(engine.Resource) *osu.ScriptOption) error {
func (be *Backend) updateSelfAndDescendants(id string, scriptProvider func(engine.Resource) *osu.BodyParamScript) error {
if scriptProvider == nil {
return fmt.Errorf("script cannot be nil")
}
@@ -283,7 +283,7 @@ func (be *Backend) updateSelfAndDescendants(id string, scriptProvider func(engin
},
},
osu.NewTermQuery[string]("Path").Value(resource.Path),
osu.UpdateByQueryReqOptions{
osu.UpdateByQueryBodyParams{
Script: scriptProvider(resource),
},
)

View File

@@ -39,8 +39,8 @@ func (t kqlOpensearchTranspiler) transpile(nodes []ast.Node) (osu.Builder, error
return builder, nil
}
boolQueryOptions := &osu.BoolQueryOptions{}
boolQuery := osu.NewBoolQuery().Options(boolQueryOptions)
boolQueryParams := &osu.BoolQueryParams{}
boolQuery := osu.NewBoolQuery().Params(boolQueryParams)
boolQueryAdd := boolQuery.Must
for i, node := range nodes {
nextOp := t.getOperatorValueAt(nodes, i+1)
@@ -71,7 +71,7 @@ func (t kqlOpensearchTranspiler) transpile(nodes []ast.Node) (osu.Builder, error
if nextOp == kql.BoolOR {
// if there are should clauses, we set the minimum should match to 1
boolQueryOptions.MinimumShouldMatch = 1
boolQueryParams.MinimumShouldMatch = 1
}
boolQueryAdd(builder)

View File

@@ -187,7 +187,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
},
},
Want: osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Should(
osu.NewTermQuery[string]("Name").Value("openCloud"),
osu.NewTermQuery[string]("age").Value("32"),
@@ -235,7 +235,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
},
},
Want: osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Should(
osu.NewTermQuery[string]("Name").Value("openCloud"),
osu.NewTermQuery[string]("age").Value("32"),
@@ -254,7 +254,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
},
},
Want: osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Must(
osu.NewTermQuery[string]("a").Value("a"),
).
@@ -275,7 +275,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
},
},
Want: osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Must(
osu.NewTermQuery[string]("b").Value("b"),
osu.NewTermQuery[string]("c").Value("c"),
@@ -296,7 +296,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
},
},
Want: osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Should(
osu.NewTermQuery[string]("a").Value("a"),
).
@@ -323,7 +323,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
Want: osu.NewBoolQuery().
Must(
osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Should(
osu.NewTermQuery[string]("a").Value("a"),
osu.NewTermQuery[string]("b").Value("b"),
@@ -351,7 +351,7 @@ func TestTranspileKQLToOpenSearch(t *testing.T) {
Want: osu.NewBoolQuery().
Must(
osu.NewBoolQuery().
Options(&osu.BoolQueryOptions{MinimumShouldMatch: 1}).
Params(&osu.BoolQueryParams{MinimumShouldMatch: 1}).
Should(
osu.NewTermQuery[string]("a").Value("a"),
osu.NewTermQuery[string]("b").Value("b"),

View File

@@ -110,19 +110,19 @@ func isEmpty(x any) bool {
}
}
func merge[T any](options ...T) T {
mapOptions := make(map[string]any)
func merge[T any](vals ...T) T {
base := make(map[string]any)
for _, option := range options {
data, err := conversions.To[map[string]any](option)
for _, val := range vals {
data, err := conversions.To[map[string]any](val)
if err != nil {
continue
}
_ = mergo.Merge(&mapOptions, data)
_ = mergo.Merge(&base, data)
}
data, _ := conversions.To[T](mapOptions)
data, _ := conversions.To[T](base)
return data
}

View File

@@ -9,10 +9,10 @@ type BoolQuery struct {
mustNot []Builder
should []Builder
filter []Builder
options *BoolQueryOptions
params *BoolQueryParams
}
type BoolQueryOptions struct {
type BoolQueryParams struct {
MinimumShouldMatch int16 `json:"minimum_should_match,omitempty"`
Boost float32 `json:"boost,omitempty"`
Name string `json:"_name,omitempty"`
@@ -22,8 +22,8 @@ func NewBoolQuery() *BoolQuery {
return &BoolQuery{}
}
func (q *BoolQuery) Options(v *BoolQueryOptions) *BoolQuery {
q.options = v
func (q *BoolQuery) Params(v *BoolQueryParams) *BoolQuery {
q.params = v
return q
}
@@ -48,7 +48,7 @@ func (q *BoolQuery) Filter(v ...Builder) *BoolQuery {
}
func (q *BoolQuery) Map() (map[string]any, error) {
base, err := newBase(q.options)
base, err := newBase(q.params)
if err != nil {
return nil, err
}

View File

@@ -17,8 +17,8 @@ func TestBoolQuery(t *testing.T) {
Want: nil,
},
{
Name: "with-options",
Got: osu.NewBoolQuery().Options(&osu.BoolQueryOptions{
Name: "with params",
Got: osu.NewBoolQuery().Params(&osu.BoolQueryParams{
MinimumShouldMatch: 10,
Boost: 10,
Name: "some-name",

View File

@@ -5,12 +5,12 @@ import (
)
type MatchPhraseQuery struct {
field string
query string
options *MatchPhraseQueryOptions
field string
query string
params *MatchPhraseQueryParams
}
type MatchPhraseQueryOptions struct {
type MatchPhraseQueryParams struct {
Analyzer string `json:"analyzer,omitempty"`
Slop int `json:"slop,omitempty"`
ZeroTermsQuery string `json:"zero_terms_query,omitempty"`
@@ -20,8 +20,8 @@ func NewMatchPhraseQuery(field string) *MatchPhraseQuery {
return &MatchPhraseQuery{field: field}
}
func (q *MatchPhraseQuery) Options(v *MatchPhraseQueryOptions) *MatchPhraseQuery {
q.options = v
func (q *MatchPhraseQuery) Params(v *MatchPhraseQueryParams) *MatchPhraseQuery {
q.params = v
return q
}
@@ -31,7 +31,7 @@ func (q *MatchPhraseQuery) Query(v string) *MatchPhraseQuery {
}
func (q *MatchPhraseQuery) Map() (map[string]any, error) {
base, err := newBase(q.options)
base, err := newBase(q.params)
if err != nil {
return nil, err
}

View File

@@ -17,8 +17,8 @@ func TestNewMatchPhraseQuery(t *testing.T) {
Want: nil,
},
{
Name: "options",
Got: osu.NewMatchPhraseQuery("name").Options(&osu.MatchPhraseQueryOptions{
Name: "with params",
Got: osu.NewMatchPhraseQuery("name").Params(&osu.MatchPhraseQueryParams{
Analyzer: "analyzer",
Slop: 2,
ZeroTermsQuery: "all",
@@ -46,7 +46,7 @@ func TestNewMatchPhraseQuery(t *testing.T) {
},
{
Name: "full",
Got: osu.NewMatchPhraseQuery("name").Options(&osu.MatchPhraseQueryOptions{
Got: osu.NewMatchPhraseQuery("name").Params(&osu.MatchPhraseQueryParams{
Analyzer: "analyzer",
Slop: 2,
ZeroTermsQuery: "all",

View File

@@ -6,11 +6,11 @@ import (
)
type IDsQuery struct {
values []string
options *IDsQueryOptions
values []string
params *IDsQueryParams
}
type IDsQueryOptions struct {
type IDsQueryParams struct {
Boost float32 `json:"boost,omitempty"`
}
@@ -18,13 +18,13 @@ func NewIDsQuery(v ...string) *IDsQuery {
return &IDsQuery{values: slices.Compact(v)}
}
func (q *IDsQuery) Options(v *IDsQueryOptions) *IDsQuery {
q.options = v
func (q *IDsQuery) Params(v *IDsQueryParams) *IDsQuery {
q.params = v
return q
}
func (q *IDsQuery) Map() (map[string]any, error) {
base, err := newBase(q.options)
base, err := newBase(q.params)
if err != nil {
return nil, err
}

View File

@@ -17,7 +17,7 @@ func TestIDsQuery(t *testing.T) {
Want: nil,
},
{
Name: "no options",
Name: "no params",
Got: osu.NewIDsQuery("1", "2", "3", "3"),
Want: map[string]any{
"ids": map[string]any{
@@ -27,7 +27,7 @@ func TestIDsQuery(t *testing.T) {
},
{
Name: "ids",
Got: osu.NewIDsQuery("1", "2", "3", "3").Options(&osu.IDsQueryOptions{Boost: 1.0}),
Got: osu.NewIDsQuery("1", "2", "3", "3").Params(&osu.IDsQueryParams{Boost: 1.0}),
Want: map[string]any{
"ids": map[string]any{
"values": []string{"1", "2", "3"},

View File

@@ -7,15 +7,15 @@ import (
)
type RangeQuery[T time.Time | string] struct {
field string
gt T
gte T
lt T
lte T
options *RangeQueryOptions
field string
gt T
gte T
lt T
lte T
params *RangeQueryParams
}
type RangeQueryOptions struct {
type RangeQueryParams struct {
Format string `json:"format,omitempty"`
Relation string `json:"relation,omitempty"`
Boost float32 `json:"boost,omitempty"`
@@ -26,8 +26,8 @@ func NewRangeQuery[T time.Time | string](field string) *RangeQuery[T] {
return &RangeQuery[T]{field: field}
}
func (q *RangeQuery[T]) Options(v *RangeQueryOptions) *RangeQuery[T] {
q.options = v
func (q *RangeQuery[T]) Params(v *RangeQueryParams) *RangeQuery[T] {
q.params = v
return q
}
@@ -60,7 +60,7 @@ func (q *RangeQuery[T]) Map() (map[string]any, error) {
return nil, errors.New("cannot set both lt and lte in RangeQuery")
}
base, err := newBase(q.options)
base, err := newBase(q.params)
if err != nil {
return nil, err
}

View File

@@ -121,8 +121,8 @@ func TestRangeQuery(t *testing.T) {
Err: errors.New(""),
},
{
Name: "options",
Got: osu.NewRangeQuery[time.Time]("created").Options(&osu.RangeQueryOptions{
Name: "with params",
Got: osu.NewRangeQuery[time.Time]("created").Params(&osu.RangeQueryParams{
Format: "strict_date_optional_time",
Relation: "within",
Boost: 1.0,

View File

@@ -5,12 +5,12 @@ import (
)
type TermQuery[T comparable] struct {
field string
value T
options *TermQueryOptions
field string
value T
params *TermQueryParams
}
type TermQueryOptions struct {
type TermQueryParams struct {
Boost float32 `json:"boost,omitempty"`
CaseInsensitive bool `json:"case_insensitive,omitempty"`
Name string `json:"_name,omitempty"`
@@ -20,8 +20,8 @@ func NewTermQuery[T comparable](field string) *TermQuery[T] {
return &TermQuery[T]{field: field}
}
func (q *TermQuery[T]) Options(v *TermQueryOptions) *TermQuery[T] {
q.options = v
func (q *TermQuery[T]) Params(v *TermQueryParams) *TermQuery[T] {
q.params = v
return q
}
@@ -31,7 +31,7 @@ func (q *TermQuery[T]) Value(v T) *TermQuery[T] {
}
func (q *TermQuery[T]) Map() (map[string]any, error) {
base, err := newBase(q.options)
base, err := newBase(q.params)
if err != nil {
return nil, err
}

View File

@@ -17,7 +17,7 @@ func TestTermQuery(t *testing.T) {
Want: nil,
},
{
Name: "no-options",
Name: "no params",
Got: osu.NewTermQuery[bool]("deleted").Value(false),
Want: map[string]any{
"term": map[string]any{
@@ -28,8 +28,8 @@ func TestTermQuery(t *testing.T) {
},
},
{
Name: "with-options",
Got: osu.NewTermQuery[bool]("deleted").Options(&osu.TermQueryOptions{
Name: "with params",
Got: osu.NewTermQuery[bool]("deleted").Params(&osu.TermQueryParams{
Boost: 1.0,
CaseInsensitive: true,
Name: "is-deleted",

View File

@@ -5,12 +5,12 @@ import (
)
type WildcardQuery struct {
field string
value string
options *WildcardQueryOptions
field string
value string
params *WildcardQueryParams
}
type WildcardQueryOptions struct {
type WildcardQueryParams struct {
Boost float32 `json:"boost,omitempty"`
CaseInsensitive bool `json:"case_insensitive,omitempty"`
Rewrite string `json:"rewrite,omitempty"`
@@ -20,8 +20,8 @@ func NewWildcardQuery(field string) *WildcardQuery {
return &WildcardQuery{field: field}
}
func (q *WildcardQuery) Options(v *WildcardQueryOptions) *WildcardQuery {
q.options = v
func (q *WildcardQuery) Params(v *WildcardQueryParams) *WildcardQuery {
q.params = v
return q
}
@@ -31,7 +31,7 @@ func (q *WildcardQuery) Value(v string) *WildcardQuery {
}
func (q *WildcardQuery) Map() (map[string]any, error) {
base, err := newBase(q.options)
base, err := newBase(q.params)
if err != nil {
return nil, err
}

View File

@@ -18,7 +18,7 @@ func TestWildcardQuery(t *testing.T) {
},
{
Name: "wildcard",
Got: osu.NewWildcardQuery("name").Options(&osu.WildcardQueryOptions{
Got: osu.NewWildcardQuery("name").Params(&osu.WildcardQueryParams{
Boost: 1.0,
CaseInsensitive: true,
Rewrite: "top_terms_blended_freqs_N",

View File

@@ -7,17 +7,17 @@ import (
opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi"
)
type RequestBody[O any] struct {
query Builder
options O
type QueryReqBody[P any] struct {
query Builder
params P
}
func NewRequestBody[O any](q Builder, o ...O) *RequestBody[O] {
return &RequestBody[O]{query: q, options: merge(o...)}
func NewQueryReqBody[P any](q Builder, p ...P) *QueryReqBody[P] {
return &QueryReqBody[P]{query: q, params: merge(p...)}
}
func (q RequestBody[O]) Map() (map[string]any, error) {
base, err := newBase(q.options)
func (q QueryReqBody[O]) Map() (map[string]any, error) {
base, err := newBase(q.params)
if err != nil {
return nil, err
}
@@ -29,7 +29,7 @@ func (q RequestBody[O]) Map() (map[string]any, error) {
return base, nil
}
func (q RequestBody[O]) MarshalJSON() ([]byte, error) {
func (q QueryReqBody[O]) MarshalJSON() ([]byte, error) {
data, err := q.Map()
if err != nil {
return nil, err
@@ -40,13 +40,13 @@ func (q RequestBody[O]) MarshalJSON() ([]byte, error) {
//----------------------------------------------------------------------------//
type HighlightOption struct {
PreTags []string `json:"pre_tags,omitempty"`
PostTags []string `json:"post_tags,omitempty"`
Fields map[string]HighlightOption `json:"fields,omitempty"`
type BodyParamHighlight struct {
PreTags []string `json:"pre_tags,omitempty"`
PostTags []string `json:"post_tags,omitempty"`
Fields map[string]BodyParamHighlight `json:"fields,omitempty"`
}
type ScriptOption struct {
type BodyParamScript struct {
Source string `json:"source,omitempty"`
Lang string `json:"lang,omitempty"`
Params map[string]any `json:"params,omitempty"`
@@ -54,8 +54,8 @@ type ScriptOption struct {
//----------------------------------------------------------------------------//
func BuildSearchReq(req *opensearchgoAPI.SearchReq, q Builder, o ...SearchReqOptions) (*opensearchgoAPI.SearchReq, error) {
body, err := json.Marshal(NewRequestBody(q, o...))
func BuildSearchReq(req *opensearchgoAPI.SearchReq, q Builder, p ...SearchBodyParams) (*opensearchgoAPI.SearchReq, error) {
body, err := json.Marshal(NewQueryReqBody(q, p...))
if err != nil {
return nil, err
}
@@ -63,14 +63,14 @@ func BuildSearchReq(req *opensearchgoAPI.SearchReq, q Builder, o ...SearchReqOpt
return req, nil
}
type SearchReqOptions struct {
Highlight *HighlightOption `json:"highlight,omitempty"`
type SearchBodyParams struct {
Highlight *BodyParamHighlight `json:"highlight,omitempty"`
}
//----------------------------------------------------------------------------//
func BuildDocumentDeleteByQueryReq(req opensearchgoAPI.DocumentDeleteByQueryReq, q Builder) (opensearchgoAPI.DocumentDeleteByQueryReq, error) {
body, err := json.Marshal(NewRequestBody[any](q))
body, err := json.Marshal(NewQueryReqBody[any](q))
if err != nil {
return req, err
}
@@ -80,8 +80,8 @@ func BuildDocumentDeleteByQueryReq(req opensearchgoAPI.DocumentDeleteByQueryReq,
//----------------------------------------------------------------------------//
func BuildUpdateByQueryReq(req opensearchgoAPI.UpdateByQueryReq, q Builder, o ...UpdateByQueryReqOptions) (opensearchgoAPI.UpdateByQueryReq, error) {
body, err := json.Marshal(NewRequestBody(q, o...))
func BuildUpdateByQueryReq(req opensearchgoAPI.UpdateByQueryReq, q Builder, o ...UpdateByQueryBodyParams) (opensearchgoAPI.UpdateByQueryReq, error) {
body, err := json.Marshal(NewQueryReqBody(q, o...))
if err != nil {
return req, err
}
@@ -89,14 +89,14 @@ func BuildUpdateByQueryReq(req opensearchgoAPI.UpdateByQueryReq, q Builder, o ..
return req, nil
}
type UpdateByQueryReqOptions struct {
Script *ScriptOption `json:"script,omitempty"`
type UpdateByQueryBodyParams struct {
Script *BodyParamScript `json:"script,omitempty"`
}
//----------------------------------------------------------------------------//
func BuildIndicesCountReq(req *opensearchgoAPI.IndicesCountReq, q Builder) (*opensearchgoAPI.IndicesCountReq, error) {
body, err := json.Marshal(NewRequestBody[any](q))
body, err := json.Marshal(NewQueryReqBody[any](q))
if err != nil {
return nil, err
}

View File

@@ -16,7 +16,7 @@ func TestRequestBody(t *testing.T) {
tests := []opensearchtest.TableTest[osu.Builder, map[string]any]{
{
Name: "simple",
Got: osu.NewRequestBody[any](osu.NewTermQuery[string]("name").Value("tom")),
Got: osu.NewQueryReqBody[any](osu.NewTermQuery[string]("name").Value("tom")),
Want: map[string]any{
"query": map[string]any{
"term": map[string]any{
@@ -44,11 +44,11 @@ func TestBuildSearchReq(t *testing.T) {
req, _ := osu.BuildSearchReq(
&opensearchgoAPI.SearchReq{},
osu.NewTermQuery[string]("content").Value("content"),
osu.SearchReqOptions{
Highlight: &osu.HighlightOption{
osu.SearchBodyParams{
Highlight: &osu.BodyParamHighlight{
PreTags: []string{"<b>"},
PostTags: []string{"</b>"},
Fields: map[string]osu.HighlightOption{
Fields: map[string]osu.BodyParamHighlight{
"content": {},
},
},