Files
agent/backend/internal/search/service.go

138 lines
5.6 KiB
Go

package search
import (
"strings"
"gorm.io/gorm"
"senlinai-agent/backend/internal/domain"
)
type Service struct {
db *gorm.DB
}
type Result struct {
Type string `json:"type"`
ID uint `json:"id"`
ProjectID uint `json:"project_id"`
Title string `json:"title"`
Snippet string `json:"snippet"`
}
func NewService(database *gorm.DB) *Service {
return &Service{db: database}
}
func (s *Service) Search(userID uint, query string) ([]Result, error) {
query = strings.TrimSpace(query)
if query == "" {
return []Result{}, nil
}
if s.db.Dialector.Name() == "postgres" {
return s.searchPostgres(userID, query)
}
like := "%" + query + "%"
results := []Result{}
var projects []domain.Project
if err := s.db.Where("owner_id = ? AND (name LIKE ? OR description LIKE ?)", userID, like, like).Find(&projects).Error; err != nil {
return nil, err
}
for _, project := range projects {
results = append(results, Result{Type: "project", ID: project.ID, ProjectID: project.ID, Title: project.Name, Snippet: project.Description})
}
var tasks []domain.Task
if err := s.db.Joins("JOIN projects ON projects.id = tasks.project_id").
Where("projects.owner_id = ? AND (tasks.title LIKE ? OR tasks.description LIKE ?)", userID, like, like).
Find(&tasks).Error; err != nil {
return nil, err
}
for _, task := range tasks {
results = append(results, Result{Type: "task", ID: task.ID, ProjectID: task.ProjectID, Title: task.Title, Snippet: task.Description})
}
var notes []domain.Note
if err := s.db.Joins("JOIN projects ON projects.id = notes.project_id").
Where("projects.owner_id = ? AND (notes.title LIKE ? OR notes.markdown LIKE ?)", userID, like, like).
Find(&notes).Error; err != nil {
return nil, err
}
for _, note := range notes {
results = append(results, Result{Type: "note", ID: note.ID, ProjectID: note.ProjectID, Title: note.Title, Snippet: note.Markdown})
}
var sources []domain.Source
if err := s.db.Joins("JOIN projects ON projects.id = sources.project_id").
Where("projects.owner_id = ? AND (sources.title LIKE ? OR sources.url LIKE ? OR sources.content_text LIKE ?)", userID, like, like, like).
Find(&sources).Error; err != nil {
return nil, err
}
for _, source := range sources {
results = append(results, Result{Type: "source", ID: source.ID, ProjectID: source.ProjectID, Title: source.Title, Snippet: source.ContentText})
}
var inboxItems []domain.InboxItem
if err := s.db.Joins("JOIN projects ON projects.id = inbox_items.project_id").
Where("projects.owner_id = ? AND (inbox_items.title LIKE ? OR inbox_items.body LIKE ?)", userID, like, like).
Find(&inboxItems).Error; err != nil {
return nil, err
}
for _, item := range inboxItems {
results = append(results, Result{Type: "inbox", ID: item.ID, ProjectID: item.ProjectID, Title: item.Title, Snippet: item.Body})
}
var sessions []domain.AISession
if err := s.db.Joins("JOIN projects ON projects.id = ai_sessions.project_id").
Where("projects.owner_id = ? AND (ai_sessions.title LIKE ? OR ai_sessions.context LIKE ?)", userID, like, like).
Find(&sessions).Error; err != nil {
return nil, err
}
for _, session := range sessions {
results = append(results, Result{Type: "ai_session", ID: session.ID, ProjectID: session.ProjectID, Title: session.Title, Snippet: session.Context})
}
return results, nil
}
func (s *Service) searchPostgres(userID uint, query string) ([]Result, error) {
var results []Result
err := s.db.Raw(`
SELECT 'project' AS type, projects.id, projects.id AS project_id, projects.name AS title, projects.description AS snippet
FROM projects
WHERE projects.owner_id = ?
AND to_tsvector('simple', coalesce(projects.name, '') || ' ' || coalesce(projects.description, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'task' AS type, tasks.id, tasks.project_id, tasks.title, tasks.description AS snippet
FROM tasks
JOIN projects ON projects.id = tasks.project_id
WHERE projects.owner_id = ?
AND to_tsvector('simple', coalesce(tasks.title, '') || ' ' || coalesce(tasks.description, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'note' AS type, notes.id, notes.project_id, notes.title, notes.markdown AS snippet
FROM notes
JOIN projects ON projects.id = notes.project_id
WHERE projects.owner_id = ?
AND to_tsvector('simple', coalesce(notes.title, '') || ' ' || coalesce(notes.markdown, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'source' AS type, sources.id, sources.project_id, sources.title, sources.content_text AS snippet
FROM sources
JOIN projects ON projects.id = sources.project_id
WHERE projects.owner_id = ?
AND to_tsvector('simple', coalesce(sources.title, '') || ' ' || coalesce(sources.url, '') || ' ' || coalesce(sources.content_text, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'inbox' AS type, inbox_items.id, inbox_items.project_id, inbox_items.title, inbox_items.body AS snippet
FROM inbox_items
JOIN projects ON projects.id = inbox_items.project_id
WHERE projects.owner_id = ?
AND to_tsvector('simple', coalesce(inbox_items.title, '') || ' ' || coalesce(inbox_items.body, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'ai_session' AS type, ai_sessions.id, ai_sessions.project_id, ai_sessions.title, ai_sessions.context AS snippet
FROM ai_sessions
JOIN projects ON projects.id = ai_sessions.project_id
WHERE projects.owner_id = ?
AND to_tsvector('simple', coalesce(ai_sessions.title, '') || ' ' || coalesce(ai_sessions.context, '')) @@ plainto_tsquery('simple', ?)
LIMIT 50
`, userID, query, userID, query, userID, query, userID, query, userID, query, userID, query).Scan(&results).Error
return results, err
}