graphile-search 2.20.6 → 2.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -62,6 +62,14 @@ then preset `tsConfig`, then request preferences. Configuration names and query
62
62
  text are SQL parameters; request preferences can select only built-in names
63
63
  from the language mapping.
64
64
 
65
+ Vector search follows the same request languages on a table tagged
66
+ `@i18n <translations table>` whose translations table has a vector column of
67
+ the same name (with `<table>_id` and `lang_code` columns). A row is compared
68
+ with the query through its translation in the first requested language it has
69
+ an embedded translation for, and through its own vector otherwise. Without
70
+ request languages, or for a language the row has no translation in, only the
71
+ row's own vector is compared, so another language's translation never matches.
72
+
65
73
  ## Features
66
74
 
67
75
  - **4 search algorithms** via adapters: tsvector (ts_rank), BM25 (pg_textsearch), pg_trgm (similarity), pgvector (distance)
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Translation vectors for a table with translations.
3
+ *
4
+ * A table the DataI18n module translated carries `@i18n <translations table>`.
5
+ * When that translations table has a vector column of the same name (the
6
+ * metaschema gives it one whenever the base table has one), vector search can
7
+ * compare a query with the translation in the request's language.
8
+ */
9
+ export interface I18nVectorInfo {
10
+ schema: string;
11
+ tableName: string;
12
+ /** Translations column referencing the base row (`<base table>_id`). */
13
+ parentFkField: string;
14
+ parentPkField: string;
15
+ langField: string;
16
+ embeddingField: string;
17
+ /** Translations column recording the model each translation vector came from. */
18
+ modelColumn: string | null;
19
+ }
20
+ export declare function getI18nVectorInfo(codec: any, attributeName: string, dimensions: number | undefined, build: any): I18nVectorInfo | undefined;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ /**
3
+ * Translation vectors for a table with translations.
4
+ *
5
+ * A table the DataI18n module translated carries `@i18n <translations table>`.
6
+ * When that translations table has a vector column of the same name (the
7
+ * metaschema gives it one whenever the base table has one), vector search can
8
+ * compare a query with the translation in the request's language.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.getI18nVectorInfo = getI18nVectorInfo;
12
+ function getI18nVectorInfo(codec, attributeName, dimensions, build) {
13
+ const tableName = codec?.extensions?.tags?.i18n;
14
+ const pg = codec?.extensions?.pg;
15
+ if (typeof tableName !== 'string' || !tableName || !pg?.schemaName || !pg?.name)
16
+ return undefined;
17
+ const codecs = Object.values(build?.input?.pgRegistry?.pgCodecs ?? {});
18
+ const translations = codecs.find((c) => c?.attributes && c.extensions?.pg?.schemaName === pg.schemaName && c.extensions?.pg?.name === tableName);
19
+ const attributes = translations?.attributes;
20
+ const vector = attributes?.[attributeName];
21
+ if (vector?.codec?.name !== 'vector')
22
+ return undefined;
23
+ // A translation vector of another width cannot be compared with the query.
24
+ const translationDimensions = vector.extensions?.vectorDimensions;
25
+ if (dimensions && translationDimensions && dimensions !== translationDimensions)
26
+ return undefined;
27
+ const resources = Object.values(build?.input?.pgRegistry?.pgResources ?? {});
28
+ const primaryKey = resources
29
+ .find((r) => r?.codec === codec && !r.parameters)
30
+ ?.uniques?.find((u) => u.isPrimary)?.attributes;
31
+ if (!Array.isArray(primaryKey) || primaryKey.length !== 1)
32
+ return undefined;
33
+ const parentPkField = primaryKey[0];
34
+ const parentFkField = `${pg.name}_id`;
35
+ const langField = 'lang_code';
36
+ if (!attributes[parentFkField] || !attributes[langField])
37
+ return undefined;
38
+ const modelColumn = Object.entries(attributes).find(([, other]) => other.extensions?.tags?.embeddingModelOf === attributeName)?.[0] ??
39
+ null;
40
+ return {
41
+ schema: pg.schemaName,
42
+ tableName,
43
+ parentFkField,
44
+ parentPkField,
45
+ langField,
46
+ embeddingField: attributeName,
47
+ modelColumn,
48
+ };
49
+ }
@@ -9,6 +9,7 @@
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.createPgvectorAdapter = createPgvectorAdapter;
11
11
  const chunks_1 = require("./chunks");
12
+ const i18n_1 = require("./i18n");
12
13
  /**
13
14
  * Build a distance expression for the given metric.
14
15
  * Uses explicit SQL template literals for each operator to avoid sql.raw.
@@ -56,11 +57,13 @@ function createPgvectorAdapter(options = {}) {
56
57
  const named = attribute.extensions?.tags?.embeddingModel;
57
58
  const model = typeof named === 'string' && named ? named : undefined;
58
59
  const modelColumn = Object.entries(codec.attributes).find(([, other]) => other.extensions?.tags?.embeddingModelOf === attributeName)?.[0];
60
+ const i18nInfo = (0, i18n_1.getI18nVectorInfo)(codec, attributeName, dimensions, _build);
59
61
  columns.push({
60
62
  attributeName,
61
- adapterData: chunksInfo || dimensions || model || modelColumn
63
+ adapterData: chunksInfo || dimensions || model || modelColumn || i18nInfo
62
64
  ? {
63
65
  ...(chunksInfo && { chunksInfo }),
66
+ ...(i18nInfo && { i18nInfo }),
64
67
  ...(dimensions && { dimensions }),
65
68
  ...(model && { model }),
66
69
  ...(modelColumn && { modelColumn }),
@@ -163,6 +166,47 @@ function createPgvectorAdapter(options = {}) {
163
166
  // turned into one that filters.
164
167
  const both = (a, b) => (a && b ? sql `(${a}) AND (${b})` : b);
165
168
  const scored = (distance) => sameModel ? sql `CASE WHEN ${sameModel} THEN ${distance} END` : distance;
169
+ // A table with translations (@i18n) is searched in the request's
170
+ // language: a row is compared through its translation in the first
171
+ // requested language it has one embedded for, and through its own
172
+ // vector otherwise. A default-language request finds no translation and
173
+ // compares the base rows only, so another language's text never matches.
174
+ const i18nInfo = adapterData?.i18nInfo;
175
+ const langCodes = adapterData?.langCodes;
176
+ let translationDistance = null;
177
+ if (i18nInfo && langCodes && langCodes.length > 0) {
178
+ const t = sql.identifier('__translation');
179
+ const langs = sql `${sql.value(langCodes)}::text[]`;
180
+ const position = sql `array_position(${langs}, ${t}.${sql.identifier(i18nInfo.langField)}::text)`;
181
+ const translationVector = sql `${t}.${sql.identifier(i18nInfo.embeddingField)}`;
182
+ const translationModel = i18nInfo.modelColumn && typeof model === 'string' && model
183
+ ? sql ` AND ${t}.${sql.identifier(i18nInfo.modelColumn)} = ${sql.value(model)}`
184
+ : sql ``;
185
+ translationDistance = sql `(
186
+ SELECT ${buildDistanceExpr(sql, translationVector, vectorExpr, resolvedMetric)}
187
+ FROM ${sql.identifier(i18nInfo.schema, i18nInfo.tableName)} AS ${t}
188
+ WHERE ${t}.${sql.identifier(i18nInfo.parentFkField)} = ${alias}.${sql.identifier(i18nInfo.parentPkField)}
189
+ AND ${position} IS NOT NULL
190
+ AND ${translationVector} IS NOT NULL${translationModel}
191
+ ORDER BY ${position}
192
+ LIMIT 1
193
+ )`;
194
+ }
195
+ const inRequestLanguage = (baseDistance, baseScorable) => {
196
+ const scoreExpression = translationDistance
197
+ ? sql `COALESCE(${translationDistance}, ${baseDistance})`
198
+ : baseDistance;
199
+ const scorableClause = translationDistance
200
+ ? sql `(${translationDistance} IS NOT NULL OR ${baseScorable ?? sql `true`})`
201
+ : baseScorable;
202
+ return {
203
+ whereClause: distance !== undefined && distance !== null
204
+ ? sql `${scoreExpression} <= ${sql.value(distance)}`
205
+ : null,
206
+ scoreExpression,
207
+ scorableClause,
208
+ };
209
+ };
166
210
  if (chunksInfo && (includeChunks !== false)) {
167
211
  // Chunk-aware query: find the closest chunk for each parent row
168
212
  // Uses a lateral subquery to get the minimum distance across all chunks
@@ -195,19 +239,14 @@ function createPgvectorAdapter(options = {}) {
195
239
  // Rows with no embedding anywhere have no distance to rank: leave them out.
196
240
  // MIN(distance) is NULL exactly when no chunk has an embedding.
197
241
  const scorableClause = sql `(${parentScorable} OR ${chunkDistanceSubquery} IS NOT NULL)`;
198
- let whereClause = null;
199
- if (distance !== undefined && distance !== null) {
200
- whereClause = sql `${combinedDistanceExpr} <= ${sql.value(distance)}`;
201
- }
202
- return {
203
- whereClause,
204
- scoreExpression: combinedDistanceExpr,
205
- scorableClause,
206
- };
242
+ return inRequestLanguage(combinedDistanceExpr, scorableClause);
207
243
  }
208
244
  // Standard (non-chunk) query
209
245
  const columnExpr = sql `${alias}.${sql.identifier(column.attributeName)}`;
210
246
  const distanceExpr = buildDistanceExpr(sql, columnExpr, vectorExpr, resolvedMetric);
247
+ if (translationDistance) {
248
+ return inRequestLanguage(scored(distanceExpr), both(sameModel, sql `${columnExpr} IS NOT NULL`));
249
+ }
211
250
  let whereClause = null;
212
251
  if (distance !== undefined && distance !== null) {
213
252
  whereClause = sql `${distanceExpr} <= ${sql.value(distance)}`;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Translation vectors for a table with translations.
3
+ *
4
+ * A table the DataI18n module translated carries `@i18n <translations table>`.
5
+ * When that translations table has a vector column of the same name (the
6
+ * metaschema gives it one whenever the base table has one), vector search can
7
+ * compare a query with the translation in the request's language.
8
+ */
9
+ export interface I18nVectorInfo {
10
+ schema: string;
11
+ tableName: string;
12
+ /** Translations column referencing the base row (`<base table>_id`). */
13
+ parentFkField: string;
14
+ parentPkField: string;
15
+ langField: string;
16
+ embeddingField: string;
17
+ /** Translations column recording the model each translation vector came from. */
18
+ modelColumn: string | null;
19
+ }
20
+ export declare function getI18nVectorInfo(codec: any, attributeName: string, dimensions: number | undefined, build: any): I18nVectorInfo | undefined;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Translation vectors for a table with translations.
3
+ *
4
+ * A table the DataI18n module translated carries `@i18n <translations table>`.
5
+ * When that translations table has a vector column of the same name (the
6
+ * metaschema gives it one whenever the base table has one), vector search can
7
+ * compare a query with the translation in the request's language.
8
+ */
9
+ export function getI18nVectorInfo(codec, attributeName, dimensions, build) {
10
+ const tableName = codec?.extensions?.tags?.i18n;
11
+ const pg = codec?.extensions?.pg;
12
+ if (typeof tableName !== 'string' || !tableName || !pg?.schemaName || !pg?.name)
13
+ return undefined;
14
+ const codecs = Object.values(build?.input?.pgRegistry?.pgCodecs ?? {});
15
+ const translations = codecs.find((c) => c?.attributes && c.extensions?.pg?.schemaName === pg.schemaName && c.extensions?.pg?.name === tableName);
16
+ const attributes = translations?.attributes;
17
+ const vector = attributes?.[attributeName];
18
+ if (vector?.codec?.name !== 'vector')
19
+ return undefined;
20
+ // A translation vector of another width cannot be compared with the query.
21
+ const translationDimensions = vector.extensions?.vectorDimensions;
22
+ if (dimensions && translationDimensions && dimensions !== translationDimensions)
23
+ return undefined;
24
+ const resources = Object.values(build?.input?.pgRegistry?.pgResources ?? {});
25
+ const primaryKey = resources
26
+ .find((r) => r?.codec === codec && !r.parameters)
27
+ ?.uniques?.find((u) => u.isPrimary)?.attributes;
28
+ if (!Array.isArray(primaryKey) || primaryKey.length !== 1)
29
+ return undefined;
30
+ const parentPkField = primaryKey[0];
31
+ const parentFkField = `${pg.name}_id`;
32
+ const langField = 'lang_code';
33
+ if (!attributes[parentFkField] || !attributes[langField])
34
+ return undefined;
35
+ const modelColumn = Object.entries(attributes).find(([, other]) => other.extensions?.tags?.embeddingModelOf === attributeName)?.[0] ??
36
+ null;
37
+ return {
38
+ schema: pg.schemaName,
39
+ tableName,
40
+ parentFkField,
41
+ parentPkField,
42
+ langField,
43
+ embeddingField: attributeName,
44
+ modelColumn,
45
+ };
46
+ }
@@ -6,6 +6,7 @@
6
6
  * Wraps the same SQL logic as graphile-pgvector but as a SearchAdapter.
