Skip to content

Commit

Permalink
Add missing SeqNo and PrimaryTerm fields in SearchHit (#574)
Browse files Browse the repository at this point in the history
* add SeqNo and PrimaryTerm to SearchHit

Signed-off-by: Erik Tammaru <[email protected]>

* update CHANGELOG

Signed-off-by: Erik Tammaru <[email protected]>

---------

Signed-off-by: Erik Tammaru <[email protected]>
Co-authored-by: Erik Tammaru <[email protected]>
  • Loading branch information
emtammaru and Erik Tammaru authored Jul 1, 2024
1 parent dbe714b commit 2464386
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions opensearchapi/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
38 changes: 38 additions & 0 deletions opensearchapi/api_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

0 comments on commit 2464386

Please sign in to comment.