custom image for meilisearch bootstrap

Former-commit-id: d77f16337c9f25cdf13cf00d7383718b82951c84
This commit is contained in:
Christian Beutel
2024-03-15 16:48:06 +01:00
parent 662a9c006d
commit c2165cab90
7 changed files with 11 additions and 97 deletions

View File

@@ -6,7 +6,6 @@ WORKDIR /
COPY go.mod ./
COPY go.sum ./
COPY migrations ./migrations
RUN tar -xvf migrations/initial_data/cities500.tar.gz -C migrations/initial_data
RUN go mod download

View File

@@ -115,11 +115,6 @@ func main() {
}
}
// bootstrap meilisearch
if err := bootstrapMeilisearch(client); err != nil {
return err
}
return nil
})

View File

@@ -1,102 +1,13 @@
package main
import (
"encoding/json"
"errors"
"io"
"log"
"os"
"github.com/meilisearch/meilisearch-go"
"github.com/pocketbase/pocketbase/models"
)
func hasIndex(index string, client *meilisearch.Client) (resp bool, err error) {
if _, err := client.GetIndex(index); err != nil {
if err.(*meilisearch.Error).StatusCode == 404 {
return false, nil
}
return false, err
}
return true, nil
}
func bootstrapMeilisearch(client *meilisearch.Client) error {
indexExists, err := hasIndex("cities500", client)
if err != nil {
return err
}
if !indexExists {
jsonFile, err := os.Open("migrations/initial_data/cities500.json")
if err != nil {
return err
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return err
}
var cities []map[string]interface{}
json.Unmarshal(byteValue, &cities)
_, err = client.CreateIndex(&meilisearch.IndexConfig{
Uid: "cities500",
PrimaryKey: "id",
})
if err != nil {
return err
}
settings := meilisearch.Settings{
SortableAttributes: []string{
"_geo",
},
FilterableAttributes: []string{
"_geo",
},
}
_, err = client.Index("cities500").UpdateSettings(&settings)
if err != nil {
return err
}
_, err = client.Index("cities500").AddDocuments(cities)
if err != nil {
return (err)
}
}
indexExists, err = hasIndex("trails", client)
if err != nil {
return err
}
if !indexExists {
_, err = client.CreateIndex(&meilisearch.IndexConfig{
Uid: "trails",
PrimaryKey: "id",
})
if err != nil {
return err
}
settings := meilisearch.Settings{
SortableAttributes: []string{
"name", "distance", "elevation_gain", "difficulty", "created",
},
FilterableAttributes: []string{
"category", "difficulty", "distance", "elevation_gain", "completed", "_geo", "public", "author",
},
}
_, err = client.Index("trails").UpdateSettings(&settings)
if err != nil {
return err
}
}
return nil
}
func indexRecord(r *models.Record, client *meilisearch.Client) error {
documents := []map[string]interface{}{
{