mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34:40 +00:00
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:
@@ -0,0 +1,11 @@
|
||||
-- Enable iterative index scans for HNSW so filtered queries don't fall back to seq scan
|
||||
SET hnsw.iterative_scan = relaxed_order;
|
||||
|
||||
-- CreateIndex: HNSW index on asset_embeddings.embedding
|
||||
-- Uses cosine distance ops (suited for normalized OpenAI-style embeddings)
|
||||
-- m=16: controls graph connectivity (higher = better recall, more memory)
|
||||
-- ef_construction=64: build-time search width (higher = better quality, slower build)
|
||||
CREATE INDEX "AssetEmbedding_embedding_idx"
|
||||
ON "asset_embeddings"
|
||||
USING hnsw (embedding vector_cosine_ops)
|
||||
WITH (m = 16, ef_construction = 64);
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
-- Create ICU numeric collation for natural sort (numbers sort by value, not lexicographically)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_collation WHERE collname = 'numeric') THEN
|
||||
CREATE COLLATION numeric (provider = icu, locale = 'en@colNumeric=yes');
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Alter assets.name to use the numeric collation
|
||||
-- This makes all ORDER BY name queries (ORM and raw SQL) use natural sort automatically
|
||||
ALTER TABLE "assets" ALTER COLUMN "name" TYPE TEXT COLLATE numeric;
|
||||
|
||||
-- CreateIndex: B-tree index on name using the numeric collation
|
||||
-- Supports efficient ORDER BY name ASC/DESC without seq scans
|
||||
CREATE INDEX "assets_name_idx" ON "assets" ("name" COLLATE numeric);
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "postgresql"
|
||||
@@ -406,6 +406,7 @@ model Asset {
|
||||
@@index([sortIndex])
|
||||
@@index([isDeleted])
|
||||
@@index([deletedAt])
|
||||
@@index([name], name: "assets_name_idx")
|
||||
@@index([nameNgram], type: Gin)
|
||||
@@unique([parentId, targetId], name: "parentIdTargetId")
|
||||
}
|
||||
@@ -494,6 +495,7 @@ model AssetEmbedding {
|
||||
assetId String @map("asset_id")
|
||||
asset Asset @relation("AssetToEmbedding", fields: [assetId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([embedding], name: "AssetEmbedding_embedding_idx")
|
||||
@@map("asset_embeddings")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user