Skip to content

Commit

Permalink
Batchでやってみる
Browse files Browse the repository at this point in the history
  • Loading branch information
kounoike committed Apr 30, 2023
1 parent 4225fa6 commit ee64903
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions meili/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func max(a, b int) int {
func (m *MeiliSearchClient) UpdateRecordedFiles(rows []db.ListRecordedFilesRow) error {
index := m.Index(recordedFileIndexName)

documents := make([]map[string]interface{}, 0, max(len(rows), maxDocumentsNum))
documents := make([]map[string]interface{}, 0, len(rows))
for idx, row := range rows {
document := map[string]interface{}{
"id": row.ProgramID,
Expand Down Expand Up @@ -155,19 +155,14 @@ func (m *MeiliSearchClient) UpdateRecordedFiles(rows []db.ListRecordedFilesRow)
}
}
documents = append(documents, document)
if len(documents) == maxDocumentsNum {
if idx%500 == 0 {
m.logger.Info(fmt.Sprintf("%d/%d 番組 準備完了...", idx+1, len(rows)))
_, err := index.UpdateDocuments(documents)
if err != nil {
m.logger.Warn("failed to update documents", zap.Error(err), zap.Any("documents", documents))
}
documents = make([]map[string]interface{}, 0, maxDocumentsNum)
}
}

m.logger.Info(fmt.Sprintf("%d/%d 番組 準備完了...", len(rows), len(rows)))

_, err := index.UpdateDocuments(documents)
_, err := index.UpdateDocumentsInBatches(documents, maxDocumentsNum)
if err != nil {
m.logger.Warn("failed to update documents", zap.Error(err), zap.Any("documents", documents))
}
Expand Down

0 comments on commit ee64903

Please sign in to comment.