@open-mercato/search 0.10.1-develop.8231.1.ae76b1d2c5 → 0.10.1-develop.8258.1.62a94b74a2

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.
Files changed (57) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/indexer/search-indexer.js +21 -4
  3. package/dist/indexer/search-indexer.js.map +2 -2
  4. package/dist/modules/search/__integration__/TC-SEARCH-016.spec.js +187 -0
  5. package/dist/modules/search/__integration__/TC-SEARCH-016.spec.js.map +7 -0
  6. package/dist/modules/search/__integration__/TC-SEARCH-017.spec.js +94 -0
  7. package/dist/modules/search/__integration__/TC-SEARCH-017.spec.js.map +7 -0
  8. package/dist/modules/search/__integration__/TC-SEARCH-018.spec.js +74 -0
  9. package/dist/modules/search/__integration__/TC-SEARCH-018.spec.js.map +7 -0
  10. package/dist/modules/search/ai-tools.js +3 -1
  11. package/dist/modules/search/ai-tools.js.map +2 -2
  12. package/dist/modules/search/api/openapi.js +1 -0
  13. package/dist/modules/search/api/openapi.js.map +2 -2
  14. package/dist/modules/search/api/reindex/route.js +35 -16
  15. package/dist/modules/search/api/reindex/route.js.map +2 -2
  16. package/dist/modules/search/cli.js +2 -16
  17. package/dist/modules/search/cli.js.map +2 -2
  18. package/dist/modules/search/frontend/components/sections/FulltextSearchSection.js +1 -1
  19. package/dist/modules/search/frontend/components/sections/FulltextSearchSection.js.map +2 -2
  20. package/dist/modules/search/i18n/de.json +2 -0
  21. package/dist/modules/search/i18n/en.json +2 -0
  22. package/dist/modules/search/i18n/es.json +2 -0
  23. package/dist/modules/search/i18n/ko.json +2 -0
  24. package/dist/modules/search/i18n/pl.json +2 -0
  25. package/dist/modules/search/lib/reindex-lock.js +29 -6
  26. package/dist/modules/search/lib/reindex-lock.js.map +2 -2
  27. package/dist/modules/search/lib/reindex-progress.js +28 -3
  28. package/dist/modules/search/lib/reindex-progress.js.map +2 -2
  29. package/dist/modules/search/workers/fulltext-index.worker.js +19 -6
  30. package/dist/modules/search/workers/fulltext-index.worker.js.map +2 -2
  31. package/dist/strategies/fulltext.strategy.js +33 -2
  32. package/dist/strategies/fulltext.strategy.js.map +2 -2
  33. package/package.json +4 -4
  34. package/src/__tests__/reindex-strategy-policy.test.ts +2 -0
  35. package/src/__tests__/search-indexer-fulltext-reindex-scope.test.ts +435 -0
  36. package/src/__tests__/workers.test.ts +46 -1
  37. package/src/indexer/search-indexer.ts +31 -7
  38. package/src/modules/search/__integration__/TC-SEARCH-016.spec.ts +247 -0
  39. package/src/modules/search/__integration__/TC-SEARCH-017.spec.ts +141 -0
  40. package/src/modules/search/__integration__/TC-SEARCH-018.spec.ts +114 -0
  41. package/src/modules/search/ai-tools.ts +3 -1
  42. package/src/modules/search/api/__tests__/reindex.routes.test.ts +222 -6
  43. package/src/modules/search/api/openapi.ts +1 -0
  44. package/src/modules/search/api/reindex/route.ts +39 -16
  45. package/src/modules/search/cli.ts +2 -16
  46. package/src/modules/search/frontend/components/sections/FulltextSearchSection.tsx +1 -1
  47. package/src/modules/search/frontend/components/sections/__tests__/FulltextSearchSection.test.tsx +112 -3
  48. package/src/modules/search/i18n/de.json +2 -0
  49. package/src/modules/search/i18n/en.json +2 -0
  50. package/src/modules/search/i18n/es.json +2 -0
  51. package/src/modules/search/i18n/ko.json +2 -0
  52. package/src/modules/search/i18n/pl.json +2 -0
  53. package/src/modules/search/lib/__tests__/reindex-progress.test.ts +157 -0
  54. package/src/modules/search/lib/reindex-lock.ts +66 -8
  55. package/src/modules/search/lib/reindex-progress.ts +55 -2
  56. package/src/modules/search/workers/fulltext-index.worker.ts +21 -6
  57. package/src/strategies/fulltext.strategy.ts +39 -2
@@ -1,2 +1,2 @@
1
- [build:search] found 107 entry points
1
+ [build:search] found 110 entry points
2
2
  [build:search] built successfully
@@ -55,6 +55,12 @@ function withoutExcludedCustomFields(customFields, exclusion) {
55
55
  }
56
56
  return filtered;
57
57
  }
