From 51f1e779b834de6b0ecfa3236f824182b2783a25 Mon Sep 17 00:00:00 2001 From: Bundit Jitkongchuen Date: Mon, 15 Apr 2024 18:12:22 +0700 Subject: [PATCH] add fields to document get api response (#526) Signed-off-by: Bundit Jitkonghcuen --- CHANGELOG.md | 1 + opensearchapi/api_document-get.go | 1 + opensearchapi/api_document_test.go | 39 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da9b3ad0c..e44d5ded6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Adds security plugin ([#507](https://github.com/opensearch-project/opensearch-go/pull/507)) - Adds security settings to container for security testing ([#507](https://github.com/opensearch-project/opensearch-go/pull/507)) - Adds cluster.get-certs to copy admin certs out of the container ([#507](https://github.com/opensearch-project/opensearch-go/pull/507)) +- Add the `Fields` field containing stored fields to the `DocumentGetResp` struct (#526)[https://github.com/opensearch-project/opensearch-go/pull/526] ### Changed - Uses docker compose v2 instead of v1 ([#506](https://github.com/opensearch-project/opensearch-go/pull/506)) diff --git a/opensearchapi/api_document-get.go b/opensearchapi/api_document-get.go index 697c5ae97..9f3ca2a38 100644 --- a/opensearchapi/api_document-get.go +++ b/opensearchapi/api_document-get.go @@ -44,6 +44,7 @@ type DocumentGetResp struct { Found bool `json:"found"` Type string `json:"_type"` // Deprecated field Source json.RawMessage `json:"_source"` + Fields json.RawMessage `json:"fields"` response *opensearch.Response } diff --git a/opensearchapi/api_document_test.go b/opensearchapi/api_document_test.go index 31475038d..f924fcdea 100644 --- a/opensearchapi/api_document_test.go +++ b/opensearchapi/api_document_test.go @@ -393,5 +393,44 @@ func TestDocumentClient(t *testing.T) { assert.NotNil(t, res.Inspect().Response) ostest.CompareRawJSONwithParsedJSON(t, res.Source, res.Inspect().Response) }) + t.Run("Fields", func(t *testing.T) { + _, err := client.Indices.Mapping.Put(nil, + opensearchapi.MappingPutReq{ + Indices: []string{index}, + Body: strings.NewReader(`{ + "properties": { + "foo-stored": { + "type": "text", + "store":true + } + } + }`), + }) + require.Nil(t, err) + _, err = client.Document.Create( + nil, + opensearchapi.DocumentCreateReq{ + Index: index, + Body: strings.NewReader(`{"foo-stored": "bar"}`), + DocumentID: "test-stored-field", + }, + ) + require.Nil(t, err) + res, err := client.Document.Get( + nil, + opensearchapi.DocumentGetReq{ + Index: index, + DocumentID: "test-stored-field", + Params: opensearchapi.DocumentGetParams{ + StoredFields: []string{"foo-stored"}, + }, + }, + ) + require.Nil(t, err) + require.NotNil(t, res) + assert.NotNil(t, res.Inspect().Response) + ostest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response) + assert.NotEmpty(t, res.Fields) + }) }) }