From 2464386c5b7147dd1a23aef0567db849627a464f Mon Sep 17 00:00:00 2001 From: emtammaru Date: Mon, 1 Jul 2024 06:39:39 -0400 Subject: [PATCH] Add missing SeqNo and PrimaryTerm fields in SearchHit (#574) * add SeqNo and PrimaryTerm to SearchHit Signed-off-by: Erik Tammaru * update CHANGELOG Signed-off-by: Erik Tammaru --------- Signed-off-by: Erik Tammaru Co-authored-by: Erik Tammaru --- CHANGELOG.md | 1 + opensearchapi/api_search.go | 2 ++ opensearchapi/api_search_test.go | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd3f9122c..af2c04603 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Adds the `SearchPipelines` field to `SearchParams` ([#532](https://github.com/opensearch-project/opensearch-go/pull/532)) - Adds support for OpenSearch 2.14 ([#552](https://github.com/opensearch-project/opensearch-go/pull/552)) - Adds the `Caches` field to Node stats ([#572](https://github.com/opensearch-project/opensearch-go/pull/572)) +- Adds the `SeqNo` and `PrimaryTerm` fields in `SearchHit` ([#574](https://github.com/opensearch-project/opensearch-go/pull/574)) ### Changed - Security roles get response struct has its own sub structs without omitempty ([#572](https://github.com/opensearch-project/opensearch-go/pull/572)) diff --git a/opensearchapi/api_search.go b/opensearchapi/api_search.go index a378562f9..08e039d51 100644 --- a/opensearchapi/api_search.go +++ b/opensearchapi/api_search.go @@ -96,4 +96,6 @@ type SearchHit struct { Type string `json:"_type"` // Deprecated field Sort []any `json:"sort"` Explanation *DocumentExplainDetails `json:"_explanation"` + SeqNo *int `json:"_seq_no"` + PrimaryTerm *int `json:"_primary_term"` } diff --git a/opensearchapi/api_search_test.go b/opensearchapi/api_search_test.go index 6cda9babc..b72da00c9 100644 --- a/opensearchapi/api_search_test.go +++ b/opensearchapi/api_search_test.go @@ -128,4 +128,42 @@ func TestSearch(t *testing.T) { assert.NotEmpty(t, resp.Hits.Hits[0].Routing) assert.Equal(t, "foo", resp.Hits.Hits[0].Routing) }) + + t.Run("with seq_no and primary_term", func(t *testing.T) { + seqNoPrimaryTerm := true + resp, err := client.Search(nil, &opensearchapi.SearchReq{ + Indices: []string{index}, + Body: strings.NewReader(""), + Params: opensearchapi.SearchParams{ + SeqNoPrimaryTerm: &seqNoPrimaryTerm, + }, + }) + require.Nil(t, err) + assert.NotNil(t, resp) + ostest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response) + assert.NotEmpty(t, resp.Hits.Hits) + for _, hit := range resp.Hits.Hits { + assert.NotNil(t, hit.SeqNo) + assert.NotNil(t, hit.PrimaryTerm) + } + }) + + t.Run("without seq_no and primary_term", func(t *testing.T) { + seqNoPrimaryTerm := false + resp, err := client.Search(nil, &opensearchapi.SearchReq{ + Indices: []string{index}, + Body: strings.NewReader(""), + Params: opensearchapi.SearchParams{ + SeqNoPrimaryTerm: &seqNoPrimaryTerm, + }, + }) + require.Nil(t, err) + assert.NotNil(t, resp) + ostest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response) + assert.NotEmpty(t, resp.Hits.Hits) + for _, hit := range resp.Hits.Hits { + assert.Nil(t, hit.SeqNo) + assert.Nil(t, hit.PrimaryTerm) + } + }) }