package search import ( "context" "fmt" "tercul/internal/app/localization" "tercul/internal/domain/work" "tercul/internal/platform/log" "tercul/internal/platform/search" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/trace" ) // IndexService pushes localized snapshots into Weaviate for search type IndexService interface { IndexWork(ctx context.Context, work work.Work) error } type indexService struct { localization *localization.Service weaviate search.WeaviateWrapper tracer trace.Tracer } func NewIndexService(localization *localization.Service, weaviate search.WeaviateWrapper) IndexService { return &indexService{ localization: localization, weaviate: weaviate, tracer: otel.Tracer("search.service"), } } func (s *indexService) IndexWork(ctx context.Context, work work.Work) error { ctx, span := s.tracer.Start(ctx, "IndexWork") defer span.End() logger := log.FromContext(ctx).With("work_id", work.ID) logger.Debug("Indexing work") // Get content from translation service content, err := s.localization.Queries.GetWorkContent(ctx, work.ID, work.Language) if err != nil { logger.Error(err, "Failed to get work content for indexing") // We can choose to index without content or return an error. // For now, we'll log the error and continue indexing with empty content. content = "" } err = s.weaviate.IndexWork(ctx, &work, content) if err != nil { logger.Error(err, "Failed to index work in Weaviate") return err } logger.Info("Successfully indexed work") return nil } func formatID(id uint) string { return fmt.Sprintf("%d", id) }