From f3ad33348b9b61f17bf339f323667cc8e9280782 Mon Sep 17 00:00:00 2001 From: Ponlawat Suparat Date: Fri, 5 Apr 2024 02:12:04 +0700 Subject: [PATCH] Adds the `Fields` field containing the document fields to the `SearchHit` struct (#508) Signed-off-by: Ponlawat Suparat --- CHANGELOG.md | 5 +++-- opensearchapi/api_search.go | 3 ++- opensearchapi/api_search_test.go | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e0f9b3f0..5b05bf25d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,10 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] ### Added -- Adds GlobalIOUsage struct for nodes stats ([#506]((https://github.com/opensearch-project/opensearch-go/pull/506)) +- Adds GlobalIOUsage struct for nodes stats ([#506]((https://github.com/opensearch-project/opensearch-go/pull/506))) -- Adds the `Explanation` field containing the document explain details to the `SearchHit` struct. ([#504](https://github.com/opensearch-project/opensearch-go/pull/504) +- Adds the `Explanation` field containing the document explain details to the `SearchHit` struct. ([#504](https://github.com/opensearch-project/opensearch-go/pull/504)) +- Adds the `Fields` field containing the document fields to the `SearchHit` struct. ([#508](https://github.com/opensearch-project/opensearch-go/pull/508)) ### Changed - Use docker compose v2 instead of v1 ([#506]((https://github.com/opensearch-project/opensearch-go/pull/506)) diff --git a/opensearchapi/api_search.go b/opensearchapi/api_search.go index 919575c8c..b4ee47753 100644 --- a/opensearchapi/api_search.go +++ b/opensearchapi/api_search.go @@ -80,7 +80,7 @@ type SearchResp struct { response *opensearch.Response } -// Inspect returns the Inspect type containing the raw *opensearch.Reponse +// Inspect returns the Inspect type containing the raw *opensearch.Response func (r SearchResp) Inspect() Inspect { return Inspect{Response: r.response} } @@ -91,6 +91,7 @@ type SearchHit struct { ID string `json:"_id"` Score float32 `json:"_score"` Source json.RawMessage `json:"_source"` + Fields json.RawMessage `json:"fields"` Type string `json:"_type"` // Deprecated field Sort []any `json:"sort"` Explanation *DocumentExplainDetails `json:"_explanation"` diff --git a/opensearchapi/api_search_test.go b/opensearchapi/api_search_test.go index c7adafb2b..27bfeb0df 100644 --- a/opensearchapi/api_search_test.go +++ b/opensearchapi/api_search_test.go @@ -71,4 +71,21 @@ func TestSearch(t *testing.T) { assert.NotEmpty(t, resp.Hits.Hits) assert.NotNil(t, resp.Hits.Hits[0].Explanation) }) + + t.Run("request with retrieve specific fields", func(t *testing.T) { + resp, err := client.Search(nil, &opensearchapi.SearchReq{Indices: []string{index}, Body: strings.NewReader(`{ + "query": { + "match": { + "foo": "bar" + } + }, + "fields": [ + "foo" + ], + "_source": false + }`)}) + require.Nil(t, err) + assert.NotEmpty(t, resp.Hits.Hits) + assert.NotEmpty(t, resp.Hits.Hits[0].Fields) + }) }