Skip to content

Commit

Permalink
tmpで置き換える
Browse files Browse the repository at this point in the history
  • Loading branch information
kounoike committed Apr 30, 2023
1 parent ee64903 commit a095b3e
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions meili/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type MeiliSearchClient struct {
}

const (
programIndexName = "program"
recordedFileIndexName = "recorded_file"
maxDocumentsNum = 200
programIndexName = "program"
recordedFileIndexName = "recorded_file"
temporarilyRecordedFileIndexName = "recorded_file_tmp"
maxDocumentsNum = 200
)

func NewMeiliSearchClient(logger *zap.Logger, host string, port int, transcribedBasePath string) *MeiliSearchClient {
Expand Down Expand Up @@ -118,7 +119,7 @@ func max(a, b int) int {
}

func (m *MeiliSearchClient) UpdateRecordedFiles(rows []db.ListRecordedFilesRow) error {
index := m.Index(recordedFileIndexName)
tmpIndex := m.Index(temporarilyRecordedFileIndexName)

documents := make([]map[string]interface{}, 0, len(rows))
for idx, row := range rows {
Expand Down Expand Up @@ -162,10 +163,36 @@ func (m *MeiliSearchClient) UpdateRecordedFiles(rows []db.ListRecordedFilesRow)

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

_, err := index.UpdateDocumentsInBatches(documents, maxDocumentsNum)
resp, err := tmpIndex.UpdateDocumentsInBatches(documents, maxDocumentsNum)
if err != nil {
m.logger.Warn("failed to update documents", zap.Error(err), zap.Any("documents", documents))
}
for _, taskInfo := range resp {
_, err := m.client.WaitForTask(taskInfo.TaskUID)
if err != nil {
m.logger.Warn("failed to wait for task", zap.Error(err))
}
}
taskInfo, err := m.client.SwapIndexes([]meilisearch.SwapIndexesParams{
{
Indexes: []string{recordedFileIndexName, temporarilyRecordedFileIndexName},
},
})
if err != nil {
m.logger.Warn("failed to swap indexes", zap.Error(err))
}
_, err = m.client.WaitForTask(taskInfo.TaskUID)
if err != nil {
m.logger.Warn("failed to wait for task", zap.Error(err))
}
delTaskInfo, err := m.client.DeleteIndex(temporarilyRecordedFileIndexName)
if err != nil {
m.logger.Warn("failed to delete index", zap.Error(err))
}
_, err = m.client.WaitForTask(delTaskInfo.TaskUID)
if err != nil {
m.logger.Warn("failed to wait for task", zap.Error(err))
}

return nil
}
Expand Down

0 comments on commit a095b3e

Please sign in to comment.