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

fix: Elasticsearch: Request Entity Too Large #28117 #29062

Merged
merged 10 commits into from
Feb 7, 2024
36 changes: 31 additions & 5 deletions modules/indexer/code/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,38 @@ func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha st
reqs = append(reqs, b.addDelete(filename, repo))
}

// Helper function to support Go v1.2 and below
inferno-umar marked this conversation as resolved.
Show resolved Hide resolved
min := func(a, b int) int {
if a <= b {
return a
}
return b
}

if len(reqs) > 0 {
_, err := b.inner.Client.Bulk().
Index(b.inner.VersionedIndexName()).
Add(reqs...).
Do(ctx)
return err
esBatchSize := 12 // Hardcoded batch size for ElasticSearch index update
var batchHead int

maxPerBatchReqCount := len(reqs)/esBatchSize + (len(reqs) % esBatchSize)
inferno-umar marked this conversation as resolved.
Show resolved Hide resolved

for i := 0; i < esBatchSize; i++ {
// Taking in another variable because (*elastic.BulkService).Do(ctx context.Context) clears out the requests slice upon successful batch
bulkReq := reqs[batchHead:min(batchHead+maxPerBatchReqCount, len(reqs))]

if len(bulkReq) > 0 {
bulkService := b.inner.Client.Bulk().
Index(b.inner.VersionedIndexName()).
Add(bulkReq...)

_, err := bulkService.
Do(ctx)
if err != nil {
return err
}
}

batchHead += maxPerBatchReqCount
}
}
return nil
}
Expand Down
Loading