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

1
.gitignore vendored
View File

@@ -3,6 +3,5 @@
search/meilisearch
search/data.ms
search/dumps
run.sh

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{}{
{

View File

@@ -7,7 +7,7 @@ x-common-env: &cenv
services:
search:
container_name: wanderer-search
image: getmeili/meilisearch:v1.6.2
build: ./search
environment:
<<: *cenv
MEILI_NO_ANALYTICS: true

10
search/Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM getmeili/meilisearch:v1.6.2
COPY ./migrations/migration.dump /meili_data/dumps/migration.dump
COPY ./run.sh /run.sh
RUN chmod +x /run.sh
CMD [ "/run.sh" ]

Binary file not shown.