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

@@ -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