7
7
  */
8
8
  import { getChunksInfo } from './chunks';
9
+ import { getI18nVectorInfo } from './i18n';
9
10
  /**
10
11
  * Build a distance expression for the given metric.
11
12
  * Uses explicit SQL template literals for each operator to avoid sql.raw.
@@ -53,11 +54,13 @@ export function createPgvectorAdapter(options = {}) {
53
54
  const named = attribute.extensions?.tags?.embeddingModel;
54
55
  const model = typeof named === 'string' && named ? named : undefined;
55
56
  const modelColumn = Object.entries(codec.attributes).find(([, other]) => other.extensions?.tags?.embeddingModelOf === attributeName)?.[0];
57
+ const i18nInfo = getI18nVectorInfo(codec, attributeName, dimensions, _build);
56
58
  columns.push({
57
59
  attributeName,
58
- adapterData: chunksInfo || dimensions || model || modelColumn
60
+ adapterData: chunksInfo || dimensions || model || modelColumn || i18nInfo
59
61
  ? {
60
62
  ...(chunksInfo && { chunksInfo }),
63
+ ...(i18nInfo && { i18nInfo }),
61
64
  ...(dimensions && { dimensions }),
62
65
  ...(model && { model }),
63
66
  ...(modelColumn && { modelColumn }),
@@ -160,6 +163,47 @@ export function createPgvectorAdapter(options = {}) {
160
163
  // turned into one that filters.
161
164
  const both = (a, b) => (a && b ? sql `(${a}) AND (${b})` : b);
162
165
  const scored = (distance) => sameModel ? sql `CASE WHEN ${sameModel} THEN ${distance} END` : distance;
166
+ // A table with translations (@i18n) is searched in the request's
167
+ // language: a row is compared through its translation in the first
168
+ // requested language it has one embedded for, and through its own
169
+ // vector otherwise. A default-language request finds no translation and
170
+ // compares the base rows only, so another language's text never matches.
171
+ const i18nInfo = adapterData?.i18nInfo;
172
+ const langCodes = adapterData?.langCodes;
173
+ let translationDistance = null;
174
+ if (i18nInfo && langCodes && langCodes.length > 0) {
175
+ const t = sql.identifier('__translation');
176
+ const langs = sql `${sql.value(langCodes)}::text[]`;
177
+ const position = sql `array_position(${langs}, ${t}.${sql.identifier(i18nInfo.langField)}::text)`;
178
+ const translationVector = sql `${t}.${sql.identifier(i18nInfo.embeddingField)}`;
179
+ const translationModel = i18nInfo.modelColumn && typeof model === 'string' && model
180
+ ? sql ` AND ${t}.${sql.identifier(i18nInfo.modelColumn)} = ${sql.value(model)}`
181
+ : sql ``;
182
+ translationDistance = sql `(
183
+ SELECT ${buildDistanceExpr(sql, translationVector, vectorExpr, resolvedMetric)}
184
+ FROM ${sql.identifier(i18nInfo.schema, i18nInfo.tableName)} AS ${t}
185
+ WHERE ${t}.${sql.identifier(i18nInfo.parentFkField)} = ${alias}.${sql.identifier(i18nInfo.parentPkField)}
186
+ AND ${position} IS NOT NULL
187
+ AND ${translationVector} IS NOT NULL${translationModel}
188
+ ORDER BY ${position}
189
+ LIMIT 1
190
+ )`;
191
+ }
192
+ const inRequestLanguage = (baseDistance, baseScorable) => {
193
+ const scoreExpression = translationDistance
194
+ ? sql `COALESCE(${translationDistance}, ${baseDistance})`
195
+ : baseDistance;
196
+ const scorableClause = translationDistance
197
+ ? sql `(${translationDistance} IS NOT NULL OR ${baseScorable ?? sql `true`})`
198
+ : baseScorable;
199
+ return {
200
+ whereClause: distance !== undefined && distance !== null
201
+ ? sql `${scoreExpression} <= ${sql.value(distance)}`
202
+ : null,
203
+ scoreExpression,
204
+ scorableClause,
205
+ };
206
+ };
163
207
  if (chunksInfo && (includeChunks !== false)) {
164
208
  // Chunk-aware query: find the closest chunk for each parent row
165
209
  // Uses a lateral subquery to get the minimum distance across all chunks
@@ -192,19 +236,14 @@ export function createPgvectorAdapter(options = {}) {
192
236
  // Rows with no embedding anywhere have no distance to rank: leave them out.
193
237
  // MIN(distance) is NULL exactly when no chunk has an embedding.
194
238
  const scorableClause = sql `(${parentScorable} OR ${chunkDistanceSubquery} IS NOT NULL)`;
195
- let whereClause = null;
196
- if (distance !== undefined && distance !== null) {
197
- whereClause = sql `${combinedDistanceExpr} <= ${sql.value(distance)}`;
198
- }
199
- return {
200
- whereClause,
201
- scoreExpression: combinedDistanceExpr,
202
- scorableClause,
203
- };
239
+ return inRequestLanguage(combinedDistanceExpr, scorableClause);
204
240
  }
205
241
  // Standard (non-chunk) query
206
242
  const columnExpr = sql `${alias}.${sql.identifier(column.attributeName)}`;
207
243
  const distanceExpr = buildDistanceExpr(sql, columnExpr, vectorExpr, resolvedMetric);
244
+ if (translationDistance) {
245
+ return inRequestLanguage(scored(distanceExpr), both(sameModel, sql `${columnExpr} IS NOT NULL`));
246
+ }
208
247
  let whereClause = null;
209
248
  if (distance !== undefined && distance !== null) {
210
249
  whereClause = sql `${distanceExpr} <= ${sql.value(distance)}`;
package/esm/plugin.js CHANGED
@@ -21,7 +21,7 @@ import 'graphile-build-pg';
21
21
  import 'graphile-connection-filter';
22
22
  import { TYPES } from '@dataplan/pg';
23
23
  import { getQueryBuilder } from 'graphile-plugin-utils';
24
- import { columnWithRequestTsConfig, REQUEST_TS_CONFIG, requestTsConfig } from './ts-config';
24
+ import { columnWithRequestContext, columnWithRequestLangCodes, REQUEST_LANG_CODES, REQUEST_TS_CONFIG, requestLangCodes, requestTsConfig } from './ts-config';
25
25
  /**
26
26
  * Carried on a filter field that searches vector columns, for whatever embeds
27
27
  * the search text (graphile-llm): which columns, and the model each column's
@@ -553,6 +553,7 @@ export function createUnifiedSearchPlugin(options) {
553
553
  const { context, lambda } = build.grafast;
554
554
  $select.apply(lambda(context(), (ctx) => (qb) => {
555
555
  qb.setMeta(REQUEST_TS_CONFIG, requestTsConfig(ctx.langCodes));
556
+ qb.setMeta(REQUEST_LANG_CODES, requestLangCodes(ctx.langCodes));
556
557
  }, true));
557
558
  return applyPlan($parent, $result, input, info);
558
559
  },
@@ -986,7 +987,7 @@ export function createUnifiedSearchPlugin(options) {
986
987
  apply: function plan($condition, val) {
987
988
  if (val == null)
988
989
  return;
989
- const applyFor = (alias) => adapter.buildFilterApply(sql, alias, adapter.name === 'tsv' ? columnWithRequestTsConfig(column, $condition) : column, val, build);
990
+ const applyFor = (alias) => adapter.buildFilterApply(sql, alias, columnWithRequestContext(adapter.name, column, $condition), val, build);
990
991
  const result = applyFor($condition.alias);
991
992
  if (!result)
992
993
  return;
@@ -1061,7 +1062,7 @@ export function createUnifiedSearchPlugin(options) {
1061
1062
  for (const column of columns) {
1062
1063
  // Convert text to adapter-specific filter input
1063
1064
  const filterInput = adapter.buildTextSearchInput(text);
1064
- const applyFor = (alias) => adapter.buildFilterApply(sql, alias, adapter.name === 'tsv' ? columnWithRequestTsConfig(column, $condition) : column, filterInput, build);
1065
+ const applyFor = (alias) => adapter.buildFilterApply(sql, alias, columnWithRequestContext(adapter.name, column, $condition), filterInput, build);
1065
1066
  const result = applyFor($condition.alias);
1066
1067
  if (!result)
1067
1068
  continue;
@@ -1086,7 +1087,7 @@ export function createUnifiedSearchPlugin(options) {
1086
1087
  const declared = column.adapterData?.dimensions;
1087
1088
  if (declared && declared !== query.vector.length)
1088
1089
  continue;
1089
- const applyFor = (alias) => adapter.buildFilterApply(sql, alias, column, { vector: query.vector, model: query.model, metric: 'COSINE' }, build);
1090
+ const applyFor = (alias) => adapter.buildFilterApply(sql, alias, columnWithRequestLangCodes(column, $condition), { vector: query.vector, model: query.model, metric: 'COSINE' }, build);
1090
1091
  const result = applyFor($condition.alias);
1091
1092
  if (!result)
1092
1093
  continue;
@@ -1,5 +1,11 @@
1
1
  import type { SearchableColumn } from './types';
2
2
  export declare const REQUEST_TS_CONFIG = "__graphile_search_ts_config";
3
+ export declare const REQUEST_LANG_CODES = "__graphile_search_lang_codes";
3
4
  export declare function requestTsConfig(langCodes: unknown): string;
4
5
  export declare function conditionTsConfig(condition: any): string | undefined;
5
6
  export declare function columnWithRequestTsConfig(column: SearchableColumn, condition: any): SearchableColumn;
7
+ export declare function requestLangCodes(langCodes: unknown): string[];
8
+ export declare function conditionLangCodes(condition: any): string[] | undefined;
9
+ /** The request's languages, for a vector column whose table has translations (@i18n). */
10
+ export declare function columnWithRequestLangCodes(column: SearchableColumn, condition: any): SearchableColumn;
11
+ export declare function columnWithRequestContext(adapterName: string, column: SearchableColumn, condition: any): SearchableColumn;
package/esm/ts-config.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export const REQUEST_TS_CONFIG = '__graphile_search_ts_config';
2
+ export const REQUEST_LANG_CODES = '__graphile_search_lang_codes';
2
3
  const languageConfigs = new Map(Object.entries({
3
4
  ar: 'arabic', hy: 'armenian', eu: 'basque', ca: 'catalan', da: 'danish',
4
5
  nl: 'dutch', en: 'english', et: 'estonian', fi: 'finnish', fr: 'french',
@@ -39,3 +40,38 @@ export function columnWithRequestTsConfig(column, condition) {
39
40
  },
40
41
  };
41
42
  }
43
+ // Each requested tag is followed by its primary language (fr-CA, fr), so a
44
+ // regional request still finds a translation stored under the bare language.
45
+ export function requestLangCodes(langCodes) {
46
+ if (!Array.isArray(langCodes))
47
+ return [];
48
+ const codes = langCodes.filter((code) => typeof code === 'string' && code !== '');
49
+ return [...new Set(codes.flatMap((code) => [code, code.split('-')[0]]))];
50
+ }
51
+ export function conditionLangCodes(condition) {
52
+ for (let current = condition; current; current = current.parent) {
53
+ const codes = current.getMetaRaw?.(REQUEST_LANG_CODES);
54
+ if (codes !== undefined)
55
+ return codes;
56
+ }
57
+ return undefined;
58
+ }
59
+ /** The request's languages, for a vector column whose table has translations (@i18n). */
60
+ export function columnWithRequestLangCodes(column, condition) {
61
+ if (!column.adapterData?.i18nInfo)
62
+ return column;
63
+ return {
64
+ ...column,
65
+ adapterData: {
66
+ ...column.adapterData,
67
+ langCodes: conditionLangCodes(condition) ?? [],
68
+ },
69
+ };
70
+ }
71
+ export function columnWithRequestContext(adapterName, column, condition) {
72
+ if (adapterName === 'tsv')
73
+ return columnWithRequestTsConfig(column, condition);
74
+ if (adapterName === 'vector')
75
+ return columnWithRequestLangCodes(column, condition);
76
+ return column;
77
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-search",
3
- "version": "2.20.6",
3
+ "version": "2.21.0",
4
4
  "description": "Unified PostGraphile v5 search plugin — abstracts tsvector, BM25, pg_trgm, and pgvector behind a single adapter-based architecture with composite searchScore",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "homepage": "https://github.com/constructive-io/constructive",
@@ -65,5 +65,5 @@
65
65
  "hybrid-search",
66
66
  "searchScore"
67
67
  ],
68
- "gitHead": "bb542c4fbc549dc8a5cced5214da5141fc480cd3"
68
+ "gitHead": "34f95e83e9bb69e1a27dde92abd6ffb0448d065f"
69
69
  }
