Skip to content

Commit

Permalink
Fixing GetSourceRequest (#402)
Browse files Browse the repository at this point in the history
* Fixing GetSourceRequest source field

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Adding sample code for get source api

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Adding changelog

Signed-off-by: Vacha Shah <vachshah@amazon.com>

---------

Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah authored Oct 23, 2023
1 parent 58ff7a7 commit 29f1735
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Fixed

- Corrects AWSv4 signature on DataStream Stats with no index name specified ([#338](https://github.com/opensearch-project/opensearch-go/pull/338))
- Fixed GetSourceRequest `Source` field and deprecated the `Source` parameter ([#402](https://github.com/opensearch-project/opensearch-go/pull/402))

### Security

Expand Down
36 changes: 36 additions & 0 deletions guides/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,42 @@ log.Printf("deleted pits: [%+v]", delpits)

Note that a point-in-time is associated with an index or a set of index. So, when performing a search with a point-in-time, you DO NOT specify the index in the search.

## Source API

The source API returns the source of the documents with included or excluded fields. The following example returns all fields from document source in the `movies` index:

```go
getSourceRequest := opensearchapi.GetSourceRequest{
Index: "movies",
DocumentID: "1",
}
getSourceResponse, err := getSourceRequest.Do(context.Background(), client)
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("source: [%+v]", getSourceResponse)
```

To include certain fields in the source response, use `SourceIncludes` or `Source`(this field is deprecated and `SourceIncludes` is recommended to be used instead). To get only required fields:

```go
getSourceRequest = opensearchapi.GetSourceRequest{
Index: "movies",
DocumentID: "1",
SourceIncludes: []string{"title"},
}
```

To exclude certain fields in the source response, use `SourceExcludes` as follows:

```go
getSourceRequest = opensearchapi.GetSourceRequest{
Index: "movies",
DocumentID: "1",
SourceExcludes: []string{"title"},
}
```

## Cleanup

```go
Expand Down
15 changes: 6 additions & 9 deletions opensearchapi/api.get_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ type GetSourceRequest struct {
Realtime *bool
Refresh *bool
Routing string
Source interface{}
// Deprecated: This parameter is similar to SourceIncludes, please use that instead.
Source []string
SourceExcludes []string
SourceIncludes []string
Version *int
Expand Down Expand Up @@ -113,12 +114,8 @@ func (r GetSourceRequest) Do(ctx context.Context, transport Transport) (*Respons
params["routing"] = r.Routing
}

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.Source) > 0 {
params["_source"] = strings.Join(r.Source, ",")
}

if len(r.SourceExcludes) > 0 {
Expand Down Expand Up @@ -236,9 +233,9 @@ func (f GetSource) WithRouting(v string) func(*GetSourceRequest) {
}
}

// WithSource - true or false to return the _source field or not, or a list of fields to return.
// WithSource - a list of fields to return. This field is deprecated, please use SourceIncludes instead.
//
func (f GetSource) WithSource(v interface{}) func(*GetSourceRequest) {
func (f GetSource) WithSource(v ...string) func(*GetSourceRequest) {
return func(r *GetSourceRequest) {
r.Source = v
}
Expand Down
63 changes: 63 additions & 0 deletions opensearchapi/opensearchapi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"

"github.com/opensearch-project/opensearch-go/v2"
"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
Expand Down Expand Up @@ -123,6 +124,68 @@ func TestAPI(t *testing.T) {
t.Fatalf("Failed to index data: %s", err)
}

// Add a document to the index.
document := strings.NewReader(`{
"title": "Moneyball",
"director": "Bennett Miller",
"year": "2011"
}`)

docId := "1"
req := opensearchapi.IndexRequest{
Index: "test",
DocumentID: docId,
Body: document,
}
insertResponse, err := req.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to insert document ", err)
}
defer insertResponse.Body.Close()

// Get the document with source filters
getSourceRequest := opensearchapi.GetSourceRequest{
Index: "test",
DocumentID: "1",
Source: []string{"title"},
}
getSourceResponse, err := getSourceRequest.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to get source ", err)
}
assert.Equal(t, 200, getSourceResponse.StatusCode)
assert.Contains(t, getSourceResponse.String(), "title")
assert.NotContains(t, getSourceResponse.String(), "director")
defer getSourceResponse.Body.Close()

getSourceRequest = opensearchapi.GetSourceRequest{
Index: "test",
DocumentID: "1",
SourceIncludes: []string{"title"},
}
getSourceResponse, err = getSourceRequest.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to get source ", err)
}
assert.Equal(t, 200, getSourceResponse.StatusCode)
assert.Contains(t, getSourceResponse.String(), "title")
assert.NotContains(t, getSourceResponse.String(), "director")
defer getSourceResponse.Body.Close()

getSourceRequest = opensearchapi.GetSourceRequest{
Index: "test",
DocumentID: "1",
SourceExcludes: []string{"title"},
}
getSourceResponse, err = getSourceRequest.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to get source ", err)
}
assert.Equal(t, 200, getSourceResponse.StatusCode)
assert.Contains(t, getSourceResponse.String(), "director")
assert.NotContains(t, getSourceResponse.String(), "title")
defer getSourceResponse.Body.Close()

// Launch reindexing task with wait_for_completion=false
//
res, err = client.Reindex(
Expand Down

0 comments on commit 29f1735

Please sign in to comment.