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
+105
View File
@@ -1482,3 +1482,108 @@ describe('AssetService', () => {
})
})
})
describe('AssetService — natural sort by name', () => {
setupTestDbHooks()
let assetService: AssetService
beforeEach(() => {
assetService = new AssetService()
})
const setupNaturalSortAssets = async (names: string[]) => {
const user = await prisma.user.create({
data: {
name: 'natural-sort-user',
email: `natural-sort-${Date.now()}@example.com`,
type: 'human',
},
})
const team = await prisma.team.create({ data: { name: 'natural-sort-team' } })
const project = await prisma.project.create({
data: { name: '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',
},
})
// Create files in a shuffled order so we are not relying on insertion order
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('sorts files with numeric suffixes in natural order (asc)', async () => {
// Insert in non-natural lexicographic order to prove the collation does the work
const { root } = await setupNaturalSortAssets(['file10', 'file2', 'file20', 'file1'])
const result = await assetService.listChildren({
assetId: root.id,
assetType: AssetType.file,
sort: 'name',
order: 'asc',
first: 20,
})
expect(result.data.map((a) => a.name)).toEqual(['file1', 'file2', 'file10', 'file20'])
})
it('sorts files with numeric suffixes in natural order (desc)', async () => {
const { root } = await setupNaturalSortAssets(['file10', 'file2', 'file20', 'file1'])
const result = await assetService.listChildren({
assetId: root.id,
assetType: AssetType.file,
sort: 'name',
order: 'desc',
first: 20,
})
expect(result.data.map((a) => a.name)).toEqual(['file20', 'file10', 'file2', 'file1'])
})
it('sorts mixed alpha-numeric names correctly', async () => {
const { root } = await setupNaturalSortAssets(['asset 100', 'asset 9', 'asset 10', 'asset 2'])
const result = await assetService.listChildren({
assetId: root.id,
assetType: AssetType.file,
sort: 'name',
order: 'asc',
first: 20,
})
expect(result.data.map((a) => a.name)).toEqual(['asset 2', 'asset 9', 'asset 10', 'asset 100'])
})
it('does not regress lexicographic ordering for purely alphabetical names', async () => {
const { root } = await setupNaturalSortAssets(['banana', 'apple', 'cherry', 'apricot'])
const result = await assetService.listChildren({
assetId: root.id,
assetType: AssetType.file,
sort: 'name',
order: 'asc',
first: 20,
})
expect(result.data.map((a) => a.name)).toEqual(['apple', 'apricot', 'banana', 'cherry'])
})
})
+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'])
})
})