fix(repository): make message keyword search work on SQLite and MySQL
SearchMessagesByKeyword filtered with `messages.content ILIKE ?`, which is PostgreSQL-only syntax. On SQLite (the Lite build) and MySQL the query fails with `near "ILIKE": syntax error`, so POST /api/v1/messages/search returned HTTP 500 in both keyword and the default hybrid modes. The session-list search (QueryPaged) was already made dialect-aware for the same reason; this path was missed. Pick the LIKE expression per dialect — ILIKE on Postgres, LOWER() LIKE LOWER() elsewhere — and add an explicit `ESCAPE ?` bound to likeEscapeChar so escapeLikeKeyword's backslash escapes are honored on SQLite, which has no default LIKE escape character (a no-op on Postgres/MySQL, where `\` is already the default). Regression tests in message_search_test.go run against in-memory SQLite: TestSearchMessagesByKeywordMatchesOnSQLite and TestSearchMessagesByKeywordTreatsWildcardsLiterally both fail on the old code with the ILIKE syntax error and pass with the fix. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CRrBenkCCr4pacMNwR4NuGmain
parent
e80f5dfbc7
commit
0ad08d41be
|
|
@ -180,7 +180,7 @@ func (r *messageRepository) GetMessageByRequestID(
|
|||
return &message, nil
|
||||
}
|
||||
|
||||
// SearchMessagesByKeyword searches messages by keyword (ILIKE) across sessions for a tenant
|
||||
// SearchMessagesByKeyword searches messages by keyword across sessions for a tenant
|
||||
func (r *messageRepository) SearchMessagesByKeyword(
|
||||
ctx context.Context, tenantID uint64, ownerID, keyword string, sessionIDs []string, limit int,
|
||||
) ([]*types.MessageWithSession, error) {
|
||||
|
|
@ -188,6 +188,15 @@ func (r *messageRepository) SearchMessagesByKeyword(
|
|||
limit = 20
|
||||
}
|
||||
|
||||
// ILIKE is Postgres-only; SQLite (Lite build) and MySQL lack the keyword,
|
||||
// so mirror the session-list search with LOWER() on both sides. ESCAPE ?
|
||||
// pairs with escapeLikeKeyword: SQLite has no default LIKE escape
|
||||
// character, so without it \% and \_ would never match a literal wildcard.
|
||||
contentLikeExpr := "LOWER(messages.content) LIKE LOWER(?) ESCAPE ?"
|
||||
if r.db.Name() == "postgres" {
|
||||
contentLikeExpr = "messages.content ILIKE ? ESCAPE ?"
|
||||
}
|
||||
|
||||
var results []*types.MessageWithSession
|
||||
|
||||
query := r.db.WithContext(ctx).
|
||||
|
|
@ -196,7 +205,7 @@ func (r *messageRepository) SearchMessagesByKeyword(
|
|||
Joins("INNER JOIN sessions ON sessions.id = messages.session_id AND sessions.deleted_at IS NULL").
|
||||
Where("sessions.tenant_id = ?", tenantID).
|
||||
Where("messages.deleted_at IS NULL").
|
||||
Where("messages.content ILIKE ?", "%"+escapeLikeKeyword(keyword)+"%")
|
||||
Where(contentLikeExpr, "%"+escapeLikeKeyword(keyword)+"%", likeEscapeChar)
|
||||
|
||||
// Matches the scoping used when listing sessions, including the legacy
|
||||
// allowance for tenant-level sessions created before per-user ownership.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SearchMessagesByKeyword runs on every supported main-database dialect —
|
||||
// the Lite build serves POST /messages/search from SQLite. The query used to
|
||||
// be Postgres-only ILIKE, which is a syntax error everywhere else.
|
||||
|
||||
func newMessageSearchDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&types.Session{}, &types.Message{}))
|
||||
return db
|
||||
}
|
||||
|
||||
func TestSearchMessagesByKeywordMatchesOnSQLite(t *testing.T) {
|
||||
db := newMessageSearchDB(t)
|
||||
repo := NewMessageRepository(db)
|
||||
|
||||
session := &types.Session{TenantID: 7, UserID: "web_user:alice", Title: "chat"}
|
||||
require.NoError(t, db.Create(session).Error)
|
||||
require.NoError(t, db.Create(&types.Message{
|
||||
SessionID: session.ID,
|
||||
Role: "user",
|
||||
Content: "How do I reset the DEVICE password?",
|
||||
}).Error)
|
||||
|
||||
results, err := repo.SearchMessagesByKeyword(
|
||||
context.Background(), 7, "web_user:alice", "device", nil, 10)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
require.Equal(t, session.ID, results[0].SessionID)
|
||||
}
|
||||
|
||||
func TestSearchMessagesByKeywordTreatsWildcardsLiterally(t *testing.T) {
|
||||
db := newMessageSearchDB(t)
|
||||
repo := NewMessageRepository(db)
|
||||
|
||||
session := &types.Session{TenantID: 7, UserID: "web_user:alice", Title: "chat"}
|
||||
require.NoError(t, db.Create(session).Error)
|
||||
require.NoError(t, db.Create(&types.Message{
|
||||
SessionID: session.ID,
|
||||
Role: "user",
|
||||
Content: "the discount is 100% today",
|
||||
}).Error)
|
||||
require.NoError(t, db.Create(&types.Message{
|
||||
SessionID: session.ID,
|
||||
Role: "user",
|
||||
Content: "no percent sign here",
|
||||
}).Error)
|
||||
|
||||
// A keyword containing % must match the literal character, not act as a
|
||||
// LIKE wildcard. escapeLikeKeyword emits \%, which only works when the
|
||||
// query declares ESCAPE '\' — SQLite has no default escape character.
|
||||
results, err := repo.SearchMessagesByKeyword(
|
||||
context.Background(), 7, "web_user:alice", "100%", nil, 10)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
require.Contains(t, results[0].Content, "100%")
|
||||
}
|
||||
|
|
@ -553,7 +553,7 @@ func (s *messageService) SearchMessages(ctx context.Context, params *types.Messa
|
|||
var vectorResults []*types.MessageSearchResultItem
|
||||
var err error
|
||||
|
||||
// Step 1: Keyword search (direct PG ILIKE)
|
||||
// Step 1: Keyword search (dialect-aware LIKE in the repository)
|
||||
if params.Mode == types.MessageSearchModeKeyword || params.Mode == types.MessageSearchModeHybrid {
|
||||
keywordResults, err = s.messageRepo.SearchMessagesByKeyword(
|
||||
ctx, tenantID, params.OwnerID, params.Query, params.SessionIDs, params.Limit*3)
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ type MessageRepository interface {
|
|||
DeleteMessagesBySessionID(ctx context.Context, sessionID string) error
|
||||
// GetFirstMessageOfUser gets the first message of a user
|
||||
GetFirstMessageOfUser(ctx context.Context, sessionID string) (*types.Message, error)
|
||||
// SearchMessagesByKeyword searches messages by keyword (ILIKE) across sessions for a tenant
|
||||
// SearchMessagesByKeyword searches messages by keyword across sessions for a tenant
|
||||
// OwnedSessionIDs narrows a set of session ids to the ones this person owns.
|
||||
OwnedSessionIDs(ctx context.Context, tenantID uint64, ownerID string, sessionIDs []string) (map[string]bool, error)
|
||||
SearchMessagesByKeyword(ctx context.Context, tenantID uint64, ownerID, keyword string, sessionIDs []string, limit int) ([]*types.MessageWithSession, error)
|
||||
|
|
|
|||
Loading…
Reference in New Issue