mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"tercul/internal/app/localization"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/search"
|
|
)
|
|
|
|
// IndexService pushes localized snapshots into Weaviate for search
|
|
type IndexService interface {
|
|
IndexWork(ctx context.Context, work domain.Work) error
|
|
}
|
|
|
|
type indexService struct {
|
|
localization localization.Service
|
|
translations domain.TranslationRepository
|
|
}
|
|
|
|
func NewIndexService(localization localization.Service, translations domain.TranslationRepository) IndexService {
|
|
return &indexService{localization: localization, translations: translations}
|
|
}
|
|
|
|
func (s *indexService) IndexWork(ctx context.Context, work domain.Work) error {
|
|
// Choose best content snapshot for indexing
|
|
content, err := s.localization.GetWorkContent(ctx, work.ID, work.Language)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
props := map[string]interface{}{
|
|
"language": work.Language,
|
|
"title": work.Title,
|
|
"description": work.Description,
|
|
"status": work.Status,
|
|
}
|
|
if content != "" {
|
|
props["content"] = content
|
|
}
|
|
|
|
_, wErr := search.Client.Data().Creator().
|
|
WithClassName("Work").
|
|
WithID(formatID(work.ID)).
|
|
WithProperties(props).
|
|
Do(ctx)
|
|
if wErr != nil {
|
|
log.Printf("weaviate index error: %v", wErr)
|
|
return wErr
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func formatID(id uint) string { return fmt.Sprintf("%d", id) }
|