@open-mercato/search 0.9.1-develop.7473.1.de5b22888f → 0.9.1-develop.7475.1.28ca93ed97

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/service.ts CHANGED
@@ -149,12 +149,14 @@ export class SearchService {
149
149
  }
150
150
 
151
151
  const strategyIds = options.strategies ?? this.defaultStrategies
152
- const activeStrategies = await this.getAvailableStrategies(strategyIds)
152
+ const honorsFilter = (strategy: SearchStrategy) =>
153
+ !options.indexDocFilter || strategy.supportsIndexDocFilter === true
154
+ const activeStrategies = (await this.getAvailableStrategies(strategyIds)).filter(honorsFilter)
153
155
 
154
156
  if (activeStrategies.length === 0) {
155
157
  // Try fallback strategy if defined
156
158
  if (this.fallbackStrategy) {
157
- const fallback = await this.getAvailableStrategies([this.fallbackStrategy])
159
+ const fallback = (await this.getAvailableStrategies([this.fallbackStrategy])).filter(honorsFilter)
158
160
  if (fallback.length > 0) {
159
161
  activeStrategies.push(...fallback)
160
162
  }
@@ -35,6 +35,7 @@ export class FullTextSearchStrategy implements SearchStrategy {
35
35
  }
36
36
 
37
37
  async search(query: string, options: SearchOptions): Promise<SearchResult[]> {
38
+ if (options.indexDocFilter) return []
38
39
  if (Array.isArray(options.organizationIds)) {
39
40
  const organizationIds = options.organizationIds
40
41
  .map((value) => (typeof value === 'string' ? value.trim() : ''))
@@ -1,6 +1,13 @@
1
1
  export { TokenSearchStrategy, type TokenStrategyConfig } from './token.strategy'
2
2
  export { VectorSearchStrategy, type VectorStrategyConfig, type EmbeddingService } from './vector.strategy'
3
3
  export { FullTextSearchStrategy } from './fulltext.strategy'
4
+ export {
5
+ buildIndexDocFilterExists,
6
+ buildIndexDocFilterPredicate,
7
+ compileIndexDocFilterExists,
8
+ INDEX_DOC_FILTER_ALIAS,
9
+ type IndexDocFilterTarget,
10
+ } from '../lib/index-doc-filter'
4
11
 
5
12
  // Re-export fulltext driver types for convenience
6
13
  export type {
@@ -7,6 +7,7 @@ import type {
7
7
  IndexableRecord,
8
8
  } from '../types'
9
9
  import type { EntityId } from '@open-mercato/shared/modules/entities'
10
+ import { buildIndexDocFilterExists } from '../lib/index-doc-filter'
10
11
 
11
12
  /**
12
13
  * Configuration for TokenSearchStrategy.
@@ -86,6 +87,7 @@ export class TokenSearchStrategy implements SearchStrategy {
86
87
  readonly id: SearchStrategyId = 'tokens'
87
88
  readonly name = 'Token Search'
88
89
  readonly priority = 10 // Lowest priority, always available as fallback
90
+ readonly supportsIndexDocFilter = true
89
91
 
90
92
  private readonly minMatchRatio: number
91
93
  private readonly defaultLimit: number
@@ -159,6 +161,16 @@ export class TokenSearchStrategy implements SearchStrategy {
159
161
  queryBuilder = queryBuilder.where('entity_type' as any, 'not in', excludedEntityTypes)
160
162
  }
161
163
 
164
+ if (options.indexDocFilter) {
165
+ queryBuilder = queryBuilder.where(
166
+ buildIndexDocFilterExists(options.indexDocFilter, {
167
+ entityTypeRef: 'search_tokens.entity_type',
168
+ recordIdRef: 'search_tokens.entity_id',
169
+ tenantIdRef: 'search_tokens.tenant_id',
170
+ }),
171
+ )
172
+ }
173
+
162
174
  const rows = await queryBuilder.execute() as Array<{
163
175
  entity_type: string
164
176
  entity_id: string
@@ -46,6 +46,10 @@ export class VectorSearchStrategy implements SearchStrategy {
46
46
  readonly name = 'Vector Search'
47
47
  readonly priority = 20 // Medium priority
48
48
 
49
+ get supportsIndexDocFilter(): boolean {
50
+ return this.vectorDriver.supportsIndexDocFilter === true
51
+ }
52
+
49
53
  private readonly defaultLimit: number
50
54
  private ready = false
51
55
  private readyPromise: Promise<void> | null = null
@@ -81,6 +85,7 @@ export class VectorSearchStrategy implements SearchStrategy {
81
85
  async search(query: string, options: SearchOptions): Promise<SearchResult[]> {
82
86
  const organizationIds = normalizeOrganizationIds(options)
83
87
  if (organizationIds && organizationIds.length === 0) return []
88
+ if (options.indexDocFilter && !this.supportsIndexDocFilter) return []
84
89
 
85
90
  await this.ensureReady()
86
91
  const embedding = await this.embeddingService.createEmbedding(query)
@@ -93,9 +98,11 @@ export class VectorSearchStrategy implements SearchStrategy {
93
98
  organizationId?: string | null
94
99
  organizationIds?: string[] | null
95
100
  entityIds?: EntityId[]
101
+ indexDocFilter?: SearchOptions['indexDocFilter']
96
102
  } = {
97
103
  tenantId: options.tenantId,
98
104
  entityIds: options.entityTypes as EntityId[],
105
+ ...(options.indexDocFilter ? { indexDocFilter: options.indexDocFilter } : {}),
99
106
  }
100
107
 
101
108
  if (organizationIds) {
package/src/types.ts CHANGED
@@ -13,6 +13,8 @@ export type {
13
13
 
14
14
  // Search options
15
15
  SearchOptions,
16
+ SearchIndexDocCondition,
17
+ SearchIndexDocFilter,
16
18
 
17
19
  // Indexable record
18
20
  IndexableRecord,
@@ -1,5 +1,6 @@
1
1
  import { Pool } from 'pg'
2
2
  import { searchDebugWarn, searchWarn } from '../../../lib/debug'
3
+ import { compileIndexDocFilterExists } from '../../../lib/index-doc-filter'
3
4
 
4
5
  type PgPoolQueryResult<T> = { rows: T[]; rowCount?: number }
5
6
  type PgPoolClient = {
@@ -420,6 +421,18 @@ export function createPgVectorDriver(opts: PgVectorDriverOptions = {}): VectorDr
420
421
  Array.isArray(filter.entityIds) && filter.entityIds.length ? filter.entityIds : null,
421
422
  input.limit ?? 20,
422
423
  ]
424
+ const indexDocClause = filter.indexDocFilter
425
+ ? compileIndexDocFilterExists(
426
+ filter.indexDocFilter,
427
+ {
428
+ entityTypeRef: `${tableName}.entity_id`,
429
+ recordIdRef: `${tableName}.record_id`,
430
+ tenantIdRef: `${tableName}.tenant_id`,
431
+ },
432
+ params.length + 1,
433
+ )
434
+ : null
435
+ if (indexDocClause) params.push(...indexDocClause.parameters)
423
436
  const res = await pool.query<{
424
437
  entity_id: string
425
438
  record_id: string
@@ -463,6 +476,7 @@ export function createPgVectorDriver(opts: PgVectorDriverOptions = {}): VectorDr
463
476
  AND (
464
477
  $5::text[] IS NULL OR entity_id = ANY($5::text[])
465
478
  )
479
+ ${indexDocClause ? `AND ${indexDocClause.sql}` : ''}
466
480
  ORDER BY embedding <=> $1::vector
467
481
  LIMIT $6
468
482
  `,
@@ -706,6 +720,7 @@ export function createPgVectorDriver(opts: PgVectorDriverOptions = {}): VectorDr
706
720
 
707
721
  return {
708
722
  id: 'pgvector',
723
+ supportsIndexDocFilter: true,
709
724
  ensureReady,
710
725
  isHealthy,
711
726
  getStatus,
@@ -1,4 +1,5 @@
1
1
  import type { EntityId } from '@open-mercato/shared/modules/entities'
2
+ import type { SearchIndexDocFilter } from '@open-mercato/shared/modules/search'
2
3
  import type {
3
4
  VectorDriverId,
4
5
  VectorIndexSource,
@@ -52,6 +53,11 @@ export type VectorDriverQuery = {
52
53
  organizationId?: string | null
53
54
  organizationIds?: string[] | null
54
55
  tenantId: string
56
+ /**
57
+ * Applied inside the similarity query (additive, 2026-10-06). Only drivers that declare
58
+ * `supportsIndexDocFilter` receive it; see `SearchOptions.indexDocFilter`.
59
+ */
60
+ indexDocFilter?: SearchIndexDocFilter
55
61
  }
56
62
  }
57
63
 
@@ -103,6 +109,11 @@ export type VectorDriverStatus = {
103
109
 
104
110
  export interface VectorDriver {
105
111
  readonly id: VectorDriverId
112
+ /**
113
+ * Whether `query()` applies `filter.indexDocFilter` in the same query that ranks by similarity.
114
+ * Absent or `false` makes the vector strategy return no results for a filtered search.
115
+ */
116
+ readonly supportsIndexDocFilter?: boolean
106
117
  ensureReady(): Promise<void>
107
118
  /**
108
119
  * Cheap, cached probe answering whether the backing store can serve reads and