feat(db): add HNSW index on asset_embeddings and natural sort collation on assets.name

- Add HNSW index on asset_embeddings.embedding using vector_cosine_ops (m=16,
  ef_construction=64) with hnsw.iterative_scan=relaxed_order for efficient
  filtered ANN queries
- Create ICU numeric collation and alter assets.name column to use it for
  natural sort (file2 < file10 instead of lexicographic ordering)
- Add B-tree index on assets.name with numeric collation for sort performance
- Add schema @@index annotations to match migration index names
- Add 7 tests verifying natural sort behaviour in both AssetService.listChildren
  and SearchService.search (Prisma ORM and raw-SQL paths)
This commit is contained in:
yiling
2026-05-29 15:18:31 +08:00
parent 840cf87728
commit 048028007e
6 changed files with 234 additions and 0 deletions
+97
View File
@@ -735,3 +735,100 @@ describe('SearchService', () => {
})
})
})
describe('SearchService — natural sort by name', () => {
setupTestDbHooks()
let searchService: SearchService
beforeEach(() => {
searchService = new SearchService()
})
const setupSearchNaturalSortAssets = async (names: string[]) => {
const user = await prisma.user.create({
data: {
name: 'search-natural-sort-user',
email: `search-natural-sort-${Date.now()}@example.com`,
type: 'human',
},
})
const team = await prisma.team.create({ data: { name: 'search-natural-sort-team' } })
const project = await prisma.project.create({
data: { name: 'search-natural-sort-proj', teamId: team.id },
})
const root = await prisma.asset.create({
data: {
name: 'root',
type: AssetType.folder,
projectId: project.id,
creatorId: user.id,
status: 'uploaded',
},
})
for (const name of names) {
await prisma.asset.create({
data: {
name,
type: AssetType.file,
projectId: project.id,
parentId: root.id,
creatorId: user.id,
status: 'uploaded',
sizeByte: 0,
},
})
}
return { root }
}
it('returns results in natural name order (asc) via raw-SQL sort path', async () => {
const { root } = await setupSearchNaturalSortAssets(['clip10', 'clip2', 'clip20', 'clip1'])
const result = await searchService.search(root.id, {
recursively: false,
assetType: 'file',
operator: 'AND',
conditions: [],
isSemantic: false,
sort: { field: 'name', order: 'asc' },
})
expect(result.data.map((a) => a.name)).toEqual(['clip1', 'clip2', 'clip10', 'clip20'])
})
it('returns results in natural name order (desc) via raw-SQL sort path', async () => {
const { root } = await setupSearchNaturalSortAssets(['clip10', 'clip2', 'clip20', 'clip1'])
const result = await searchService.search(root.id, {
recursively: false,
assetType: 'file',
operator: 'AND',
conditions: [],
isSemantic: false,
sort: { field: 'name', order: 'desc' },
})
expect(result.data.map((a) => a.name)).toEqual(['clip20', 'clip10', 'clip2', 'clip1'])
})
it('sorts mixed alpha-numeric names naturally via search', async () => {
const { root } = await setupSearchNaturalSortAssets([
'scene 100',
'scene 9',
'scene 10',
'scene 2',
])
const result = await searchService.search(root.id, {
recursively: false,
assetType: 'file',
operator: 'AND',
conditions: [],
isSemantic: false,
sort: { field: 'name', order: 'asc' },
})
expect(result.data.map((a) => a.name)).toEqual(['scene 2', 'scene 9', 'scene 10', 'scene 100'])
})
})