Skip to content

Commit 3bf6c18

Browse files
Anaethelionbenjyiw
andauthored
feat: implement if_seq_no and if_primary_term parameters in bulk indexer (#783) (#784)
Co-authored-by: ben <[email protected]>
1 parent 3e540dd commit 3bf6c18

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

esutil/bulk_indexer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ type BulkIndexerItem struct {
107107
VersionType string
108108
Body io.ReadSeeker
109109
RetryOnConflict *int
110+
IfSeqNo *int64
111+
IfPrimaryTerm *int64
110112
meta bytes.Buffer // Item metadata header
111113
payloadLength int // Item payload total length metadata+newline+body length
112114

@@ -177,6 +179,15 @@ func (item *BulkIndexerItem) marshallMeta() {
177179
aux = aux[:0]
178180
}
179181

182+
if item.DocumentID != "" && item.IfSeqNo != nil && item.IfPrimaryTerm != nil {
183+
item.meta.WriteRune(',')
184+
item.meta.WriteString(`"if_seq_no":`)
185+
item.meta.WriteString(strconv.FormatInt(*item.IfSeqNo, 10))
186+
item.meta.WriteRune(',')
187+
item.meta.WriteString(`"if_primary_term":`)
188+
item.meta.WriteString(strconv.FormatInt(*item.IfPrimaryTerm, 10))
189+
}
190+
180191
item.meta.WriteRune('}')
181192
item.meta.WriteRune('}')
182193
item.meta.WriteRune('\n')

esutil/bulk_indexer_internal_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ func TestBulkIndexer(t *testing.T) {
598598
})
599599
t.Run("Worker.writeMeta()", func(t *testing.T) {
600600
v := int64(23)
601+
ifSeqNo := int64(45)
602+
ifPrimaryTerm := int64(67)
601603
type args struct {
602604
item BulkIndexerItem
603605
}
@@ -706,6 +708,16 @@ func TestBulkIndexer(t *testing.T) {
706708
}},
707709
`{"update":{"_id":"1","retry_on_conflict":3}}` + "\n",
708710
},
711+
{
712+
"with if_seq_no and if_primary_term",
713+
args{BulkIndexerItem{
714+
Action: "index",
715+
DocumentID: "1",
716+
IfSeqNo: &ifSeqNo,
717+
IfPrimaryTerm: &ifPrimaryTerm,
718+
}},
719+
`{"index":{"_id":"1","if_seq_no":45,"if_primary_term":67}}` + "\n",
720+
},
709721
}
710722
for _, tt := range tests {
711723
tt := tt

0 commit comments

Comments
 (0)