|
1 | 1 | import { db } from '@sim/db' |
2 | 2 | import { document, embedding } from '@sim/db/schema' |
3 | | -import { createLogger } from '@sim/logger' |
4 | 3 | import { and, eq, inArray, isNull, sql } from 'drizzle-orm' |
5 | 4 | import type { StructuredFilter } from '@/lib/knowledge/types' |
6 | 5 |
|
7 | | -const logger = createLogger('KnowledgeSearchUtils') |
8 | | - |
9 | 6 | export async function getDocumentNamesByIds( |
10 | 7 | documentIds: string[] |
11 | 8 | ): Promise<Record<string, string>> { |
@@ -140,17 +137,12 @@ function buildFilterCondition(filter: StructuredFilter, embeddingTable: any) { |
140 | 137 | const { tagSlot, fieldType, operator, value, valueTo } = filter |
141 | 138 |
|
142 | 139 | if (!isTagSlotKey(tagSlot)) { |
143 | | - logger.debug(`[getStructuredTagFilters] Unknown tag slot: ${tagSlot}`) |
144 | 140 | return null |
145 | 141 | } |
146 | 142 |
|
147 | 143 | const column = embeddingTable[tagSlot] |
148 | 144 | if (!column) return null |
149 | 145 |
|
150 | | - logger.debug( |
151 | | - `[getStructuredTagFilters] Processing ${tagSlot} (${fieldType}) ${operator} ${value}` |
152 | | - ) |
153 | | - |
154 | 146 | // Handle text operators |
155 | 147 | if (fieldType === 'text') { |
156 | 148 | const stringValue = String(value) |
@@ -208,7 +200,6 @@ function buildFilterCondition(filter: StructuredFilter, embeddingTable: any) { |
208 | 200 | const dateStr = String(value) |
209 | 201 | // Validate YYYY-MM-DD format |
210 | 202 | if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) { |
211 | | - logger.debug(`[getStructuredTagFilters] Invalid date format: ${dateStr}, expected YYYY-MM-DD`) |
212 | 203 | return null |
213 | 204 | } |
214 | 205 |
|
@@ -287,9 +278,6 @@ function getStructuredTagFilters(filters: StructuredFilter[], embeddingTable: an |
287 | 278 | conditions.push(slotConditions[0]) |
288 | 279 | } else { |
289 | 280 | // Multiple conditions for same slot - OR them together |
290 | | - logger.debug( |
291 | | - `[getStructuredTagFilters] OR'ing ${slotConditions.length} conditions for ${slot}` |
292 | | - ) |
293 | 281 | conditions.push(sql`(${sql.join(slotConditions, sql` OR `)})`) |
294 | 282 | } |
295 | 283 | } |
@@ -380,8 +368,6 @@ export async function handleTagOnlySearch(params: SearchParams): Promise<SearchR |
380 | 368 | throw new Error('Tag filters are required for tag-only search') |
381 | 369 | } |
382 | 370 |
|
383 | | - logger.debug(`[handleTagOnlySearch] Executing tag-only search with filters:`, structuredFilters) |
384 | | - |
385 | 371 | const strategy = getQueryStrategy(knowledgeBaseIds.length, topK) |
386 | 372 | const tagFilterConditions = getStructuredTagFilters(structuredFilters, embedding) |
387 | 373 |
|
@@ -431,8 +417,6 @@ export async function handleVectorOnlySearch(params: SearchParams): Promise<Sear |
431 | 417 | throw new Error('Query vector and distance threshold are required for vector-only search') |
432 | 418 | } |
433 | 419 |
|
434 | | - logger.debug(`[handleVectorOnlySearch] Executing vector-only search`) |
435 | | - |
436 | 420 | const strategy = getQueryStrategy(knowledgeBaseIds.length, topK) |
437 | 421 |
|
438 | 422 | const distanceExpr = sql<number>`${embedding.embedding} <=> ${queryVector}::vector`.as('distance') |
@@ -489,23 +473,13 @@ export async function handleTagAndVectorSearch(params: SearchParams): Promise<Se |
489 | 473 | throw new Error('Query vector and distance threshold are required for tag and vector search') |
490 | 474 | } |
491 | 475 |
|
492 | | - logger.debug( |
493 | | - `[handleTagAndVectorSearch] Executing tag + vector search with filters:`, |
494 | | - structuredFilters |
495 | | - ) |
496 | | - |
497 | 476 | // Step 1: Filter by tags first |
498 | 477 | const tagFilteredIds = await executeTagFilterQuery(knowledgeBaseIds, structuredFilters) |
499 | 478 |
|
500 | 479 | if (tagFilteredIds.length === 0) { |
501 | | - logger.debug(`[handleTagAndVectorSearch] No results found after tag filtering`) |
502 | 480 | return [] |
503 | 481 | } |
504 | 482 |
|
505 | | - logger.debug( |
506 | | - `[handleTagAndVectorSearch] Found ${tagFilteredIds.length} results after tag filtering` |
507 | | - ) |
508 | | - |
509 | 483 | // Step 2: Perform vector search only on tag-filtered results |
510 | 484 | return await executeVectorSearchOnIds( |
511 | 485 | tagFilteredIds.map((r) => r.id), |
|
0 commit comments