map speed improvements

This commit is contained in:
Christian Beutel
2025-02-10 18:10:14 +01:00
parent 3173be8d79
commit 550e3daa43
10 changed files with 207 additions and 94 deletions

View File

@@ -59,10 +59,10 @@ func registerMigrations(app *pocketbase.PocketBase) {
func setupEventHandlers(app *pocketbase.PocketBase, client meilisearch.ServiceManager) {
app.OnModelAfterCreate("users").Add(createUserHandler(app, client))
app.OnModelAfterCreate("trails").Add(createTrailIndexHandler(client))
app.OnModelAfterCreate("trails").Add(createTrailIndexHandler(app, client))
app.OnRecordAfterCreateRequest("trails").Add(createTrailHandler(app))
app.OnRecordAfterUpdateRequest("trails").Add(updateTrailHandler(client))
app.OnRecordAfterUpdateRequest("trails").Add(updateTrailHandler(app, client))
app.OnRecordAfterDeleteRequest("trails").Add(deleteTrailHandler(client))
app.OnRecordAfterCreateRequest("trail_share").Add(createTrailShareHandler(app, client))
@@ -128,10 +128,14 @@ func createDefaultUserSettings(app *pocketbase.PocketBase, userId string) error
return app.Dao().SaveRecord(settings)
}
func createTrailIndexHandler(client meilisearch.ServiceManager) func(e *core.ModelEvent) error {
func createTrailIndexHandler(app *pocketbase.PocketBase, client meilisearch.ServiceManager) func(e *core.ModelEvent) error {
return func(e *core.ModelEvent) error {
record := e.Model.(*models.Record)
if err := util.IndexTrail(record, client); err != nil {
author, err := app.Dao().FindRecordById("users", record.GetString(("author")))
if err != nil {
return err
}
if err := util.IndexTrail(record, author, client); err != nil {
return err
}
return nil
@@ -156,9 +160,13 @@ func createTrailHandler(app *pocketbase.PocketBase) func(e *core.RecordCreateEve
}
}
func updateTrailHandler(client meilisearch.ServiceManager) func(e *core.RecordUpdateEvent) error {
func updateTrailHandler(app *pocketbase.PocketBase, client meilisearch.ServiceManager) func(e *core.RecordUpdateEvent) error {
return func(e *core.RecordUpdateEvent) error {
return util.UpdateTrail(e.Record, client)
author, err := app.Dao().FindRecordById("users", e.Record.GetString(("author")))
if err != nil {
return err
}
return util.UpdateTrail(e.Record, author, client)
}
}
@@ -727,8 +735,13 @@ func bootstrapMeilisearchTrails(app *pocketbase.PocketBase, client meilisearch.S
return err
}
client.Index("trails").DeleteAllDocuments()
for _, trail := range trails {
if err := util.UpdateTrail(trail, client); err != nil {
author, err := app.Dao().FindRecordById("users", trail.GetString(("author")))
if err != nil {
return err
}
if err := util.IndexTrail(trail, author, client); err != nil {
return err
}
}

View File

@@ -8,10 +8,18 @@ import (
"github.com/pocketbase/pocketbase/models"
)
func documentFromTrailRecord(r *models.Record, includeShares bool) map[string]interface{} {
func documentFromTrailRecord(r *models.Record, author *models.Record, includeShares bool) map[string]interface{} {
photos := r.GetStringSlice("photos")
thumbnail := ""
if len(photos) > 0 {
thumbnail = r.GetStringSlice("photos")[r.GetInt("thumbnail")]
}
document := map[string]interface{}{
"id": r.Id,
"author": r.GetString("author"),
"author_name": author.GetString("username"),
"author_avatar": author.GetString("avatar"),
"name": r.GetString("name"),
"description": r.GetString("description"),
"location": r.GetString("location"),
@@ -25,6 +33,8 @@ func documentFromTrailRecord(r *models.Record, includeShares bool) map[string]in
"date": r.GetDateTime("date").Time().Unix(),
"created": r.GetDateTime("created").Time().Unix(),
"public": r.GetBool("public"),
"thumbnail": thumbnail,
"gpx": r.GetString("gpx"),
"_geo": map[string]float64{
"lat": r.GetFloat("lat"),
"lng": r.GetFloat("lon"),
@@ -56,8 +66,8 @@ func documentFromListRecord(r *models.Record, includeShares bool) map[string]int
return document
}
func IndexTrail(r *models.Record, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{documentFromTrailRecord(r, true)}
func IndexTrail(r *models.Record, author *models.Record, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{documentFromTrailRecord(r, author, true)}
if _, err := client.Index("trails").AddDocuments(documents); err != nil {
return err
@@ -66,8 +76,8 @@ func IndexTrail(r *models.Record, client meilisearch.ServiceManager) error {
return nil
}
func UpdateTrail(r *models.Record, client meilisearch.ServiceManager) error {
documents := documentFromTrailRecord(r, false)
func UpdateTrail(r *models.Record, author *models.Record, client meilisearch.ServiceManager) error {
documents := documentFromTrailRecord(r, author, false)
if _, err := client.Index("trails").UpdateDocuments(documents); err != nil {
return err