Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds dynamic type to _source field #285

Merged
merged 12 commits into from
Apr 14, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adds markdown linter ([#261](https://github.com/opensearch-project/opensearch-go/pull/261))
- Adds testcases to check upsert functionality ([#207](https://github.com/opensearch-project/opensearch-go/issues/207))
- Added @Jakob3xD to co-maintainers ([#270](https://github.com/opensearch-project/opensearch-go/pull/270))
zethuman marked this conversation as resolved.
Show resolved Hide resolved
- Adds dynamic type to \_source field ([#158](https://github.com/opensearch-project/opensearch-go/issues/158))
- Adds testcases for Document API ([#280](https://github.com/opensearch-project/opensearch-go/issues/280))

### Changed

Expand Down
14 changes: 9 additions & 5 deletions opensearchapi/api.bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ type Bulk func(body io.Reader, o ...func(*BulkRequest)) (*Response, error)
// BulkRequest configures the Bulk API request.
//
type BulkRequest struct {
Index string
Index string

Body io.Reader

Pipeline string
Refresh string
RequireAlias *bool
Routing string
Source []string
Source interface{}
SourceExcludes []string
SourceIncludes []string
Timeout time.Duration
Expand Down Expand Up @@ -116,8 +116,12 @@ func (r BulkRequest) Do(ctx context.Context, transport Transport) (*Response, er
params["routing"] = r.Routing
}

if len(r.Source) > 0 {
params["_source"] = strings.Join(r.Source, ",")
if source, ok := r.Source.(bool); ok {
params["_source"] = strconv.FormatBool(source)
} else if source, ok := r.Source.(string); ok && source != "" {
params["_source"] = source
} else if sources, ok := r.Source.([]string); ok && len(sources) > 0 {
params["_source"] = strings.Join(sources, ",")
}

if len(r.SourceExcludes) > 0 {
Expand Down Expand Up @@ -249,7 +253,7 @@ func (f Bulk) WithRouting(v string) func(*BulkRequest) {

// WithSource - true or false to return the _source field or not, or default list of fields to return, can be overridden on each sub-request.
//
func (f Bulk) WithSource(v ...string) func(*BulkRequest) {
func (f Bulk) WithSource(v interface{}) func(*BulkRequest) {
return func(r *BulkRequest) {
r.Source = v
}
Expand Down
14 changes: 9 additions & 5 deletions opensearchapi/api.delete_by_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type DeleteByQuery func(index []string, body io.Reader, o ...func(*DeleteByQuery
// DeleteByQueryRequest configures the Delete By Query API request.
//
type DeleteByQueryRequest struct {
Index []string
Index []string

Body io.Reader

Expand Down Expand Up @@ -84,7 +84,7 @@ type DeleteByQueryRequest struct {
Size *int
Slices interface{}
Sort []string
Source []string
Source interface{}
SourceExcludes []string
SourceIncludes []string
Stats []string
Expand Down Expand Up @@ -219,8 +219,12 @@ func (r DeleteByQueryRequest) Do(ctx context.Context, transport Transport) (*Res
params["sort"] = strings.Join(r.Sort, ",")
}

if len(r.Source) > 0 {
params["_source"] = strings.Join(r.Source, ",")
if source, ok := r.Source.(bool); ok {
params["_source"] = strconv.FormatBool(source)
} else if source, ok := r.Source.(string); ok && source != "" {
params["_source"] = source
} else if sources, ok := r.Source.([]string); ok && len(sources) > 0 {
params["_source"] = strings.Join(sources, ",")
}

if len(r.SourceExcludes) > 0 {
Expand Down Expand Up @@ -520,7 +524,7 @@ func (f DeleteByQuery) WithSort(v ...string) func(*DeleteByQueryRequest) {

// WithSource - true or false to return the _source field or not, or a list of fields to return.
//
func (f DeleteByQuery) WithSource(v ...string) func(*DeleteByQueryRequest) {
func (f DeleteByQuery) WithSource(v interface{}) func(*DeleteByQueryRequest) {
return func(r *DeleteByQueryRequest) {
r.Source = v
}
Expand Down
Loading