Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic templates to bulk indexer item #189

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions bulk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,15 @@
}

type BulkIndexerItem struct {
Index string
DocumentID string
Body io.WriterTo
Index string
DocumentID string
Body io.WriterTo
DynamicTemplates map[string]string
carsonip marked this conversation as resolved.
Show resolved Hide resolved
}

// Add encodes an item in the buffer.
func (b *BulkIndexer) Add(item BulkIndexerItem) error {
b.writeMeta(item.Index, item.DocumentID)
b.writeMeta(item.Index, item.DocumentID, item.DynamicTemplates)
if _, err := item.Body.WriteTo(b.writer); err != nil {
return fmt.Errorf("failed to write bulk indexer item: %w", err)
}
Expand All @@ -287,18 +288,39 @@
return nil
}

func (b *BulkIndexer) writeMeta(index, documentID string) {
func (b *BulkIndexer) writeMeta(index, documentID string, dynamicTemplates map[string]string) {
b.jsonw.RawString(`{"create":{`)
first := true
if documentID != "" {
b.jsonw.RawString(`"_id":`)
b.jsonw.String(documentID)
first = false
}
if index != "" {
if documentID != "" {
if !first {
b.jsonw.RawByte(',')
}
b.jsonw.RawString(`"_index":`)
b.jsonw.String(index)
first = false
}
if dynamicTemplates != nil && len(dynamicTemplates) > 0 {

Check failure on line 307 in bulk_indexer.go

View workflow job for this annotation

GitHub Actions / lint

should omit nil check; len() for map[string]string is defined as zero (S1009)
carsonip marked this conversation as resolved.
Show resolved Hide resolved
if !first {
b.jsonw.RawByte(',')
}
b.jsonw.RawString(`"dynamic_templates":{`)
var i int
for k, v := range dynamicTemplates {
if i > 0 {
b.jsonw.RawByte(',')
}
b.jsonw.String(k)
b.jsonw.RawByte(':')
b.jsonw.String(v)
i++
}
b.jsonw.RawByte('}')
first = false
}
b.jsonw.RawString("}}\n")
b.writer.Write(b.jsonw.Bytes())
Expand Down
Loading