package/plugin.js CHANGED
@@ -557,6 +557,7 @@ function createUnifiedSearchPlugin(options) {
557
557
  const { context, lambda } = build.grafast;
558
558
  $select.apply(lambda(context(), (ctx) => (qb) => {
559
559
  qb.setMeta(ts_config_1.REQUEST_TS_CONFIG, (0, ts_config_1.requestTsConfig)(ctx.langCodes));
560
+ qb.setMeta(ts_config_1.REQUEST_LANG_CODES, (0, ts_config_1.requestLangCodes)(ctx.langCodes));
560
561
  }, true));
561
562
  return applyPlan($parent, $result, input, info);
562
563
  },
@@ -990,7 +991,7 @@ function createUnifiedSearchPlugin(options) {
990
991
  apply: function plan($condition, val) {
991
992
  if (val == null)
992
993
  return;
993
- const applyFor = (alias) => adapter.buildFilterApply(sql, alias, adapter.name === 'tsv' ? (0, ts_config_1.columnWithRequestTsConfig)(column, $condition) : column, val, build);
994
+ const applyFor = (alias) => adapter.buildFilterApply(sql, alias, (0, ts_config_1.columnWithRequestContext)(adapter.name, column, $condition), val, build);
994
995
  const result = applyFor($condition.alias);
995
996
  if (!result)
996
997
  return;
@@ -1065,7 +1066,7 @@ function createUnifiedSearchPlugin(options) {
1065
1066
  for (const column of columns) {
1066
1067
  // Convert text to adapter-specific filter input
1067
1068
  const filterInput = adapter.buildTextSearchInput(text);
1068
- const applyFor = (alias) => adapter.buildFilterApply(sql, alias, adapter.name === 'tsv' ? (0, ts_config_1.columnWithRequestTsConfig)(column, $condition) : column, filterInput, build);
1069
+ const applyFor = (alias) => adapter.buildFilterApply(sql, alias, (0, ts_config_1.columnWithRequestContext)(adapter.name, column, $condition), filterInput, build);
1069
1070
  const result = applyFor($condition.alias);
1070
1071
  if (!result)
1071
1072
  continue;
@@ -1090,7 +1091,7 @@ function createUnifiedSearchPlugin(options) {
1090
1091
  const declared = column.adapterData?.dimensions;
1091
1092
  if (declared && declared !== query.vector.length)
1092
1093
  continue;
1093
- const applyFor = (alias) => adapter.buildFilterApply(sql, alias, column, { vector: query.vector, model: query.model, metric: 'COSINE' }, build);
1094
+ const applyFor = (alias) => adapter.buildFilterApply(sql, alias, (0, ts_config_1.columnWithRequestLangCodes)(column, $condition), { vector: query.vector, model: query.model, metric: 'COSINE' }, build);
1094
1095
  const result = applyFor($condition.alias);
1095
1096
  if (!result)
1096
1097
  continue;
package/ts-config.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import type { SearchableColumn } from './types';
2
2
  export declare const REQUEST_TS_CONFIG = "__graphile_search_ts_config";
3
+ export declare const REQUEST_LANG_CODES = "__graphile_search_lang_codes";
3
4
  export declare function requestTsConfig(langCodes: unknown): string;
4
5
  export declare function conditionTsConfig(condition: any): string | undefined;
5
6
  export declare function columnWithRequestTsConfig(column: SearchableColumn, condition: any): SearchableColumn;
7
+ export declare function requestLangCodes(langCodes: unknown): string[];
8
+ export declare function conditionLangCodes(condition: any): string[] | undefined;
9
+ /** The request's languages, for a vector column whose table has translations (@i18n). */
10
+ export declare function columnWithRequestLangCodes(column: SearchableColumn, condition: any): SearchableColumn;
11
+ export declare function columnWithRequestContext(adapterName: string, column: SearchableColumn, condition: any): SearchableColumn;
package/ts-config.js CHANGED
@@ -1,10 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.REQUEST_TS_CONFIG = void 0;
3
+ exports.REQUEST_LANG_CODES = exports.REQUEST_TS_CONFIG = void 0;
4
4
  exports.requestTsConfig = requestTsConfig;
5
5
  exports.conditionTsConfig = conditionTsConfig;
6
6
  exports.columnWithRequestTsConfig = columnWithRequestTsConfig;
7
+ exports.requestLangCodes = requestLangCodes;
8
+ exports.conditionLangCodes = conditionLangCodes;
9
+ exports.columnWithRequestLangCodes = columnWithRequestLangCodes;
10
+ exports.columnWithRequestContext = columnWithRequestContext;
7
11
  exports.REQUEST_TS_CONFIG = '__graphile_search_ts_config';
12
+ exports.REQUEST_LANG_CODES = '__graphile_search_lang_codes';
8
13
  const languageConfigs = new Map(Object.entries({
9
14
  ar: 'arabic', hy: 'armenian', eu: 'basque', ca: 'catalan', da: 'danish',
10
15
  nl: 'dutch', en: 'english', et: 'estonian', fi: 'finnish', fr: 'french',
@@ -45,3 +50,38 @@ function columnWithRequestTsConfig(column, condition) {
45
50
  },
46
51
  };
47
52
  }
53
+ // Each requested tag is followed by its primary language (fr-CA, fr), so a
54
+ // regional request still finds a translation stored under the bare language.
55
+ function requestLangCodes(langCodes) {
56
+ if (!Array.isArray(langCodes))
57
+ return [];
58
+ const codes = langCodes.filter((code) => typeof code === 'string' && code !== '');
59
+ return [...new Set(codes.flatMap((code) => [code, code.split('-')[0]]))];
60
+ }
61
+ function conditionLangCodes(condition) {
62
+ for (let current = condition; current; current = current.parent) {
63
+ const codes = current.getMetaRaw?.(exports.REQUEST_LANG_CODES);
64
+ if (codes !== undefined)
65
+ return codes;
66
+ }
67
+ return undefined;
68
+ }
69
+ /** The request's languages, for a vector column whose table has translations (@i18n). */
70
+ function columnWithRequestLangCodes(column, condition) {
71
+ if (!column.adapterData?.i18nInfo)
72
+ return column;
73
+ return {
74
+ ...column,
75
+ adapterData: {
76
+ ...column.adapterData,
77
+ langCodes: conditionLangCodes(condition) ?? [],
78
+ },
79
+ };
80
+ }
81
+ function columnWithRequestContext(adapterName, column, condition) {
82
+ if (adapterName === 'tsv')
83
+ return columnWithRequestTsConfig(column, condition);
84
+ if (adapterName === 'vector')
85
+ return columnWithRequestLangCodes(column, condition);
86
+ return column;
87
+ }