58
+ function canScopedPurge(fulltext) {
59
+ return typeof fulltext.canPurge !== "function" || fulltext.canPurge();
60
+ }
61
+ function scopedPurgeUnsupportedError(fulltext) {
62
+ return `Fulltext driver "${fulltext.driverId}" cannot clear documents per entity (implement purge or listDocumentIds); reindex aborted to avoid keeping stale documents`;
63
+ }
58
64
  class SearchIndexer {
59
65
  constructor(searchService, moduleConfigs, options) {
60
66
  this.searchService = searchService;
@@ -582,7 +588,12 @@ class SearchIndexer {
582
588
  processed: 0
583
589
  });
584
590
  if (params.recreateIndex !== false) {
585
- await fulltext.recreateIndex(params.tenantId);
591
+ if (!canScopedPurge(fulltext)) {
592
+ result.success = false;
593
+ result.errors.push({ entityId: params.entityId, error: scopedPurgeUnsupportedError(fulltext) });
594
+ return result;
595
+ }
596
+ await fulltext.purge(params.entityId, params.tenantId, params.organizationId ?? null);
586
597
  }
587
598
  const exclusions = this.createCustomFieldExclusionLookup();
588
599
  const pageSize = 200;
@@ -698,17 +709,23 @@ class SearchIndexer {
698
709
  result.errors.push({ entityId: "all", error: "Fulltext strategy not available" });
699
710
  return result;
700
711
  }
701
- if (params.recreateIndex !== false) {
712
+ const clearIndex = params.recreateIndex !== false;
713
+ const organizationScoped = typeof params.organizationId === "string" && params.organizationId.trim().length > 0;
714
+ if (clearIndex && !organizationScoped) {
702
715
  await fulltext.recreateIndex(params.tenantId);
703
716
  }
717
+ if (clearIndex && organizationScoped && !canScopedPurge(fulltext)) {
718
+ result.success = false;
719
+ result.errors.push({ entityId: "all", error: scopedPurgeUnsupportedError(fulltext) });
720
+ return result;
721
+ }
704
722
  const entities = this.listEnabledEntities();
705
723
  for (const entityId of entities) {
706
724
  const entityResult = await this.reindexEntityToFulltext({
707
725
  entityId,
708
726
  tenantId: params.tenantId,
709
727
  organizationId: params.organizationId,
710
- recreateIndex: false,
711
- // Already recreated above
728
+ recreateIndex: clearIndex && organizationScoped,
712
729
  onProgress: params.onProgress,
713
730
  useQueue: params.useQueue
714
731
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/indexer/search-indexer.ts"],
4
- "sourcesContent": ["import { isEntityIndexStrategyAllowed } from '../lib/entity-strategy-policy'\nimport type { SearchService } from '../service'\nimport type {\n SearchModuleConfig,\n SearchEntityConfig,\n SearchBuildContext,\n IndexableRecord,\n SearchResultPresenter,\n SearchResultLink,\n} from '../types'\nimport type { FullTextSearchStrategy } from '../strategies/fulltext.strategy'\nimport type { EntityId } from '@open-mercato/shared/modules/entities'\nimport type { QueryEngine } from '@open-mercato/shared/lib/query/types'\nimport type { Queue } from '@open-mercato/queue'\nimport type { FulltextIndexJobPayload } from '../queue/fulltext-indexing'\nimport type { VectorIndexJobPayload, VectorBatchRecord } from '../queue/vector-indexing'\nimport { reportSearchError, searchDebug, searchDebugWarn, searchError, searchWarn } from '../lib/debug'\nimport type { EncryptedCustomFieldKeysResolver } from '../lib/encrypted-custom-field-resolver'\nimport { normalizeDefinitionKey } from '@open-mercato/shared/lib/crud/custom-field-definition-index'\n\n/**\n * Maximum number of pages to process during reindex to prevent infinite loops.\n * At 50 records per page, this allows up to 500,000 records per entity.\n */\nconst MAX_PAGES = 10000\n\n/**\n * Custom fields kept out of the build context: the normalized keys of encrypted\n * definitions, every custom field when they could not be resolved, or none.\n */\ntype CustomFieldExclusion = Set<string> | 'all' | null\n\ntype CustomFieldExclusionLookup = (entityId: EntityId, tenantId: string | null) => Promise<CustomFieldExclusion>\n\nfunction toPlaintextExcludedCustomFields(\n exclusion: CustomFieldExclusion,\n): IndexableRecord['plaintextExcludedCustomFields'] {\n if (!exclusion) return undefined\n return exclusion === 'all' ? 'all' : Array.from(exclusion)\n}\n\nfunction mergeCustomFieldExclusions(exclusions: CustomFieldExclusion[]): CustomFieldExclusion {\n if (exclusions.includes('all')) return 'all'\n const sets = exclusions.filter((exclusion): exclusion is Set<string> => exclusion instanceof Set)\n if (!sets.length) return null\n if (sets.length === 1) return sets[0]\n return new Set(sets.flatMap((set) => Array.from(set)))\n}\n\nfunction isExcludedCustomField(key: string, exclusion: CustomFieldExclusion): boolean {\n if (!exclusion) return false\n if (exclusion === 'all') return true\n return exclusion.has(normalizeDefinitionKey(key))\n}\n\nfunction withoutExcludedRecordCustomFields(\n record: Record<string, unknown>,\n exclusion: CustomFieldExclusion,\n): Record<string, unknown> {\n if (!exclusion) return record\n const filtered: Record<string, unknown> = {}\n let removed = false\n for (const [key, value] of Object.entries(record)) {\n const isCustomField = key.startsWith('cf:') || key.startsWith('cf_')\n if (isCustomField && isExcludedCustomField(key.slice(3), exclusion)) {\n removed = true\n continue\n }\n filtered[key] = value\n }\n return removed ? filtered : record\n}\n\n/**\n * Build callbacks may enrich the record they receive (for example with a hydrated parent);\n * those changes reach the indexed fields as they did before the record was filtered.\n */\nfunction applyBuildRecordChanges(\n buildRecord: Record<string, unknown>,\n record: Record<string, unknown>,\n exclusion: CustomFieldExclusion,\n): void {\n if (buildRecord === record) return\n for (const [key, value] of Object.entries(buildRecord)) {\n if (record[key] !== value) record[key] = value\n }\n for (const key of Object.keys(record)) {\n if (key in buildRecord) continue\n const isCustomField = key.startsWith('cf:') || key.startsWith('cf_')\n if (isCustomField && isExcludedCustomField(key.slice(3), exclusion)) continue\n delete record[key]\n }\n}\n\nfunction withoutExcludedCustomFields(\n customFields: Record<string, unknown>,\n exclusion: CustomFieldExclusion,\n): Record<string, unknown> {\n if (!exclusion) return customFields\n const filtered: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(customFields)) {\n const isPrefixed = key.startsWith('cf:') || key.startsWith('cf_')\n if (isExcludedCustomField(key, exclusion)) continue\n if (isPrefixed && isExcludedCustomField(key.slice(3), exclusion)) continue\n filtered[key] = value\n }\n return filtered\n}\n\n/**\n * Parameters for indexing a record.\n */\nexport type IndexRecordParams = {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n record: Record<string, unknown>\n customFields?: Record<string, unknown>\n}\n\ntype IndexRecordByIdParams = {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Parameters for indexing a batch of records by id in a single bulk write.\n */\nexport type IndexRecordsByIdParams = {\n items: Array<{ entityId: EntityId; recordId: string }>\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Parameters for deleting a record from the search index.\n */\nexport type DeleteRecordParams = {\n entityId: EntityId\n recordId: string\n tenantId: string\n}\n\n/**\n * Parameters for purging all records of an entity type.\n */\nexport type PurgeEntityParams = {\n entityId: EntityId\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Parameters for reindexing an entity to fulltext search.\n */\nexport type ReindexEntityParams = {\n entityId: EntityId\n tenantId: string\n organizationId?: string | null\n /** Whether to recreate the index first (default: true) */\n recreateIndex?: boolean\n /** Callback for progress tracking */\n onProgress?: (progress: ReindexProgress) => void\n /** Whether to use queue for batch processing (default: false) */\n useQueue?: boolean\n}\n\n/**\n * Parameters for reindexing all entities to fulltext search.\n */\nexport type ReindexAllParams = {\n tenantId: string\n organizationId?: string | null\n /** Whether to recreate the index first (default: true) */\n recreateIndex?: boolean\n /** Callback for progress tracking */\n onProgress?: (progress: ReindexProgress) => void\n /** Whether to use queue for batch processing (default: false) */\n useQueue?: boolean\n}\n\n/**\n * Progress information during reindex.\n */\nexport type ReindexProgress = {\n entityId: EntityId\n phase: 'starting' | 'fetching' | 'indexing' | 'complete'\n processed: number\n total?: number\n}\n\n/**\n * Result of a reindex operation.\n */\nexport type ReindexResult = {\n success: boolean\n entitiesProcessed: number\n recordsIndexed: number\n /** Number of records dropped due to missing id or other validation failures */\n recordsDropped?: number\n /** Number of jobs enqueued (when useQueue is true) */\n jobsEnqueued?: number\n errors: Array<{ entityId: EntityId; error: string }>\n}\n\n/**\n * Optional dependencies for SearchIndexer.\n */\nexport type SearchIndexerOptions = {\n queryEngine?: QueryEngine\n /** Queue for fulltext batch indexing */\n fulltextQueue?: Queue<FulltextIndexJobPayload>\n /** Queue for vector batch indexing */\n vectorQueue?: Queue<VectorIndexJobPayload>\n /**\n * Resolves the custom fields declared `encrypted` for an entity, so their values never reach\n * buildSource, formatResult, resolveUrl or resolveLinks and stay out of the plain text a\n * strategy stores or embeds. Without it, every custom field is left out of those callbacks.\n * `IndexableRecord.fields` keeps them for hash-only tokens.\n */\n encryptedCustomFieldKeysResolver?: EncryptedCustomFieldKeysResolver\n}\n\n/**\n * SearchIndexer orchestrates indexing operations by resolving entity configs\n * and building IndexableRecords for the SearchService.\n */\nexport class SearchIndexer {\n private readonly entityConfigMap: Map<EntityId, SearchEntityConfig>\n private readonly queryEngine?: QueryEngine\n private readonly fulltextQueue?: Queue<FulltextIndexJobPayload>\n private readonly vectorQueue?: Queue<VectorIndexJobPayload>\n private readonly encryptedCustomFieldKeysResolver?: EncryptedCustomFieldKeysResolver\n\n constructor(\n private readonly searchService: SearchService,\n private readonly moduleConfigs: SearchModuleConfig[],\n options?: SearchIndexerOptions,\n ) {\n this.entityConfigMap = new Map()\n this.queryEngine = options?.queryEngine\n this.fulltextQueue = options?.fulltextQueue\n this.vectorQueue = options?.vectorQueue\n this.encryptedCustomFieldKeysResolver = options?.encryptedCustomFieldKeysResolver\n if (!this.encryptedCustomFieldKeysResolver) {\n searchWarn('SearchIndexer', 'No encrypted custom-field resolver; excluding every custom field from build contexts')\n reportSearchError(\n 'SearchIndexer',\n new Error('[internal] SearchIndexer has no encryptedCustomFieldKeysResolver; excluding every custom field from build contexts'),\n 'search.encrypted_custom_fields_resolver_missing',\n )\n }\n for (const moduleConfig of moduleConfigs) {\n for (const entityConfig of moduleConfig.entities) {\n if (entityConfig.enabled !== false) {\n this.entityConfigMap.set(entityConfig.entityId as EntityId, entityConfig)\n }\n }\n }\n }\n\n /**\n * Returns a lookup of the custom fields to keep out of build contexts, memoized per entity and\n * tenant for one indexing call or reindex run. A missing resolver or a failed resolution excludes\n * every custom field. Failed resolutions stay cached for the current operation and are retried by\n * the fresh lookup created for the next top-level indexing call.\n */\n private createCustomFieldExclusionLookup(): CustomFieldExclusionLookup {\n const resolver = this.encryptedCustomFieldKeysResolver\n const cache = new Map<string, Promise<CustomFieldExclusion>>()\n return (entityId, tenantId) => {\n if (!resolver) return Promise.resolve('all')\n const cacheKey = `${entityId}\\u0000${tenantId ?? ''}`\n let pending = cache.get(cacheKey)\n if (!pending) {\n pending = Promise.resolve().then(() => resolver(entityId, tenantId)).catch((error): CustomFieldExclusion => {\n const resolutionError = error instanceof Error\n ? error\n : new Error('[internal] Encrypted custom-field resolver rejected with a non-Error value')\n searchWarn('SearchIndexer', 'Encrypted custom fields could not be resolved; excluding every custom field', {\n entityId,\n error: resolutionError.message,\n })\n reportSearchError('SearchIndexer', resolutionError, 'search.encrypted_custom_fields_unresolved', { entityId })\n return 'all'\n })\n cache.set(cacheKey, pending)\n }\n return pending\n }\n }\n\n /**\n * Returns a wrapped QueryEngine that forces skipAutoReindex: true on every query.\n * Used to prevent feedback loops when search indexing callbacks (buildSource, formatResult)\n * call queryEngine.query() to hydrate related entities. Encrypted custom fields are removed\n * from the hydrated records, as from the indexed record itself, including those joined in\n * from `customFieldSources`. A query without a tenant is checked against every tenant's definitions.\n */\n private createBuildQueryEngine(exclusions: CustomFieldExclusionLookup): QueryEngine | undefined {\n if (!this.queryEngine) return undefined\n const queryEngine = this.queryEngine\n const wrapped: QueryEngine = {\n query: async (entity, opts) => {\n const result = await queryEngine.query(entity, { ...opts, skipAutoReindex: true })\n const sourceEntityIds = [entity, ...(opts?.customFieldSources ?? []).map((source) => source.entityId)]\n const tenantId = opts?.tenantId ?? null\n const exclusion = mergeCustomFieldExclusions(\n await Promise.all(sourceEntityIds.map((sourceEntityId) => exclusions(sourceEntityId as EntityId, tenantId))),\n )\n if (!exclusion || !Array.isArray(result?.items)) return result\n return {\n ...result,\n items: result.items.map((item: Record<string, unknown>) => withoutExcludedRecordCustomFields(item, exclusion)),\n }\n },\n }\n return wrapped\n }\n\n /**\n * Get the entity config for a given entity ID.\n */\n getEntityConfig(entityId: EntityId): SearchEntityConfig | undefined {\n return this.entityConfigMap.get(entityId)\n }\n\n /**\n * Get all configured entity configs.\n */\n getAllEntityConfigs(): SearchEntityConfig[] {\n return Array.from(this.entityConfigMap.values())\n }\n\n /**\n * Check if an entity is configured for search indexing.\n */\n isEntityEnabled(entityId: EntityId): boolean {\n const config = this.entityConfigMap.get(entityId)\n return config?.enabled !== false\n }\n\n /**\n * Index a record in the search service.\n */\n async indexRecord(params: IndexRecordParams): Promise<void> {\n await this.indexRecordWithExclusions(params, this.createCustomFieldExclusionLookup())\n }\n\n private async indexRecordWithExclusions(\n params: IndexRecordParams,\n exclusions: CustomFieldExclusionLookup,\n ): Promise<void> {\n const indexableRecord = await this.buildIndexableRecord(params, exclusions)\n if (indexableRecord) {\n await this.searchService.index(indexableRecord)\n }\n }\n\n private async buildIndexableRecord(\n params: IndexRecordParams,\n exclusions: CustomFieldExclusionLookup = this.createCustomFieldExclusionLookup(),\n ): Promise<IndexableRecord | null> {\n const config = this.entityConfigMap.get(params.entityId)\n if (!config || config.enabled === false) {\n return null\n }\n\n const exclusion = await exclusions(params.entityId, params.tenantId)\n const buildContext: SearchBuildContext = {\n record: withoutExcludedRecordCustomFields(params.record, exclusion),\n customFields: withoutExcludedCustomFields(params.customFields ?? {}, exclusion),\n organizationId: params.organizationId,\n tenantId: params.tenantId,\n queryEngine: this.createBuildQueryEngine(exclusions),\n }\n\n // Try buildSource first (provides text, presenter, links, checksumSource)\n let text: string | string[] | undefined = config.buildSource ? [] : undefined\n let presenter: SearchResultPresenter | undefined\n let url: string | undefined\n let links: SearchResultLink[] | undefined\n let checksumSource: unknown | undefined\n\n if (config.buildSource) {\n try {\n const source = await config.buildSource(buildContext)\n if (source) {\n text = source.text ?? []\n if (source.presenter) presenter = source.presenter\n if (source.links) links = source.links\n if (source.checksumSource !== undefined) checksumSource = source.checksumSource\n }\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'buildSource failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Fall back to formatResult if no presenter from buildSource\n if (!presenter && config.formatResult) {\n try {\n const result = await config.formatResult(buildContext)\n if (result) presenter = result\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'formatResult failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Resolve URL if not already set\n if (!url && config.resolveUrl) {\n try {\n const result = await config.resolveUrl(buildContext)\n if (result) url = result\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'resolveUrl failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Resolve links if not already set\n if (!links && config.resolveLinks) {\n try {\n const result = await config.resolveLinks(buildContext)\n if (result) links = result\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'resolveLinks failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n applyBuildRecordChanges(buildContext.record, params.record, exclusion)\n\n return {\n entityId: params.entityId,\n recordId: params.recordId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n fields: params.record,\n plaintextExcludedCustomFields: toPlaintextExcludedCustomFields(exclusion),\n presenter,\n url,\n links,\n text,\n checksumSource,\n }\n }\n\n /**\n * Index a record by ID (loads the record from database first).\n * Used by workers that only have record identifiers.\n */\n async indexRecordById(params: {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n }): Promise<{ action: 'indexed' | 'skipped'; reason?: string }> {\n return this.indexRecordByIdWithExclusions(params, this.createCustomFieldExclusionLookup())\n }\n\n private async indexRecordByIdWithExclusions(\n params: IndexRecordByIdParams,\n exclusions: CustomFieldExclusionLookup,\n ): Promise<{ action: 'indexed' | 'skipped'; reason?: string }> {\n if (!this.queryEngine) {\n return { action: 'skipped', reason: 'queryEngine not available' }\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config || config.enabled === false) {\n return { action: 'skipped', reason: 'entity not configured' }\n }\n\n // Load record from database\n try {\n const result = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n filters: { id: params.recordId },\n includeCustomFields: true,\n page: { page: 1, pageSize: 1 },\n skipAutoReindex: true,\n })\n\n const record = result.items[0] as Record<string, unknown> | undefined\n if (!record) {\n return { action: 'skipped', reason: 'record not found' }\n }\n\n // Extract custom fields\n const customFields: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(record)) {\n if (key.startsWith('cf:') || key.startsWith('cf_')) {\n customFields[key.slice(3)] = value\n }\n }\n\n await this.indexRecordWithExclusions({\n entityId: params.entityId,\n recordId: params.recordId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n record,\n customFields,\n }, exclusions)\n\n return { action: 'indexed' }\n } catch (error) {\n searchError('SearchIndexer', 'Failed to load record for indexing', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n throw error\n }\n }\n\n /**\n * Index a batch of records by id in a single bulk write.\n * Unlike calling indexRecordById() in a loop, this loads each record fresh\n * (same as indexRecordById) but flushes the whole batch through a single\n * searchService.bulkIndex() call. Strategies that implement bulkIndex then\n * collapse the batch into one write; strategies without it still write per\n * record, at the bounded concurrency SearchService applies.\n */\n async indexRecordsById(params: IndexRecordsByIdParams): Promise<{ indexed: number; skipped: number }> {\n const { items, tenantId, organizationId } = params\n if (!this.queryEngine || items.length === 0) {\n return { indexed: 0, skipped: items.length }\n }\n\n const recordIdsByEntity = new Map<EntityId, string[]>()\n for (const item of items) {\n const list = recordIdsByEntity.get(item.entityId) ?? []\n list.push(item.recordId)\n recordIdsByEntity.set(item.entityId, list)\n }\n const exclusions = this.createCustomFieldExclusionLookup()\n\n const allRecords: IndexableRecord[] = []\n let skipped = 0\n\n for (const [entityId, recordIds] of recordIdsByEntity) {\n const config = this.entityConfigMap.get(entityId)\n if (!config || config.enabled === false) {\n skipped += recordIds.length\n continue\n }\n\n for (const recordId of recordIds) {\n try {\n const result = await this.queryEngine.query(entityId, {\n tenantId,\n organizationId: organizationId ?? undefined,\n filters: { id: recordId },\n includeCustomFields: true,\n page: { page: 1, pageSize: 1 },\n skipAutoReindex: true,\n })\n\n const record = result.items[0] as Record<string, unknown> | undefined\n if (!record) {\n skipped++\n continue\n }\n\n const { records: built, dropped } = await this.buildIndexableRecords(\n entityId,\n tenantId,\n organizationId ?? null,\n [record],\n config,\n exclusions,\n )\n skipped += dropped\n allRecords.push(...built)\n } catch (error) {\n skipped++\n searchError('SearchIndexer', 'Failed to load record for batch indexing', {\n entityId,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n }\n\n if (allRecords.length > 0) {\n await this.searchService.bulkIndex(allRecords)\n }\n\n return { indexed: allRecords.length, skipped }\n }\n\n /**\n * Delete a record from the search index.\n */\n async deleteRecord(params: DeleteRecordParams): Promise<void> {\n await this.searchService.delete(params.entityId, params.recordId, params.tenantId)\n }\n\n /**\n * Purge all records of an entity type from the search index.\n */\n async purgeEntity(params: PurgeEntityParams): Promise<void> {\n await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)\n }\n\n /**\n * Reindex an entity via all configured strategies (including vector).\n * This is the general reindex method that works with all search strategies.\n */\n async reindexEntity(params: {\n entityId: EntityId\n tenantId: string\n organizationId?: string | null\n purgeFirst?: boolean\n }): Promise<ReindexResult> {\n if (!this.queryEngine) {\n return {\n success: false,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n errors: [{ entityId: params.entityId, error: 'Query engine not available' }],\n }\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config || config.enabled === false) {\n return {\n success: false,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n errors: [{ entityId: params.entityId, error: 'Entity not configured for search' }],\n }\n }\n\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 1,\n recordsIndexed: 0,\n errors: [],\n }\n\n // Optionally purge first\n if (params.purgeFirst) {\n try {\n await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to purge entity before reindex', {\n entityId: params.entityId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Paginate through all records\n const exclusions = this.createCustomFieldExclusionLookup()\n let page = 1\n const pageSize = 200\n let hasMore = true\n\n while (hasMore && page <= MAX_PAGES) {\n try {\n const queryResult = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n includeCustomFields: true,\n page: { page, pageSize },\n skipAutoReindex: true,\n })\n\n const items = queryResult.items as Record<string, unknown>[]\n if (items.length === 0) {\n hasMore = false\n break\n }\n\n // Build and index records\n const { records } = await this.buildIndexableRecords(\n params.entityId,\n params.tenantId,\n params.organizationId ?? null,\n items,\n config,\n exclusions,\n )\n\n // Bulk index records via SearchService (sends to all strategies)\n if (records.length > 0) {\n try {\n await this.searchService.bulkIndex(records)\n result.recordsIndexed += records.length\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to bulk index records', {\n entityId: params.entityId,\n recordCount: records.length,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n page++\n hasMore = items.length === pageSize\n } catch (error) {\n result.success = false\n result.errors.push({\n entityId: params.entityId,\n error: error instanceof Error ? error.message : String(error),\n })\n break\n }\n }\n\n return result\n }\n\n /**\n * Reindex all enabled entities via all configured strategies.\n */\n async reindexAll(params: {\n tenantId: string\n organizationId?: string | null\n purgeFirst?: boolean\n }): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n errors: [],\n }\n\n const enabledEntities = this.listEnabledEntities()\n\n for (const entityId of enabledEntities) {\n const entityResult = await this.reindexEntity({\n entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n purgeFirst: params.purgeFirst,\n })\n\n result.entitiesProcessed++\n result.recordsIndexed += entityResult.recordsIndexed\n result.errors.push(...entityResult.errors)\n\n if (!entityResult.success) {\n result.success = false\n }\n }\n\n return result\n }\n\n /**\n * Bulk index multiple records.\n */\n async bulkIndexRecords(params: IndexRecordParams[]): Promise<void> {\n const indexableRecords: IndexableRecord[] = []\n const exclusions = this.createCustomFieldExclusionLookup()\n\n for (const param of params) {\n const indexableRecord = await this.buildIndexableRecord(param, exclusions)\n if (indexableRecord) {\n indexableRecords.push(indexableRecord)\n }\n }\n\n if (indexableRecords.length > 0) {\n try {\n await this.searchService.bulkIndex(indexableRecords)\n } catch (error) {\n throw new Error(\n `Failed to bulk index ${indexableRecords.length} records: ${\n error instanceof Error ? error.message : error\n }`\n )\n }\n }\n }\n\n /**\n * List all enabled entity IDs from the module configurations.\n */\n listEnabledEntities(): EntityId[] {\n return Array.from(this.entityConfigMap.keys())\n }\n\n /**\n * Get the fulltext strategy from the search service.\n */\n private getFulltextStrategy(): FullTextSearchStrategy | undefined {\n const strategy = this.searchService.getStrategy('fulltext')\n if (!strategy) return undefined\n return strategy as FullTextSearchStrategy\n }\n\n /**\n * Reindex a single entity type to fulltext search.\n * This fetches all records from the database and re-indexes them to fulltext only.\n *\n * When `useQueue` is true, batches are enqueued for background processing by workers.\n * When `useQueue` is false (default), batches are indexed directly (blocking).\n */\n async reindexEntityToFulltext(params: ReindexEntityParams): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n if (!isEntityIndexStrategyAllowed(params.entityId, 'fulltext', this.moduleConfigs)) return result\n\n const fulltext = this.getFulltextStrategy()\n if (!fulltext) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Fulltext strategy not available' })\n return result\n }\n\n // If useQueue is requested but no queue is available, return error\n if (params.useQueue && !this.fulltextQueue) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Fulltext queue not configured for queue-based reindexing' })\n return result\n }\n\n if (!this.queryEngine) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'QueryEngine not available for reindexing' })\n return result\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Entity not configured for search' })\n return result\n }\n\n try {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'starting',\n processed: 0,\n })\n\n // Recreate index if requested (default: true)\n if (params.recreateIndex !== false) {\n await fulltext.recreateIndex(params.tenantId)\n }\n\n // Fetch and index records with pagination\n const exclusions = this.createCustomFieldExclusionLookup()\n const pageSize = 200\n let page = 1\n let totalProcessed = 0\n let jobsEnqueued = 0\n\n for (;;) {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'fetching',\n processed: totalProcessed,\n })\n\n try {\n const queryResult = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n page: { page, pageSize },\n skipAutoReindex: true,\n })\n\n if (!queryResult.items.length) {\n break\n }\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'indexing',\n processed: totalProcessed,\n total: queryResult.total,\n })\n\n // Build IndexableRecords for this batch\n const { records: indexableRecords, dropped } = await this.buildIndexableRecords(\n params.entityId,\n params.tenantId,\n params.organizationId ?? null,\n queryResult.items,\n config,\n exclusions,\n )\n result.recordsDropped = (result.recordsDropped ?? 0) + dropped\n\n // Index to fulltext - either via queue or directly\n if (indexableRecords.length > 0) {\n if (params.useQueue && this.fulltextQueue) {\n // Enqueue batch for background processing - only pass minimal references\n // Worker will load fresh data from entity_indexes table\n await this.fulltextQueue.enqueue({\n jobType: 'batch-index',\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n records: indexableRecords.map((r) => ({ entityId: r.entityId, recordId: r.recordId })),\n })\n jobsEnqueued += 1\n totalProcessed += indexableRecords.length\n } else {\n // Direct indexing (blocking)\n try {\n await fulltext.bulkIndex(indexableRecords)\n totalProcessed += indexableRecords.length\n } catch (indexError) {\n // Log error but continue with remaining batches\n const errorMsg = indexError instanceof Error ? indexError.message : String(indexError)\n result.errors.push({\n entityId: params.entityId,\n error: `Batch ${page} failed: ${errorMsg}`,\n })\n }\n }\n }\n\n if (queryResult.items.length < pageSize) {\n break\n }\n page += 1\n\n // Safety check to prevent infinite loops\n if (page > MAX_PAGES) {\n break\n }\n } catch (queryError) {\n const errorMsg = queryError instanceof Error ? queryError.message : String(queryError)\n result.errors.push({\n entityId: params.entityId,\n error: `Query failed: ${errorMsg}`,\n })\n break\n }\n }\n\n result.entitiesProcessed = 1\n result.recordsIndexed = totalProcessed\n result.jobsEnqueued = jobsEnqueued\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'complete',\n processed: totalProcessed,\n total: totalProcessed,\n })\n } catch (error) {\n result.success = false\n result.errors.push({\n entityId: params.entityId,\n error: error instanceof Error ? error.message : String(error),\n })\n }\n\n return result\n }\n\n /**\n * Reindex all enabled entities to fulltext search.\n *\n * When `useQueue` is true, batches are enqueued for background processing by workers.\n * When `useQueue` is false (default), batches are indexed directly (blocking).\n */\n async reindexAllToFulltext(params: ReindexAllParams): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n const fulltext = this.getFulltextStrategy()\n if (!fulltext) {\n result.success = false\n result.errors.push({ entityId: 'all' as EntityId, error: 'Fulltext strategy not available' })\n return result\n }\n\n // Recreate index once before processing all entities\n if (params.recreateIndex !== false) {\n await fulltext.recreateIndex(params.tenantId)\n }\n\n const entities = this.listEnabledEntities()\n\n for (const entityId of entities) {\n const entityResult = await this.reindexEntityToFulltext({\n entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n recreateIndex: false, // Already recreated above\n onProgress: params.onProgress,\n useQueue: params.useQueue,\n })\n\n result.entitiesProcessed += entityResult.entitiesProcessed\n result.recordsIndexed += entityResult.recordsIndexed\n result.recordsDropped = (result.recordsDropped ?? 0) + (entityResult.recordsDropped ?? 0)\n result.jobsEnqueued = (result.jobsEnqueued ?? 0) + (entityResult.jobsEnqueued ?? 0)\n result.errors.push(...entityResult.errors)\n\n if (!entityResult.success) {\n result.success = false\n }\n }\n\n return result\n }\n\n /**\n * Reindex a single entity type to vector search.\n * This fetches all records from the database and enqueues them for vector indexing.\n *\n * When `useQueue` is true (default), record IDs are enqueued for background processing by workers.\n * When `useQueue` is false, records are indexed directly (blocking).\n */\n async reindexEntityToVector(params: ReindexEntityParams & { purgeFirst?: boolean }): Promise<ReindexResult> {\n searchDebug('SearchIndexer', 'reindexEntityToVector called', {\n entityId: params.entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n useQueue: params.useQueue,\n purgeFirst: params.purgeFirst,\n })\n\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n if (!isEntityIndexStrategyAllowed(params.entityId, 'vector', this.moduleConfigs)) return result\n\n // If useQueue is requested but no queue is available, return error\n if (params.useQueue !== false && !this.vectorQueue) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Vector queue not configured for queue-based reindexing' })\n return result\n }\n\n if (!this.queryEngine) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'QueryEngine not available for reindexing' })\n return result\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Entity not configured for search' })\n return result\n }\n\n try {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'starting',\n processed: 0,\n })\n\n // Optionally purge vector index first\n if (params.purgeFirst) {\n try {\n await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to purge entity before vector reindex', {\n entityId: params.entityId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Fetch and enqueue records with pagination\n const pageSize = 200\n let page = 1\n let totalProcessed = 0\n let jobsEnqueued = 0\n const exclusions = this.createCustomFieldExclusionLookup()\n\n for (;;) {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'fetching',\n processed: totalProcessed,\n })\n\n const queryResult = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n page: { page, pageSize },\n skipAutoReindex: true,\n })\n\n if (!queryResult.items.length) break\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'indexing',\n processed: totalProcessed,\n total: queryResult.total,\n })\n\n // Build batch of record references\n const batchRecords: VectorBatchRecord[] = []\n for (const item of queryResult.items) {\n const recordId = String((item as Record<string, unknown>).id ?? '')\n if (!recordId) {\n result.recordsDropped = (result.recordsDropped ?? 0) + 1\n continue\n }\n batchRecords.push({\n entityId: params.entityId,\n recordId,\n })\n }\n\n // Enqueue batch for background processing or index directly\n if (batchRecords.length > 0) {\n if (params.useQueue !== false && this.vectorQueue) {\n await this.vectorQueue.enqueue({\n jobType: 'batch-index',\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? null,\n records: batchRecords,\n })\n jobsEnqueued += 1\n totalProcessed += batchRecords.length\n searchDebug('SearchIndexer', 'Enqueued batch for vector indexing', {\n entityId: params.entityId,\n batchSize: batchRecords.length,\n jobsEnqueued,\n totalProcessed,\n })\n } else {\n // Direct indexing (blocking) - index each record via SearchService\n for (const { entityId, recordId } of batchRecords) {\n try {\n await this.indexRecordByIdWithExclusions({\n entityId: entityId as EntityId,\n recordId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n }, exclusions)\n totalProcessed++\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to index record to vector', {\n entityId,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n }\n }\n\n if (queryResult.items.length < pageSize) break\n page += 1\n\n // Safety check to prevent infinite loops\n if (page > MAX_PAGES) {\n searchDebugWarn('SearchIndexer', 'Reached MAX_PAGES limit, stopping pagination', {\n entityId: params.entityId,\n maxPages: MAX_PAGES,\n totalProcessed,\n })\n break\n }\n }\n\n result.entitiesProcessed = 1\n result.recordsIndexed = totalProcessed\n result.jobsEnqueued = jobsEnqueued\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'complete',\n processed: totalProcessed,\n total: totalProcessed,\n })\n } catch (error) {\n result.success = false\n result.errors.push({\n entityId: params.entityId,\n error: error instanceof Error ? error.message : String(error),\n })\n }\n\n return result\n }\n\n /**\n * Reindex all enabled entities to vector search.\n *\n * When `useQueue` is true (default), batches are enqueued for background processing by workers.\n * When `useQueue` is false, batches are indexed directly (blocking).\n */\n async reindexAllToVector(params: ReindexAllParams & { purgeFirst?: boolean }): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n const entities = this.listEnabledEntities()\n for (const entityId of entities) {\n const entityResult = await this.reindexEntityToVector({\n entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n onProgress: params.onProgress,\n useQueue: params.useQueue,\n purgeFirst: params.purgeFirst,\n })\n\n result.entitiesProcessed += entityResult.entitiesProcessed\n result.recordsIndexed += entityResult.recordsIndexed\n result.recordsDropped = (result.recordsDropped ?? 0) + (entityResult.recordsDropped ?? 0)\n result.jobsEnqueued = (result.jobsEnqueued ?? 0) + (entityResult.jobsEnqueued ?? 0)\n result.errors.push(...entityResult.errors)\n\n if (!entityResult.success) {\n result.success = false\n }\n }\n\n return result\n }\n\n /**\n * Build IndexableRecords from raw query results.\n * Returns records and count of dropped items (missing id or other validation failures).\n */\n private async buildIndexableRecords(\n entityId: EntityId,\n tenantId: string,\n organizationId: string | null,\n items: Record<string, unknown>[],\n config: SearchEntityConfig,\n exclusions: CustomFieldExclusionLookup = this.createCustomFieldExclusionLookup(),\n ): Promise<{ records: IndexableRecord[]; dropped: number }> {\n const records: IndexableRecord[] = []\n let dropped = 0\n const exclusion = await exclusions(entityId, tenantId)\n const plaintextExcludedCustomFields = toPlaintextExcludedCustomFields(exclusion)\n const buildQueryEngine = this.createBuildQueryEngine(exclusions)\n\n if (items.length > 0) {\n searchDebug('SearchIndexer', 'Sample item structure', {\n entityId,\n sampleKeys: Object.keys(items[0]),\n hasId: 'id' in items[0],\n })\n }\n\n for (const item of items) {\n const recordId = String(item.id ?? '')\n if (!recordId) {\n searchDebugWarn('SearchIndexer', 'Skipping item without id', { entityId, itemKeys: Object.keys(item) })\n dropped++\n continue\n }\n\n // Extract custom fields from record\n const customFields: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(item)) {\n if (key.startsWith('cf:') || key.startsWith('cf_')) {\n const cfKey = key.slice(3) // Remove 'cf:' or 'cf_' prefix (both are 3 chars)\n customFields[cfKey] = value\n }\n }\n\n const buildContext: SearchBuildContext = {\n record: withoutExcludedRecordCustomFields(item, exclusion),\n customFields: withoutExcludedCustomFields(customFields, exclusion),\n organizationId,\n tenantId,\n queryEngine: buildQueryEngine,\n }\n\n // Try buildSource first (provides text, presenter, links, checksumSource)\n let text: string | string[] | undefined = config.buildSource ? [] : undefined\n let presenter: SearchResultPresenter | undefined\n let url: string | undefined\n let links: SearchResultLink[] | undefined\n let checksumSource: unknown | undefined\n\n if (config.buildSource) {\n try {\n const source = await config.buildSource(buildContext)\n if (source) {\n text = source.text ?? []\n if (source.presenter) presenter = source.presenter\n if (source.links) links = source.links\n if (source.checksumSource !== undefined) checksumSource = source.checksumSource\n }\n } catch (err) {\n searchDebugWarn('SearchIndexer', 'buildSource failed', {\n entityId,\n recordId,\n error: err instanceof Error ? err.message : err,\n })\n }\n }\n\n // Fall back to formatResult if no presenter from buildSource\n if (!presenter && config.formatResult) {\n try {\n const result = await config.formatResult(buildContext)\n if (result) presenter = result\n } catch {\n // Skip presenter on error\n }\n }\n\n // Resolve URL if not already set\n if (!url && config.resolveUrl) {\n try {\n const result = await config.resolveUrl(buildContext)\n if (result) url = result\n } catch {\n // Skip URL on error\n }\n }\n\n // Resolve links if not already set\n if (!links && config.resolveLinks) {\n try {\n const result = await config.resolveLinks(buildContext)\n if (result) links = result\n } catch {\n // Skip links on error\n }\n }\n\n applyBuildRecordChanges(buildContext.record, item, exclusion)\n\n records.push({\n entityId,\n recordId,\n tenantId,\n organizationId,\n fields: item,\n plaintextExcludedCustomFields,\n presenter,\n url,\n links,\n text,\n checksumSource,\n })\n }\n\n searchDebug('SearchIndexer', 'Finished building records', {\n entityId,\n inputCount: items.length,\n outputCount: records.length,\n dropped,\n })\n\n return { records, dropped }\n }\n}\n"],
5
- "mappings": "AAAA,SAAS,oCAAoC;AAgB7C,SAAS,mBAAmB,aAAa,iBAAiB,aAAa,kBAAkB;AAEzF,SAAS,8BAA8B;AAMvC,MAAM,YAAY;AAUlB,SAAS,gCACP,WACkD;AAClD,MAAI,CAAC,UAAW,QAAO;AACvB,SAAO,cAAc,QAAQ,QAAQ,MAAM,KAAK,SAAS;AAC3D;AAEA,SAAS,2BAA2B,YAA0D;AAC5F,MAAI,WAAW,SAAS,KAAK,EAAG,QAAO;AACvC,QAAM,OAAO,WAAW,OAAO,CAAC,cAAwC,qBAAqB,GAAG;AAChG,MAAI,CAAC,KAAK,OAAQ,QAAO;AACzB,MAAI,KAAK,WAAW,EAAG,QAAO,KAAK,CAAC;AACpC,SAAO,IAAI,IAAI,KAAK,QAAQ,CAAC,QAAQ,MAAM,KAAK,GAAG,CAAC,CAAC;AACvD;AAEA,SAAS,sBAAsB,KAAa,WAA0C;AACpF,MAAI,CAAC,UAAW,QAAO;AACvB,MAAI,cAAc,MAAO,QAAO;AAChC,SAAO,UAAU,IAAI,uBAAuB,GAAG,CAAC;AAClD;AAEA,SAAS,kCACP,QACA,WACyB;AACzB,MAAI,CAAC,UAAW,QAAO;AACvB,QAAM,WAAoC,CAAC;AAC3C,MAAI,UAAU;AACd,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,GAAG;AACjD,UAAM,gBAAgB,IAAI,WAAW,KAAK,KAAK,IAAI,WAAW,KAAK;AACnE,QAAI,iBAAiB,sBAAsB,IAAI,MAAM,CAAC,GAAG,SAAS,GAAG;AACnE,gBAAU;AACV;AAAA,IACF;AACA,aAAS,GAAG,IAAI;AAAA,EACl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AAU;AAAA,UAC9C,CAAC;AAAA,QACH;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,OAAO,cAAc;AACrC,YAAI;AACF,gBAAM,SAAS,MAAM,OAAO,aAAa,YAAY;AACrD,cAAI,OAAQ,aAAY;AAAA,QAC1B,QAAQ;AAAA,QAER;AAAA,MACF;AAGA,UAAI,CAAC,OAAO,OAAO,YAAY;AAC7B,YAAI;AACF,gBAAM,SAAS,MAAM,OAAO,WAAW,YAAY;AACnD,cAAI,OAAQ,OAAM;AAAA,QACpB,QAAQ;AAAA,QAER;AAAA,MACF;AAGA,UAAI,CAAC,SAAS,OAAO,cAAc;AACjC,YAAI;AACF,gBAAM,SAAS,MAAM,OAAO,aAAa,YAAY;AACrD,cAAI,OAAQ,SAAQ;AAAA,QACtB,QAAQ;AAAA,QAER;AAAA,MACF;AAEA,8BAAwB,aAAa,QAAQ,MAAM,SAAS;AAE5D,cAAQ,KAAK;AAAA,QACX;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,QAAQ;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAEA,gBAAY,iBAAiB,6BAA6B;AAAA,MACxD;AAAA,MACA,YAAY,MAAM;AAAA,MAClB,aAAa,QAAQ;AAAA,MACrB;AAAA,IACF,CAAC;AAED,WAAO,EAAE,SAAS,QAAQ;AAAA,EAC5B;AACF;",
4
+ "sourcesContent": ["import { isEntityIndexStrategyAllowed } from '../lib/entity-strategy-policy'\nimport type { SearchService } from '../service'\nimport type {\n SearchModuleConfig,\n SearchEntityConfig,\n SearchBuildContext,\n IndexableRecord,\n SearchResultPresenter,\n SearchResultLink,\n} from '../types'\nimport type { FullTextSearchStrategy } from '../strategies/fulltext.strategy'\nimport type { EntityId } from '@open-mercato/shared/modules/entities'\nimport type { QueryEngine } from '@open-mercato/shared/lib/query/types'\nimport type { Queue } from '@open-mercato/queue'\nimport type { FulltextIndexJobPayload } from '../queue/fulltext-indexing'\nimport type { VectorIndexJobPayload, VectorBatchRecord } from '../queue/vector-indexing'\nimport { reportSearchError, searchDebug, searchDebugWarn, searchError, searchWarn } from '../lib/debug'\nimport type { EncryptedCustomFieldKeysResolver } from '../lib/encrypted-custom-field-resolver'\nimport { normalizeDefinitionKey } from '@open-mercato/shared/lib/crud/custom-field-definition-index'\n\n/**\n * Maximum number of pages to process during reindex to prevent infinite loops.\n * At 50 records per page, this allows up to 500,000 records per entity.\n */\nconst MAX_PAGES = 10000\n\n/**\n * Custom fields kept out of the build context: the normalized keys of encrypted\n * definitions, every custom field when they could not be resolved, or none.\n */\ntype CustomFieldExclusion = Set<string> | 'all' | null\n\ntype CustomFieldExclusionLookup = (entityId: EntityId, tenantId: string | null) => Promise<CustomFieldExclusion>\n\nfunction toPlaintextExcludedCustomFields(\n exclusion: CustomFieldExclusion,\n): IndexableRecord['plaintextExcludedCustomFields'] {\n if (!exclusion) return undefined\n return exclusion === 'all' ? 'all' : Array.from(exclusion)\n}\n\nfunction mergeCustomFieldExclusions(exclusions: CustomFieldExclusion[]): CustomFieldExclusion {\n if (exclusions.includes('all')) return 'all'\n const sets = exclusions.filter((exclusion): exclusion is Set<string> => exclusion instanceof Set)\n if (!sets.length) return null\n if (sets.length === 1) return sets[0]\n return new Set(sets.flatMap((set) => Array.from(set)))\n}\n\nfunction isExcludedCustomField(key: string, exclusion: CustomFieldExclusion): boolean {\n if (!exclusion) return false\n if (exclusion === 'all') return true\n return exclusion.has(normalizeDefinitionKey(key))\n}\n\nfunction withoutExcludedRecordCustomFields(\n record: Record<string, unknown>,\n exclusion: CustomFieldExclusion,\n): Record<string, unknown> {\n if (!exclusion) return record\n const filtered: Record<string, unknown> = {}\n let removed = false\n for (const [key, value] of Object.entries(record)) {\n const isCustomField = key.startsWith('cf:') || key.startsWith('cf_')\n if (isCustomField && isExcludedCustomField(key.slice(3), exclusion)) {\n removed = true\n continue\n }\n filtered[key] = value\n }\n return removed ? filtered : record\n}\n\n/**\n * Build callbacks may enrich the record they receive (for example with a hydrated parent);\n * those changes reach the indexed fields as they did before the record was filtered.\n */\nfunction applyBuildRecordChanges(\n buildRecord: Record<string, unknown>,\n record: Record<string, unknown>,\n exclusion: CustomFieldExclusion,\n): void {\n if (buildRecord === record) return\n for (const [key, value] of Object.entries(buildRecord)) {\n if (record[key] !== value) record[key] = value\n }\n for (const key of Object.keys(record)) {\n if (key in buildRecord) continue\n const isCustomField = key.startsWith('cf:') || key.startsWith('cf_')\n if (isCustomField && isExcludedCustomField(key.slice(3), exclusion)) continue\n delete record[key]\n }\n}\n\nfunction withoutExcludedCustomFields(\n customFields: Record<string, unknown>,\n exclusion: CustomFieldExclusion,\n): Record<string, unknown> {\n if (!exclusion) return customFields\n const filtered: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(customFields)) {\n const isPrefixed = key.startsWith('cf:') || key.startsWith('cf_')\n if (isExcludedCustomField(key, exclusion)) continue\n if (isPrefixed && isExcludedCustomField(key.slice(3), exclusion)) continue\n filtered[key] = value\n }\n return filtered\n}\n\n/**\n * Scoped reindexes clear only the reindexed entity's (or organization's) documents. A driver\n * that can do neither `purge` nor `listDocumentIds` would keep deleted records, and recreating\n * the whole tenant index instead would drop every other entity and organization, so such a\n * reindex fails up front.\n */\nfunction canScopedPurge(fulltext: FullTextSearchStrategy): boolean {\n return typeof fulltext.canPurge !== 'function' || fulltext.canPurge()\n}\n\nfunction scopedPurgeUnsupportedError(fulltext: FullTextSearchStrategy): string {\n return `Fulltext driver \"${fulltext.driverId}\" cannot clear documents per entity (implement purge or listDocumentIds); reindex aborted to avoid keeping stale documents`\n}\n\n/**\n * Parameters for indexing a record.\n */\nexport type IndexRecordParams = {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n record: Record<string, unknown>\n customFields?: Record<string, unknown>\n}\n\ntype IndexRecordByIdParams = {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Parameters for indexing a batch of records by id in a single bulk write.\n */\nexport type IndexRecordsByIdParams = {\n items: Array<{ entityId: EntityId; recordId: string }>\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Parameters for deleting a record from the search index.\n */\nexport type DeleteRecordParams = {\n entityId: EntityId\n recordId: string\n tenantId: string\n}\n\n/**\n * Parameters for purging all records of an entity type.\n */\nexport type PurgeEntityParams = {\n entityId: EntityId\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Parameters for reindexing an entity to fulltext search.\n */\nexport type ReindexEntityParams = {\n entityId: EntityId\n tenantId: string\n organizationId?: string | null\n /** Whether to clear this entity's documents (scoped to `organizationId` when set) first (default: true) */\n recreateIndex?: boolean\n /** Callback for progress tracking */\n onProgress?: (progress: ReindexProgress) => void\n /** Whether to use queue for batch processing (default: false) */\n useQueue?: boolean\n}\n\n/**\n * Parameters for reindexing all entities to fulltext search.\n */\nexport type ReindexAllParams = {\n tenantId: string\n organizationId?: string | null\n /** Whether to clear indexed documents first (default: true): the whole tenant index, or only `organizationId`'s documents when set */\n recreateIndex?: boolean\n /** Callback for progress tracking */\n onProgress?: (progress: ReindexProgress) => void\n /** Whether to use queue for batch processing (default: false) */\n useQueue?: boolean\n}\n\n/**\n * Progress information during reindex.\n */\nexport type ReindexProgress = {\n entityId: EntityId\n phase: 'starting' | 'fetching' | 'indexing' | 'complete'\n processed: number\n total?: number\n}\n\n/**\n * Result of a reindex operation.\n */\nexport type ReindexResult = {\n success: boolean\n entitiesProcessed: number\n recordsIndexed: number\n /** Number of records dropped due to missing id or other validation failures */\n recordsDropped?: number\n /** Number of jobs enqueued (when useQueue is true) */\n jobsEnqueued?: number\n errors: Array<{ entityId: EntityId; error: string }>\n}\n\n/**\n * Optional dependencies for SearchIndexer.\n */\nexport type SearchIndexerOptions = {\n queryEngine?: QueryEngine\n /** Queue for fulltext batch indexing */\n fulltextQueue?: Queue<FulltextIndexJobPayload>\n /** Queue for vector batch indexing */\n vectorQueue?: Queue<VectorIndexJobPayload>\n /**\n * Resolves the custom fields declared `encrypted` for an entity, so their values never reach\n * buildSource, formatResult, resolveUrl or resolveLinks and stay out of the plain text a\n * strategy stores or embeds. Without it, every custom field is left out of those callbacks.\n * `IndexableRecord.fields` keeps them for hash-only tokens.\n */\n encryptedCustomFieldKeysResolver?: EncryptedCustomFieldKeysResolver\n}\n\n/**\n * SearchIndexer orchestrates indexing operations by resolving entity configs\n * and building IndexableRecords for the SearchService.\n */\nexport class SearchIndexer {\n private readonly entityConfigMap: Map<EntityId, SearchEntityConfig>\n private readonly queryEngine?: QueryEngine\n private readonly fulltextQueue?: Queue<FulltextIndexJobPayload>\n private readonly vectorQueue?: Queue<VectorIndexJobPayload>\n private readonly encryptedCustomFieldKeysResolver?: EncryptedCustomFieldKeysResolver\n\n constructor(\n private readonly searchService: SearchService,\n private readonly moduleConfigs: SearchModuleConfig[],\n options?: SearchIndexerOptions,\n ) {\n this.entityConfigMap = new Map()\n this.queryEngine = options?.queryEngine\n this.fulltextQueue = options?.fulltextQueue\n this.vectorQueue = options?.vectorQueue\n this.encryptedCustomFieldKeysResolver = options?.encryptedCustomFieldKeysResolver\n if (!this.encryptedCustomFieldKeysResolver) {\n searchWarn('SearchIndexer', 'No encrypted custom-field resolver; excluding every custom field from build contexts')\n reportSearchError(\n 'SearchIndexer',\n new Error('[internal] SearchIndexer has no encryptedCustomFieldKeysResolver; excluding every custom field from build contexts'),\n 'search.encrypted_custom_fields_resolver_missing',\n )\n }\n for (const moduleConfig of moduleConfigs) {\n for (const entityConfig of moduleConfig.entities) {\n if (entityConfig.enabled !== false) {\n this.entityConfigMap.set(entityConfig.entityId as EntityId, entityConfig)\n }\n }\n }\n }\n\n /**\n * Returns a lookup of the custom fields to keep out of build contexts, memoized per entity and\n * tenant for one indexing call or reindex run. A missing resolver or a failed resolution excludes\n * every custom field. Failed resolutions stay cached for the current operation and are retried by\n * the fresh lookup created for the next top-level indexing call.\n */\n private createCustomFieldExclusionLookup(): CustomFieldExclusionLookup {\n const resolver = this.encryptedCustomFieldKeysResolver\n const cache = new Map<string, Promise<CustomFieldExclusion>>()\n return (entityId, tenantId) => {\n if (!resolver) return Promise.resolve('all')\n const cacheKey = `${entityId}\\u0000${tenantId ?? ''}`\n let pending = cache.get(cacheKey)\n if (!pending) {\n pending = Promise.resolve().then(() => resolver(entityId, tenantId)).catch((error): CustomFieldExclusion => {\n const resolutionError = error instanceof Error\n ? error\n : new Error('[internal] Encrypted custom-field resolver rejected with a non-Error value')\n searchWarn('SearchIndexer', 'Encrypted custom fields could not be resolved; excluding every custom field', {\n entityId,\n error: resolutionError.message,\n })\n reportSearchError('SearchIndexer', resolutionError, 'search.encrypted_custom_fields_unresolved', { entityId })\n return 'all'\n })\n cache.set(cacheKey, pending)\n }\n return pending\n }\n }\n\n /**\n * Returns a wrapped QueryEngine that forces skipAutoReindex: true on every query.\n * Used to prevent feedback loops when search indexing callbacks (buildSource, formatResult)\n * call queryEngine.query() to hydrate related entities. Encrypted custom fields are removed\n * from the hydrated records, as from the indexed record itself, including those joined in\n * from `customFieldSources`. A query without a tenant is checked against every tenant's definitions.\n */\n private createBuildQueryEngine(exclusions: CustomFieldExclusionLookup): QueryEngine | undefined {\n if (!this.queryEngine) return undefined\n const queryEngine = this.queryEngine\n const wrapped: QueryEngine = {\n query: async (entity, opts) => {\n const result = await queryEngine.query(entity, { ...opts, skipAutoReindex: true })\n const sourceEntityIds = [entity, ...(opts?.customFieldSources ?? []).map((source) => source.entityId)]\n const tenantId = opts?.tenantId ?? null\n const exclusion = mergeCustomFieldExclusions(\n await Promise.all(sourceEntityIds.map((sourceEntityId) => exclusions(sourceEntityId as EntityId, tenantId))),\n )\n if (!exclusion || !Array.isArray(result?.items)) return result\n return {\n ...result,\n items: result.items.map((item: Record<string, unknown>) => withoutExcludedRecordCustomFields(item, exclusion)),\n }\n },\n }\n return wrapped\n }\n\n /**\n * Get the entity config for a given entity ID.\n */\n getEntityConfig(entityId: EntityId): SearchEntityConfig | undefined {\n return this.entityConfigMap.get(entityId)\n }\n\n /**\n * Get all configured entity configs.\n */\n getAllEntityConfigs(): SearchEntityConfig[] {\n return Array.from(this.entityConfigMap.values())\n }\n\n /**\n * Check if an entity is configured for search indexing.\n */\n isEntityEnabled(entityId: EntityId): boolean {\n const config = this.entityConfigMap.get(entityId)\n return config?.enabled !== false\n }\n\n /**\n * Index a record in the search service.\n */\n async indexRecord(params: IndexRecordParams): Promise<void> {\n await this.indexRecordWithExclusions(params, this.createCustomFieldExclusionLookup())\n }\n\n private async indexRecordWithExclusions(\n params: IndexRecordParams,\n exclusions: CustomFieldExclusionLookup,\n ): Promise<void> {\n const indexableRecord = await this.buildIndexableRecord(params, exclusions)\n if (indexableRecord) {\n await this.searchService.index(indexableRecord)\n }\n }\n\n private async buildIndexableRecord(\n params: IndexRecordParams,\n exclusions: CustomFieldExclusionLookup = this.createCustomFieldExclusionLookup(),\n ): Promise<IndexableRecord | null> {\n const config = this.entityConfigMap.get(params.entityId)\n if (!config || config.enabled === false) {\n return null\n }\n\n const exclusion = await exclusions(params.entityId, params.tenantId)\n const buildContext: SearchBuildContext = {\n record: withoutExcludedRecordCustomFields(params.record, exclusion),\n customFields: withoutExcludedCustomFields(params.customFields ?? {}, exclusion),\n organizationId: params.organizationId,\n tenantId: params.tenantId,\n queryEngine: this.createBuildQueryEngine(exclusions),\n }\n\n // Try buildSource first (provides text, presenter, links, checksumSource)\n let text: string | string[] | undefined = config.buildSource ? [] : undefined\n let presenter: SearchResultPresenter | undefined\n let url: string | undefined\n let links: SearchResultLink[] | undefined\n let checksumSource: unknown | undefined\n\n if (config.buildSource) {\n try {\n const source = await config.buildSource(buildContext)\n if (source) {\n text = source.text ?? []\n if (source.presenter) presenter = source.presenter\n if (source.links) links = source.links\n if (source.checksumSource !== undefined) checksumSource = source.checksumSource\n }\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'buildSource failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Fall back to formatResult if no presenter from buildSource\n if (!presenter && config.formatResult) {\n try {\n const result = await config.formatResult(buildContext)\n if (result) presenter = result\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'formatResult failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Resolve URL if not already set\n if (!url && config.resolveUrl) {\n try {\n const result = await config.resolveUrl(buildContext)\n if (result) url = result\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'resolveUrl failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Resolve links if not already set\n if (!links && config.resolveLinks) {\n try {\n const result = await config.resolveLinks(buildContext)\n if (result) links = result\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'resolveLinks failed', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n applyBuildRecordChanges(buildContext.record, params.record, exclusion)\n\n return {\n entityId: params.entityId,\n recordId: params.recordId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n fields: params.record,\n plaintextExcludedCustomFields: toPlaintextExcludedCustomFields(exclusion),\n presenter,\n url,\n links,\n text,\n checksumSource,\n }\n }\n\n /**\n * Index a record by ID (loads the record from database first).\n * Used by workers that only have record identifiers.\n */\n async indexRecordById(params: {\n entityId: EntityId\n recordId: string\n tenantId: string\n organizationId?: string | null\n }): Promise<{ action: 'indexed' | 'skipped'; reason?: string }> {\n return this.indexRecordByIdWithExclusions(params, this.createCustomFieldExclusionLookup())\n }\n\n private async indexRecordByIdWithExclusions(\n params: IndexRecordByIdParams,\n exclusions: CustomFieldExclusionLookup,\n ): Promise<{ action: 'indexed' | 'skipped'; reason?: string }> {\n if (!this.queryEngine) {\n return { action: 'skipped', reason: 'queryEngine not available' }\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config || config.enabled === false) {\n return { action: 'skipped', reason: 'entity not configured' }\n }\n\n // Load record from database\n try {\n const result = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n filters: { id: params.recordId },\n includeCustomFields: true,\n page: { page: 1, pageSize: 1 },\n skipAutoReindex: true,\n })\n\n const record = result.items[0] as Record<string, unknown> | undefined\n if (!record) {\n return { action: 'skipped', reason: 'record not found' }\n }\n\n // Extract custom fields\n const customFields: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(record)) {\n if (key.startsWith('cf:') || key.startsWith('cf_')) {\n customFields[key.slice(3)] = value\n }\n }\n\n await this.indexRecordWithExclusions({\n entityId: params.entityId,\n recordId: params.recordId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n record,\n customFields,\n }, exclusions)\n\n return { action: 'indexed' }\n } catch (error) {\n searchError('SearchIndexer', 'Failed to load record for indexing', {\n entityId: params.entityId,\n recordId: params.recordId,\n error: error instanceof Error ? error.message : error,\n })\n throw error\n }\n }\n\n /**\n * Index a batch of records by id in a single bulk write.\n * Unlike calling indexRecordById() in a loop, this loads each record fresh\n * (same as indexRecordById) but flushes the whole batch through a single\n * searchService.bulkIndex() call. Strategies that implement bulkIndex then\n * collapse the batch into one write; strategies without it still write per\n * record, at the bounded concurrency SearchService applies.\n */\n async indexRecordsById(params: IndexRecordsByIdParams): Promise<{ indexed: number; skipped: number }> {\n const { items, tenantId, organizationId } = params\n if (!this.queryEngine || items.length === 0) {\n return { indexed: 0, skipped: items.length }\n }\n\n const recordIdsByEntity = new Map<EntityId, string[]>()\n for (const item of items) {\n const list = recordIdsByEntity.get(item.entityId) ?? []\n list.push(item.recordId)\n recordIdsByEntity.set(item.entityId, list)\n }\n const exclusions = this.createCustomFieldExclusionLookup()\n\n const allRecords: IndexableRecord[] = []\n let skipped = 0\n\n for (const [entityId, recordIds] of recordIdsByEntity) {\n const config = this.entityConfigMap.get(entityId)\n if (!config || config.enabled === false) {\n skipped += recordIds.length\n continue\n }\n\n for (const recordId of recordIds) {\n try {\n const result = await this.queryEngine.query(entityId, {\n tenantId,\n organizationId: organizationId ?? undefined,\n filters: { id: recordId },\n includeCustomFields: true,\n page: { page: 1, pageSize: 1 },\n skipAutoReindex: true,\n })\n\n const record = result.items[0] as Record<string, unknown> | undefined\n if (!record) {\n skipped++\n continue\n }\n\n const { records: built, dropped } = await this.buildIndexableRecords(\n entityId,\n tenantId,\n organizationId ?? null,\n [record],\n config,\n exclusions,\n )\n skipped += dropped\n allRecords.push(...built)\n } catch (error) {\n skipped++\n searchError('SearchIndexer', 'Failed to load record for batch indexing', {\n entityId,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n }\n\n if (allRecords.length > 0) {\n await this.searchService.bulkIndex(allRecords)\n }\n\n return { indexed: allRecords.length, skipped }\n }\n\n /**\n * Delete a record from the search index.\n */\n async deleteRecord(params: DeleteRecordParams): Promise<void> {\n await this.searchService.delete(params.entityId, params.recordId, params.tenantId)\n }\n\n /**\n * Purge all records of an entity type from the search index.\n */\n async purgeEntity(params: PurgeEntityParams): Promise<void> {\n await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)\n }\n\n /**\n * Reindex an entity via all configured strategies (including vector).\n * This is the general reindex method that works with all search strategies.\n */\n async reindexEntity(params: {\n entityId: EntityId\n tenantId: string\n organizationId?: string | null\n purgeFirst?: boolean\n }): Promise<ReindexResult> {\n if (!this.queryEngine) {\n return {\n success: false,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n errors: [{ entityId: params.entityId, error: 'Query engine not available' }],\n }\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config || config.enabled === false) {\n return {\n success: false,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n errors: [{ entityId: params.entityId, error: 'Entity not configured for search' }],\n }\n }\n\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 1,\n recordsIndexed: 0,\n errors: [],\n }\n\n // Optionally purge first\n if (params.purgeFirst) {\n try {\n await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to purge entity before reindex', {\n entityId: params.entityId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Paginate through all records\n const exclusions = this.createCustomFieldExclusionLookup()\n let page = 1\n const pageSize = 200\n let hasMore = true\n\n while (hasMore && page <= MAX_PAGES) {\n try {\n const queryResult = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n includeCustomFields: true,\n page: { page, pageSize },\n skipAutoReindex: true,\n })\n\n const items = queryResult.items as Record<string, unknown>[]\n if (items.length === 0) {\n hasMore = false\n break\n }\n\n // Build and index records\n const { records } = await this.buildIndexableRecords(\n params.entityId,\n params.tenantId,\n params.organizationId ?? null,\n items,\n config,\n exclusions,\n )\n\n // Bulk index records via SearchService (sends to all strategies)\n if (records.length > 0) {\n try {\n await this.searchService.bulkIndex(records)\n result.recordsIndexed += records.length\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to bulk index records', {\n entityId: params.entityId,\n recordCount: records.length,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n page++\n hasMore = items.length === pageSize\n } catch (error) {\n result.success = false\n result.errors.push({\n entityId: params.entityId,\n error: error instanceof Error ? error.message : String(error),\n })\n break\n }\n }\n\n return result\n }\n\n /**\n * Reindex all enabled entities via all configured strategies.\n */\n async reindexAll(params: {\n tenantId: string\n organizationId?: string | null\n purgeFirst?: boolean\n }): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n errors: [],\n }\n\n const enabledEntities = this.listEnabledEntities()\n\n for (const entityId of enabledEntities) {\n const entityResult = await this.reindexEntity({\n entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n purgeFirst: params.purgeFirst,\n })\n\n result.entitiesProcessed++\n result.recordsIndexed += entityResult.recordsIndexed\n result.errors.push(...entityResult.errors)\n\n if (!entityResult.success) {\n result.success = false\n }\n }\n\n return result\n }\n\n /**\n * Bulk index multiple records.\n */\n async bulkIndexRecords(params: IndexRecordParams[]): Promise<void> {\n const indexableRecords: IndexableRecord[] = []\n const exclusions = this.createCustomFieldExclusionLookup()\n\n for (const param of params) {\n const indexableRecord = await this.buildIndexableRecord(param, exclusions)\n if (indexableRecord) {\n indexableRecords.push(indexableRecord)\n }\n }\n\n if (indexableRecords.length > 0) {\n try {\n await this.searchService.bulkIndex(indexableRecords)\n } catch (error) {\n throw new Error(\n `Failed to bulk index ${indexableRecords.length} records: ${\n error instanceof Error ? error.message : error\n }`\n )\n }\n }\n }\n\n /**\n * List all enabled entity IDs from the module configurations.\n */\n listEnabledEntities(): EntityId[] {\n return Array.from(this.entityConfigMap.keys())\n }\n\n /**\n * Get the fulltext strategy from the search service.\n */\n private getFulltextStrategy(): FullTextSearchStrategy | undefined {\n const strategy = this.searchService.getStrategy('fulltext')\n if (!strategy) return undefined\n return strategy as FullTextSearchStrategy\n }\n\n /**\n * Reindex a single entity type to fulltext search.\n * This fetches all records from the database and re-indexes them to fulltext only.\n *\n * When `useQueue` is true, batches are enqueued for background processing by workers.\n * When `useQueue` is false (default), batches are indexed directly (blocking).\n */\n async reindexEntityToFulltext(params: ReindexEntityParams): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n if (!isEntityIndexStrategyAllowed(params.entityId, 'fulltext', this.moduleConfigs)) return result\n\n const fulltext = this.getFulltextStrategy()\n if (!fulltext) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Fulltext strategy not available' })\n return result\n }\n\n // If useQueue is requested but no queue is available, return error\n if (params.useQueue && !this.fulltextQueue) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Fulltext queue not configured for queue-based reindexing' })\n return result\n }\n\n if (!this.queryEngine) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'QueryEngine not available for reindexing' })\n return result\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Entity not configured for search' })\n return result\n }\n\n try {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'starting',\n processed: 0,\n })\n\n if (params.recreateIndex !== false) {\n if (!canScopedPurge(fulltext)) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: scopedPurgeUnsupportedError(fulltext) })\n return result\n }\n await fulltext.purge(params.entityId, params.tenantId, params.organizationId ?? null)\n }\n\n // Fetch and index records with pagination\n const exclusions = this.createCustomFieldExclusionLookup()\n const pageSize = 200\n let page = 1\n let totalProcessed = 0\n let jobsEnqueued = 0\n\n for (;;) {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'fetching',\n processed: totalProcessed,\n })\n\n try {\n const queryResult = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n page: { page, pageSize },\n skipAutoReindex: true,\n })\n\n if (!queryResult.items.length) {\n break\n }\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'indexing',\n processed: totalProcessed,\n total: queryResult.total,\n })\n\n // Build IndexableRecords for this batch\n const { records: indexableRecords, dropped } = await this.buildIndexableRecords(\n params.entityId,\n params.tenantId,\n params.organizationId ?? null,\n queryResult.items,\n config,\n exclusions,\n )\n result.recordsDropped = (result.recordsDropped ?? 0) + dropped\n\n // Index to fulltext - either via queue or directly\n if (indexableRecords.length > 0) {\n if (params.useQueue && this.fulltextQueue) {\n // Enqueue batch for background processing - only pass minimal references\n // Worker will load fresh data from entity_indexes table\n await this.fulltextQueue.enqueue({\n jobType: 'batch-index',\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n records: indexableRecords.map((r) => ({ entityId: r.entityId, recordId: r.recordId })),\n })\n jobsEnqueued += 1\n totalProcessed += indexableRecords.length\n } else {\n // Direct indexing (blocking)\n try {\n await fulltext.bulkIndex(indexableRecords)\n totalProcessed += indexableRecords.length\n } catch (indexError) {\n // Log error but continue with remaining batches\n const errorMsg = indexError instanceof Error ? indexError.message : String(indexError)\n result.errors.push({\n entityId: params.entityId,\n error: `Batch ${page} failed: ${errorMsg}`,\n })\n }\n }\n }\n\n if (queryResult.items.length < pageSize) {\n break\n }\n page += 1\n\n // Safety check to prevent infinite loops\n if (page > MAX_PAGES) {\n break\n }\n } catch (queryError) {\n const errorMsg = queryError instanceof Error ? queryError.message : String(queryError)\n result.errors.push({\n entityId: params.entityId,\n error: `Query failed: ${errorMsg}`,\n })\n break\n }\n }\n\n result.entitiesProcessed = 1\n result.recordsIndexed = totalProcessed\n result.jobsEnqueued = jobsEnqueued\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'complete',\n processed: totalProcessed,\n total: totalProcessed,\n })\n } catch (error) {\n result.success = false\n result.errors.push({\n entityId: params.entityId,\n error: error instanceof Error ? error.message : String(error),\n })\n }\n\n return result\n }\n\n /**\n * Reindex all enabled entities to fulltext search.\n *\n * When `useQueue` is true, batches are enqueued for background processing by workers.\n * When `useQueue` is false (default), batches are indexed directly (blocking).\n */\n async reindexAllToFulltext(params: ReindexAllParams): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n const fulltext = this.getFulltextStrategy()\n if (!fulltext) {\n result.success = false\n result.errors.push({ entityId: 'all' as EntityId, error: 'Fulltext strategy not available' })\n return result\n }\n\n const clearIndex = params.recreateIndex !== false\n const organizationScoped = typeof params.organizationId === 'string' && params.organizationId.trim().length > 0\n if (clearIndex && !organizationScoped) {\n await fulltext.recreateIndex(params.tenantId)\n }\n if (clearIndex && organizationScoped && !canScopedPurge(fulltext)) {\n result.success = false\n result.errors.push({ entityId: 'all' as EntityId, error: scopedPurgeUnsupportedError(fulltext) })\n return result\n }\n\n const entities = this.listEnabledEntities()\n\n for (const entityId of entities) {\n const entityResult = await this.reindexEntityToFulltext({\n entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n recreateIndex: clearIndex && organizationScoped,\n onProgress: params.onProgress,\n useQueue: params.useQueue,\n })\n\n result.entitiesProcessed += entityResult.entitiesProcessed\n result.recordsIndexed += entityResult.recordsIndexed\n result.recordsDropped = (result.recordsDropped ?? 0) + (entityResult.recordsDropped ?? 0)\n result.jobsEnqueued = (result.jobsEnqueued ?? 0) + (entityResult.jobsEnqueued ?? 0)\n result.errors.push(...entityResult.errors)\n\n if (!entityResult.success) {\n result.success = false\n }\n }\n\n return result\n }\n\n /**\n * Reindex a single entity type to vector search.\n * This fetches all records from the database and enqueues them for vector indexing.\n *\n * When `useQueue` is true (default), record IDs are enqueued for background processing by workers.\n * When `useQueue` is false, records are indexed directly (blocking).\n */\n async reindexEntityToVector(params: ReindexEntityParams & { purgeFirst?: boolean }): Promise<ReindexResult> {\n searchDebug('SearchIndexer', 'reindexEntityToVector called', {\n entityId: params.entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n useQueue: params.useQueue,\n purgeFirst: params.purgeFirst,\n })\n\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n if (!isEntityIndexStrategyAllowed(params.entityId, 'vector', this.moduleConfigs)) return result\n\n // If useQueue is requested but no queue is available, return error\n if (params.useQueue !== false && !this.vectorQueue) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Vector queue not configured for queue-based reindexing' })\n return result\n }\n\n if (!this.queryEngine) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'QueryEngine not available for reindexing' })\n return result\n }\n\n const config = this.entityConfigMap.get(params.entityId)\n if (!config) {\n result.success = false\n result.errors.push({ entityId: params.entityId, error: 'Entity not configured for search' })\n return result\n }\n\n try {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'starting',\n processed: 0,\n })\n\n // Optionally purge vector index first\n if (params.purgeFirst) {\n try {\n await this.searchService.purge(params.entityId, params.tenantId, params.organizationId)\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to purge entity before vector reindex', {\n entityId: params.entityId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n\n // Fetch and enqueue records with pagination\n const pageSize = 200\n let page = 1\n let totalProcessed = 0\n let jobsEnqueued = 0\n const exclusions = this.createCustomFieldExclusionLookup()\n\n for (;;) {\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'fetching',\n processed: totalProcessed,\n })\n\n const queryResult = await this.queryEngine.query(params.entityId, {\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? undefined,\n page: { page, pageSize },\n skipAutoReindex: true,\n })\n\n if (!queryResult.items.length) break\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'indexing',\n processed: totalProcessed,\n total: queryResult.total,\n })\n\n // Build batch of record references\n const batchRecords: VectorBatchRecord[] = []\n for (const item of queryResult.items) {\n const recordId = String((item as Record<string, unknown>).id ?? '')\n if (!recordId) {\n result.recordsDropped = (result.recordsDropped ?? 0) + 1\n continue\n }\n batchRecords.push({\n entityId: params.entityId,\n recordId,\n })\n }\n\n // Enqueue batch for background processing or index directly\n if (batchRecords.length > 0) {\n if (params.useQueue !== false && this.vectorQueue) {\n await this.vectorQueue.enqueue({\n jobType: 'batch-index',\n tenantId: params.tenantId,\n organizationId: params.organizationId ?? null,\n records: batchRecords,\n })\n jobsEnqueued += 1\n totalProcessed += batchRecords.length\n searchDebug('SearchIndexer', 'Enqueued batch for vector indexing', {\n entityId: params.entityId,\n batchSize: batchRecords.length,\n jobsEnqueued,\n totalProcessed,\n })\n } else {\n // Direct indexing (blocking) - index each record via SearchService\n for (const { entityId, recordId } of batchRecords) {\n try {\n await this.indexRecordByIdWithExclusions({\n entityId: entityId as EntityId,\n recordId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n }, exclusions)\n totalProcessed++\n } catch (error) {\n searchDebugWarn('SearchIndexer', 'Failed to index record to vector', {\n entityId,\n recordId,\n error: error instanceof Error ? error.message : error,\n })\n }\n }\n }\n }\n\n if (queryResult.items.length < pageSize) break\n page += 1\n\n // Safety check to prevent infinite loops\n if (page > MAX_PAGES) {\n searchDebugWarn('SearchIndexer', 'Reached MAX_PAGES limit, stopping pagination', {\n entityId: params.entityId,\n maxPages: MAX_PAGES,\n totalProcessed,\n })\n break\n }\n }\n\n result.entitiesProcessed = 1\n result.recordsIndexed = totalProcessed\n result.jobsEnqueued = jobsEnqueued\n\n params.onProgress?.({\n entityId: params.entityId,\n phase: 'complete',\n processed: totalProcessed,\n total: totalProcessed,\n })\n } catch (error) {\n result.success = false\n result.errors.push({\n entityId: params.entityId,\n error: error instanceof Error ? error.message : String(error),\n })\n }\n\n return result\n }\n\n /**\n * Reindex all enabled entities to vector search.\n *\n * When `useQueue` is true (default), batches are enqueued for background processing by workers.\n * When `useQueue` is false, batches are indexed directly (blocking).\n */\n async reindexAllToVector(params: ReindexAllParams & { purgeFirst?: boolean }): Promise<ReindexResult> {\n const result: ReindexResult = {\n success: true,\n entitiesProcessed: 0,\n recordsIndexed: 0,\n recordsDropped: 0,\n jobsEnqueued: 0,\n errors: [],\n }\n\n const entities = this.listEnabledEntities()\n for (const entityId of entities) {\n const entityResult = await this.reindexEntityToVector({\n entityId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n onProgress: params.onProgress,\n useQueue: params.useQueue,\n purgeFirst: params.purgeFirst,\n })\n\n result.entitiesProcessed += entityResult.entitiesProcessed\n result.recordsIndexed += entityResult.recordsIndexed\n result.recordsDropped = (result.recordsDropped ?? 0) + (entityResult.recordsDropped ?? 0)\n result.jobsEnqueued = (result.jobsEnqueued ?? 0) + (entityResult.jobsEnqueued ?? 0)\n result.errors.push(...entityResult.errors)\n\n if (!entityResult.success) {\n result.success = false\n }\n }\n\n return result\n }\n\n /**\n * Build IndexableRecords from raw query results.\n * Returns records and count of dropped items (missing id or other validation failures).\n */\n private async buildIndexableRecords(\n entityId: EntityId,\n tenantId: string,\n organizationId: string | null,\n items: Record<string, unknown>[],\n config: SearchEntityConfig,\n exclusions: CustomFieldExclusionLookup = this.createCustomFieldExclusionLookup(),\n ): Promise<{ records: IndexableRecord[]; dropped: number }> {\n const records: IndexableRecord[] = []\n let dropped = 0\n const exclusion = await exclusions(entityId, tenantId)\n const plaintextExcludedCustomFields = toPlaintextExcludedCustomFields(exclusion)\n const buildQueryEngine = this.createBuildQueryEngine(exclusions)\n\n if (items.length > 0) {\n searchDebug('SearchIndexer', 'Sample item structure', {\n entityId,\n sampleKeys: Object.keys(items[0]),\n hasId: 'id' in items[0],\n })\n }\n\n for (const item of items) {\n const recordId = String(item.id ?? '')\n if (!recordId) {\n searchDebugWarn('SearchIndexer', 'Skipping item without id', { entityId, itemKeys: Object.keys(item) })\n dropped++\n continue\n }\n\n // Extract custom fields from record\n const customFields: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(item)) {\n if (key.startsWith('cf:') || key.startsWith('cf_')) {\n const cfKey = key.slice(3) // Remove 'cf:' or 'cf_' prefix (both are 3 chars)\n customFields[cfKey] = value\n }\n }\n\n const buildContext: SearchBuildContext = {\n record: withoutExcludedRecordCustomFields(item, exclusion),\n customFields: withoutExcludedCustomFields(customFields, exclusion),\n organizationId,\n tenantId,\n queryEngine: buildQueryEngine,\n }\n\n // Try buildSource first (provides text, presenter, links, checksumSource)\n let text: string | string[] | undefined = config.buildSource ? [] : undefined\n let presenter: SearchResultPresenter | undefined\n let url: string | undefined\n let links: SearchResultLink[] | undefined\n let checksumSource: unknown | undefined\n\n if (config.buildSource) {\n try {\n const source = await config.buildSource(buildContext)\n if (source) {\n text = source.text ?? []\n if (source.presenter) presenter = source.presenter\n if (source.links) links = source.links\n if (source.checksumSource !== undefined) checksumSource = source.checksumSource\n }\n } catch (err) {\n searchDebugWarn('SearchIndexer', 'buildSource failed', {\n entityId,\n recordId,\n error: err instanceof Error ? err.message : err,\n })\n }\n }\n\n // Fall back to formatResult if no presenter from buildSource\n if (!presenter && config.formatResult) {\n try {\n const result = await config.formatResult(buildContext)\n if (result) presenter = result\n } catch {\n // Skip presenter on error\n }\n }\n\n // Resolve URL if not already set\n if (!url && config.resolveUrl) {\n try {\n const result = await config.resolveUrl(buildContext)\n if (result) url = result\n } catch {\n // Skip URL on error\n }\n }\n\n // Resolve links if not already set\n if (!links && config.resolveLinks) {\n try {\n const result = await config.resolveLinks(buildContext)\n if (result) links = result\n } catch {\n // Skip links on error\n }\n }\n\n applyBuildRecordChanges(buildContext.record, item, exclusion)\n\n records.push({\n entityId,\n recordId,\n tenantId,\n organizationId,\n fields: item,\n plaintextExcludedCustomFields,\n presenter,\n url,\n links,\n text,\n checksumSource,\n })\n }\n\n searchDebug('SearchIndexer', 'Finished building records', {\n entityId,\n inputCount: items.length,\n outputCount: records.length,\n dropped,\n })\n\n return { records, dropped }\n }\n}\n"],
5
+ "mappings": "AAAA,SAAS,oCAAoC;AAgB7C,SAAS,mBAAmB,aAAa,iBAAiB,aAAa,kBAAkB;AAEzF,SAAS,8BAA8B;AAMvC,MAAM,YAAY;AAUlB,SAAS,gCACP,WACkD;AAClD,MAAI,CAAC,UAAW,QAAO;AACvB,SAAO,cAAc,QAAQ,QAAQ,MAAM,KAAK,SAAS;AAC3D;AAEA,SAAS,2BAA2B,YAA0D;AAC5F,MAAI,WAAW,SAAS,KAAK,EAAG,QAAO;AACvC,QAAM,OAAO,WAAW,OAAO,CAAC,cAAwC,qBAAqB,GAAG;AAChG,MAAI,CAAC,KAAK,OAAQ,QAAO;AACzB,MAAI,KAAK,WAAW,EAAG,QAAO,KAAK,CAAC;AACpC,SAAO,IAAI,IAAI,KAAK,QAAQ,CAAC,QAAQ,MAAM,KAAK,GAAG,CAAC,CAAC;AACvD;AAEA,SAAS,sBAAsB,KAAa,WAA0C;AACpF,MAAI,CAAC,UAAW,QAAO;AACvB,MAAI,cAAc,MAAO,QAAO;AAChC,SAAO,UAAU,IAAI,uBAAuB,GAAG,CAAC;AAClD;AAEA,SAAS,kCACP,QACA,WACyB;AACzB,MAAI,CAAC,UA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AI,OAAQ,OAAM;AAAA,QACpB,QAAQ;AAAA,QAER;AAAA,MACF;AAGA,UAAI,CAAC,SAAS,OAAO,cAAc;AACjC,YAAI;AACF,gBAAM,SAAS,MAAM,OAAO,aAAa,YAAY;AACrD,cAAI,OAAQ,SAAQ;AAAA,QACtB,QAAQ;AAAA,QAER;AAAA,MACF;AAEA,8BAAwB,aAAa,QAAQ,MAAM,SAAS;AAE5D,cAAQ,KAAK;AAAA,QACX;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,QAAQ;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAEA,gBAAY,iBAAiB,6BAA6B;AAAA,MACxD;AAAA,MACA,YAAY,MAAM;AAAA,MAClB,aAAa,QAAQ;AAAA,MACrB;AAAA,IACF,CAAC;AAED,WAAO,EAAE,SAAS,QAAQ;AAAA,EAC5B;AACF;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,187 @@
1
+ import { expect, test } from "@playwright/test";
2
+ import { apiRequest, getAuthToken } from "@open-mercato/core/helpers/integration/api";
3
+ import { createOrganizationFixture } from "@open-mercato/core/helpers/integration/authFixtures";
4
+ import { withClient } from "@open-mercato/core/helpers/integration/dbFixtures";
5
+ import {
6
+ deleteGeneralEntityIfExists,
7
+ expectId,
8
+ getTokenContext,
9
+ readJsonSafe
10
+ } from "@open-mercato/core/helpers/integration/generalFixtures";
11
+ import { drainIntegrationQueue } from "@open-mercato/core/helpers/integration/queue";
12
+ const COMPANY_ENTITY = "customers:customer_company_profile";
13
+ const FULLTEXT_QUEUE = "fulltext-indexing";
14
+ function appBaseUrl() {
15
+ const baseUrl = new URL(process.env.BASE_URL || "http://localhost:3000");
16
+ if (baseUrl.hostname === "127.0.0.1") baseUrl.hostname = "localhost";
17
+ return baseUrl.origin;
18
+ }
19
+ function scopedRequest(request, method, path, options) {
20
+ return request.fetch(`${appBaseUrl()}${path}`, {
21
+ method,
22
+ headers: {
23
+ Authorization: `Bearer ${options.token}`,
24
+ Cookie: [
25
+ `om_selected_tenant=${encodeURIComponent(options.scope.tenantId)}`,
26
+ `om_selected_org=${encodeURIComponent(options.scope.organizationId)}`
27
+ ].join("; "),
28
+ "Content-Type": "application/json",
29
+ Origin: appBaseUrl()
30
+ },
31
+ data: options.data
32
+ });
33
+ }
34
+ async function countFulltextHits(request, token, scope, name) {
35
+ const params = new URLSearchParams({ q: name, limit: "10", strategies: "fulltext", entityTypes: COMPANY_ENTITY });
36
+ const response = await scopedRequest(request, "GET", `/api/search/search?${params.toString()}`, { token, scope });
37
+ if (!response.ok()) return -1;
38
+ const body = await readJsonSafe(response) ?? {};
39
+ const results = Array.isArray(body.results) ? body.results : [];
40
+ return results.filter((result) => result.entityId === COMPANY_ENTITY && result.presenter?.title === name).length;
41
+ }
42
+ async function createCompany(request, token, scope, name) {
43
+ const response = await scopedRequest(request, "POST", "/api/customers/companies", {
44
+ token,
45
+ scope,
46
+ data: { displayName: name }
47
+ });
48
+ expect(response.status(), `company fixture ${name} should be created`).toBe(201);
49
+ const body = await readJsonSafe(response);
50
+ return expectId(body?.id ?? body?.entityId, "company create response should contain an id");
51
+ }
52
+ async function reindex(request, token, scope, data) {
53
+ await scopedRequest(request, "POST", "/api/search/reindex/cancel", { token, scope }).catch(() => void 0);
54
+ const response = await scopedRequest(request, "POST", "/api/search/reindex", { token, scope, data });
55
+ const body = await readJsonSafe(response);
56
+ expect(response.status(), `reindex ${JSON.stringify(data)} should succeed: ${JSON.stringify(body)}`).toBe(200);
57
+ expect(body?.ok, `reindex ${JSON.stringify(data)} should report ok`).toBe(true);
58
+ if (data.useQueue) await drainIntegrationQueue(FULLTEXT_QUEUE);
59
+ }
60
+ async function removeCompanyBehindTheIndex(companyEntityId) {
61
+ await withClient(async (client) => {
62
+ const removed = await client.query(
63
+ "delete from customer_companies where entity_id = $1 returning id",
64
+ [companyEntityId]
65
+ );
66
+ expect(removed.rowCount, "stale company profile should be removed").toBe(1);
67
+ const recordIds = [companyEntityId, ...removed.rows.map((row) => row.id)];
68
+ await client.query("delete from entity_indexes where entity_id = any($1::text[])", [recordIds]);
69
+ await client.query("update customer_entities set deleted_at = now() where id = $1", [companyEntityId]);
70
+ });
71
+ }
72
+ test.describe("TC-SEARCH-016: organization-scoped fulltext reindex keeps other organizations and tenants searchable", () => {
73
+ test("entity and full reindex, direct and queued, only touch the reindexed organization", async ({ request }) => {
74
+ test.slow();
75
+ const token = await getAuthToken(request, "superadmin");
76
+ const homeTenantId = getTokenContext(token).tenantId;
77
+ const stamp = Date.now();
78
+ const companyIds = [];
79
+ let organizationAId = null;
80
+ let organizationBId = null;
81
+ let otherTenantId = null;
82
+ let otherOrganizationId = null;
83
+ try {
84
+ organizationAId = await createOrganizationFixture(request, token, {
85
+ name: `QA TC-SEARCH-016 A ${stamp}`,
86
+ tenantId: homeTenantId
87
+ });
88
+ organizationBId = await createOrganizationFixture(request, token, {
89
+ name: `QA TC-SEARCH-016 B ${stamp}`,
90
+ tenantId: homeTenantId
91
+ });
92
+ const scopeA = { tenantId: homeTenantId, organizationId: organizationAId };
93
+ const scopeB = { tenantId: homeTenantId, organizationId: organizationBId };
94
+ const settingsResponse = await scopedRequest(request, "GET", "/api/search/settings", { token, scope: scopeA });
95
+ expect(settingsResponse.ok()).toBeTruthy();
96
+ const settings = await readJsonSafe(settingsResponse) ?? {};
97
+ const strategies = Array.isArray(settings.settings?.strategies) ? settings.settings.strategies : [];
98
+ if (!strategies.some((strategy) => strategy.id === "fulltext" && strategy.available === true)) {
99
+ test.skip(true, "Fulltext strategy is not available in this environment");
100
+ return;
101
+ }
102
+ const tenantResponse = await apiRequest(request, "POST", "/api/directory/tenants", {
103
+ token,
104
+ data: { name: `QA TC-SEARCH-016 Tenant ${stamp}` }
105
+ });
106
+ expect(tenantResponse.status(), "tenant fixture should be created").toBe(201);
107
+ otherTenantId = expectId((await readJsonSafe(tenantResponse))?.id, "tenant id");
108
+ otherOrganizationId = await createOrganizationFixture(request, token, {
109
+ name: `QA TC-SEARCH-016 C ${stamp}`,
110
+ tenantId: otherTenantId
111
+ });
112
+ const scopeOtherTenant = { tenantId: otherTenantId, organizationId: otherOrganizationId };
113
+ const names = {
114
+ aLive: `QATCSEARCH016ALIVE${stamp}`,
115
+ b: `QATCSEARCH016B${stamp}`,
116
+ otherTenant: `QATCSEARCH016T${stamp}`
117
+ };
118
+ const phases = [
119
+ { label: "entity reindex, direct", data: { entityId: COMPANY_ENTITY, useQueue: false } },
120
+ { label: "entity reindex, queued", data: { entityId: COMPANY_ENTITY, useQueue: true } },
121
+ { label: "full reindex, direct", data: { useQueue: false } },
122
+ { label: "full reindex, queued", data: { useQueue: true } }
123
+ ].map((phase, index) => ({ ...phase, staleName: `QATCSEARCH016S${index}${stamp}`, staleId: "" }));
124
+ const track = async (scope, name) => {
125
+ const id = await createCompany(request, token, scope, name);
126
+ companyIds.push({ id, scope });
127
+ return id;
128
+ };
129
+ await track(scopeA, names.aLive);
130
+ for (const phase of phases) phase.staleId = await track(scopeA, phase.staleName);
131
+ await track(scopeB, names.b);
132
+ await track(scopeOtherTenant, names.otherTenant);
133
+ for (const scope of [scopeB, scopeOtherTenant, scopeA]) {
134
+ await reindex(request, token, scope, { entityId: COMPANY_ENTITY, useQueue: false });
135
+ }
136
+ const expectSearchable = async (scope, name, message) => {
137
+ await expect.poll(() => countFulltextHits(request, token, scope, name), { message, timeout: 3e4 }).toBe(1);
138
+ };
139
+ await expectSearchable(scopeA, names.aLive, "organization A company should be indexed");
140
+ for (const phase of phases) await expectSearchable(scopeA, phase.staleName, "organization A fixture should be indexed");
141
+ expect(
142
+ await countFulltextHits(request, token, scopeB, names.b),
143
+ "seeding organization A must keep the organization B documents indexed just before"
144
+ ).toBe(1);
145
+ await expectSearchable(scopeOtherTenant, names.otherTenant, "other tenant company should be indexed");
146
+ for (const phase of phases) {
147
+ await removeCompanyBehindTheIndex(phase.staleId);
148
+ expect(
149
+ await countFulltextHits(request, token, scopeA, phase.staleName),
150
+ `${phase.label}: the removed company must still be indexed before the reindex (stale document)`
151
+ ).toBe(1);
152
+ await reindex(request, token, scopeA, phase.data);
153
+ await expect.poll(() => countFulltextHits(request, token, scopeA, phase.staleName), {
154
+ message: `${phase.label}: the stale organization A document must be cleared`,
155
+ timeout: 3e4
156
+ }).toBe(0);
157
+ await expectSearchable(scopeA, names.aLive, `${phase.label}: the live organization A company must be rebuilt`);
158
+ expect(
159
+ await countFulltextHits(request, token, scopeB, names.b),
160
+ `${phase.label}: organization B must keep its documents`
161
+ ).toBe(1);
162
+ expect(
163
+ await countFulltextHits(request, token, scopeOtherTenant, names.otherTenant),
164
+ `${phase.label}: the other tenant must keep its documents`
165
+ ).toBe(1);
166
+ }
167
+ } finally {
168
+ for (const { id, scope } of companyIds) {
169
+ await scopedRequest(request, "DELETE", `/api/customers/companies?id=${encodeURIComponent(id)}`, {
170
+ token,
171
+ scope
172
+ }).catch(() => void 0);
173
+ }
174
+ for (const scope of [
175
+ organizationAId ? { tenantId: homeTenantId, organizationId: organizationAId } : null,
176
+ otherTenantId && otherOrganizationId ? { tenantId: otherTenantId, organizationId: otherOrganizationId } : null
177
+ ]) {
178
+ if (scope) await scopedRequest(request, "POST", "/api/search/reindex/cancel", { token, scope }).catch(() => void 0);
179
+ }
180
+ await deleteGeneralEntityIfExists(request, token, "/api/directory/organizations", organizationAId);
181
+ await deleteGeneralEntityIfExists(request, token, "/api/directory/organizations", organizationBId);
182
+ await deleteGeneralEntityIfExists(request, token, "/api/directory/organizations", otherOrganizationId);
183
+ await deleteGeneralEntityIfExists(request, token, "/api/directory/tenants", otherTenantId);
184
+ }
185
+ });
186
+ });
187
+ //# sourceMappingURL=TC-SEARCH-016.spec.js.map