-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (161 loc) · 5.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"context"
"flag"
"fmt"
"os"
"sync"
"time"
"github.com/georgysavva/scany/v2/pgxscan"
log "github.com/sirupsen/logrus"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/meilisearch/meilisearch-go"
)
var ctx = context.Background()
func main() {
drop := flag.Bool("drop", false, "Drop the index (destination name)")
meta := flag.Bool("meta", false, "Re-create the index metadata")
only := flag.String("only", "", "Only synchronize one index by its name")
configPath := flag.String("config", "config.yml", "Path to the config file")
flag.Parse()
var config Config
if err := config.Parse(*configPath); err != nil {
log.WithError(err).Fatalf("Failed to parse config")
}
ms := meilisearch.New(os.Getenv("MEILISEARCH_DSN"), meilisearch.WithAPIKey(os.Getenv("MEILISEARCH_API_KEY")))
if !ms.IsHealthy() {
log.Fatal("MeiliSearch is not healthy")
}
pg, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.WithError(err).Fatal("Failed to connect to Postgres")
}
for idx, index := range config.Indexes {
if *only != "" && index.Destination != *only {
log.Printf("Skipping index %s", index.Destination)
continue
}
log.Printf("Synchronizing index %s", index.Destination)
start := time.Now()
if index.Cursor.Column != "" && *drop {
log.WithFields(log.Fields{
"index": index.Destination,
}).Warn("Dropping index, cursor will be reset")
index.Cursor.LastSync = time.Time{}
}
if index.Cursor.Column != "" && index.Cursor.LastSync.IsZero() {
log.WithFields(log.Fields{
"index": index.Destination,
}).Warn("Cursor is at zero, indexing all documents")
}
indexMs, err := MakeIndex(index, *drop, *meta, ms)
if err != nil {
log.WithError(err).WithField("index", index.Destination).Error("Failed to make index")
continue
}
total, err := SyncIndex(config, pg, index, func(batch []map[string]interface{}) error {
_, err := indexMs.AddDocuments(batch)
if err != nil {
return err
}
log.WithField("batch", len(batch)).Info("Added batch to index")
return nil
})
if err != nil {
log.WithError(err).WithField("index", index.Destination).Error("Failed to sync index")
continue
}
if index.Cursor.Column != "" {
config.Indexes[idx].Cursor.LastSync = time.Now()
log.WithFields(log.Fields{
"index": index.Destination,
"time": config.Indexes[idx].Cursor.LastSync,
}).Info("Updated cursor")
}
log.WithFields(log.Fields{
"total": total,
"index": index.Destination,
"time": time.Since(start),
}).Info("Index synchronized")
}
if err := config.Save(*configPath); err != nil {
log.WithError(err).Error("Failed to save config")
}
log.Println("Done indexing")
}
// MakeIndex creates a new index in MeiliSearch and returns the index manager.
func MakeIndex(config IndexConfig, drop bool, meta bool, ms meilisearch.ServiceManager) (meilisearch.IndexManager, error) {
idx := ms.Index(config.Destination)
_, err := idx.GetStats()
if drop || err != nil {
if _, err := ms.DeleteIndex(config.Destination); err != nil {
return nil, err
}
if _, err := ms.CreateIndex(&meilisearch.IndexConfig{
Uid: config.Destination,
PrimaryKey: config.Primary,
}); err != nil {
return nil, err
}
log.WithField("index", config.Destination).Info("Created index")
}
stats, err := idx.GetStats()
if meta || err != nil || stats.NumberOfDocuments == 0 {
if _, err := idx.UpdateFilterableAttributes(&config.Filterable); err != nil {
return nil, err
}
if _, err := idx.UpdateSortableAttributes(&config.Sortable); err != nil {
return nil, err
}
if _, err := idx.UpdateSearchableAttributes(&config.Searchable); err != nil {
return nil, err
}
log.WithField("index", config.Destination).Info("Updated index metadata")
}
return idx, nil
}
// SyncIndex synchronizes the index in Postgres with the index in MeiliSearch.
func SyncIndex(config Config, pg *pgxpool.Pool, index IndexConfig, onBatch func([]map[string]interface{}) error) (int, error) {
query := fmt.Sprintf("SELECT * FROM %s", index.Source)
if index.Cursor.Column != "" {
query += fmt.Sprintf(" WHERE %s > '%s'", index.Cursor.Column, index.Cursor.LastSync.Format(time.RFC3339))
}
query += " ORDER BY " + index.SourcePrimary
total := 0
offset := 0
wg := sync.WaitGroup{}
for {
rows, err := pg.Query(ctx, fmt.Sprintf("%s LIMIT %d OFFSET %d", query, config.BatchSize, offset))
if err != nil {
return total, err
}
batch := []map[string]interface{}{}
for rows.Next() {
var row map[string]interface{}
if err := pgxscan.ScanRow(&row, rows); err != nil {
return total, err
}
batch = append(batch, row)
}
total += len(batch)
if len(batch) == 0 {
break
}
wg.Add(1)
go func() {
defer wg.Done()
if err := onBatch(batch); err != nil {
log.WithError(err).Error("Failed to add batch to index")
}
}()
if !config.EnableAsync {
wg.Wait()
}
offset += config.BatchSize
if !config.EnableAsync && config.WaitTime > 0 {
time.Sleep(time.Duration(config.WaitTime) * time.Millisecond)
}
}
wg.Wait()
return total, nil
}