graphile-search 2.20.5 → 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 +22 -0
- package/adapters/i18n.d.ts +20 -0
- package/adapters/i18n.js +49 -0
- package/adapters/pgvector.js +49 -10
- package/adapters/tsvector.js +8 -3
- package/codecs/operator-factories.d.ts +1 -1
- package/codecs/operator-factories.js +5 -2
- package/esm/adapters/i18n.d.ts +20 -0
- package/esm/adapters/i18n.js +46 -0
- package/esm/adapters/pgvector.js +49 -10
- package/esm/adapters/tsvector.js +8 -3
- package/esm/codecs/operator-factories.d.ts +1 -1
- package/esm/codecs/operator-factories.js +5 -2
- package/esm/plugin.js +19 -3
- package/esm/preset.js +3 -3
- package/esm/ts-config.d.ts +11 -0
- package/esm/ts-config.js +77 -0
- package/package.json +2 -2
- package/plugin.js +19 -3
- package/preset.js +3 -3
- package/ts-config.d.ts +11 -0
- package/ts-config.js +87 -0
package/README.md
CHANGED
|
@@ -48,6 +48,28 @@ const preset = {
|
|
|
48
48
|
};
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
### Request language
|
|
52
|
+
|
|
53
|
+
Without an explicit configuration, tsvector filters, `unifiedSearch`, and
|
|
54
|
+
`FullText.matches` use the request context's ordered `langCodes`. The first
|
|
55
|
+
supported primary subtag selects a PostgreSQL configuration (`fr-CA` → `french`,
|
|
56
|
+
`de-DE` → `german`); built-in configuration names such as `french` also work.
|
|
57
|
+
Missing, empty, or entirely unsupported preferences retain the English default.
|
|
58
|
+
The server supplies these preferences from HTTP and WebSocket `Accept-Language`.
|
|
59
|
+
|
|
60
|
+
An explicit column `@tsConfig` tag takes precedence over adapter `tsConfig`,
|
|
61
|
+
then preset `tsConfig`, then request preferences. Configuration names and query
|
|
62
|
+
text are SQL parameters; request preferences can select only built-in names
|
|
63
|
+
from the language mapping.
|
|
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
|
+
|
|
51
73
|
## Features
|
|
52
74
|
|
|
53
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;
|
package/adapters/i18n.js
ADDED
|
@@ -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
|
+
}
|
package/adapters/pgvector.js
CHANGED
|
@@ -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
|
-
|
|
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)}`;
|
package/adapters/tsvector.js
CHANGED
|
@@ -22,7 +22,7 @@ function isTsvectorCodec(codec) {
|
|
|
22
22
|
codec?.extensions?.pg?.name === 'tsvector'));
|
|
23
23
|
}
|
|
24
24
|
function createTsvectorAdapter(options = {}) {
|
|
25
|
-
const { filterPrefix = 'tsv', tsConfig
|
|
25
|
+
const { filterPrefix = 'tsv', tsConfig } = options;
|
|
26
26
|
return {
|
|
27
27
|
name: 'tsv',
|
|
28
28
|
scoreSemantics: {
|
|
@@ -46,9 +46,12 @@ function createTsvectorAdapter(options = {}) {
|
|
|
46
46
|
const chunksInfo = (0, chunks_1.getChunksInfo)(codec);
|
|
47
47
|
const hasChunkFulltext = chunksInfo?.searchField &&
|
|
48
48
|
chunksInfo.searchIndexes.includes('fulltext');
|
|
49
|
+
const columnConfig = attribute.extensions?.tags?.tsConfig;
|
|
49
50
|
columns.push({
|
|
50
51
|
attributeName,
|
|
51
|
-
adapterData: hasChunkFulltext
|
|
52
|
+
adapterData: hasChunkFulltext || typeof columnConfig === 'string'
|
|
53
|
+
? { ...(hasChunkFulltext ? chunksInfo : {}), ...(typeof columnConfig === 'string' && { tsConfig: columnConfig }) }
|
|
54
|
+
: undefined,
|
|
52
55
|
});
|
|
53
56
|
}
|
|
54
57
|
}
|
|
@@ -75,7 +78,9 @@ function createTsvectorAdapter(options = {}) {
|
|
|
75
78
|
}
|
|
76
79
|
if (val.trim().length === 0)
|
|
77
80
|
return null;
|
|
78
|
-
const
|
|
81
|
+
const data = column.adapterData;
|
|
82
|
+
const config = data?.tsConfig ?? tsConfig ?? data?.requestTsConfig ?? 'english';
|
|
83
|
+
const tsquery = sql `websearch_to_tsquery(${sql.value(config)}::regconfig, ${sql.value(val)})`;
|
|
79
84
|
const columnExpr = sql `${alias}.${sql.identifier(column.attributeName)}`;
|
|
80
85
|
// Check for chunk-aware querying
|
|
81
86
|
const chunksInfo = column.adapterData;
|
|
@@ -13,7 +13,7 @@ import type { ConnectionFilterOperatorFactory } from 'graphile-connection-filter
|
|
|
13
13
|
* Declared here so it's registered via the declarative
|
|
14
14
|
* `connectionFilterOperatorFactories` API.
|
|
15
15
|
*/
|
|
16
|
-
export declare function createMatchesOperatorFactory(fullTextScalarName: string, tsConfig
|
|
16
|
+
export declare function createMatchesOperatorFactory(fullTextScalarName: string, tsConfig?: string): ConnectionFilterOperatorFactory;
|
|
17
17
|
/**
|
|
18
18
|
* Creates the `similarTo` and `wordSimilarTo` filter operator factories
|
|
19
19
|
* for pg_trgm fuzzy text matching. Declared here so they're registered
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createMatchesOperatorFactory = createMatchesOperatorFactory;
|
|
13
13
|
exports.createTrgmOperatorFactories = createTrgmOperatorFactories;
|
|
14
|
+
const ts_config_1 = require("../ts-config");
|
|
14
15
|
/**
|
|
15
16
|
* Creates the `matches` filter operator factory for full-text search.
|
|
16
17
|
* Declared here so it's registered via the declarative
|
|
@@ -27,8 +28,10 @@ function createMatchesOperatorFactory(fullTextScalarName, tsConfig) {
|
|
|
27
28
|
description: 'Performs a full text search on the field.',
|
|
28
29
|
resolveType: () => GraphQLString,
|
|
29
30
|
resolveInputCodec: TYPES ? () => TYPES.text : undefined,
|
|
30
|
-
resolve(sqlIdentifier, sqlValue, _input,
|
|
31
|
-
|
|
31
|
+
resolve(sqlIdentifier, sqlValue, _input, $where, _details) {
|
|
32
|
+
const columnConfig = $where.extensions?.pgFilterAttribute?.attribute?.extensions?.tags?.tsConfig;
|
|
33
|
+
const config = (typeof columnConfig === 'string' ? columnConfig : undefined) ?? tsConfig ?? (0, ts_config_1.conditionTsConfig)($where) ?? 'english';
|
|
34
|
+
return sql `${sqlIdentifier} @@ websearch_to_tsquery(${sql.value(config)}::regconfig, ${sqlValue})`;
|
|
32
35
|
},
|
|
33
36
|
},
|
|
34
37
|
}];
|
|
@@ -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
|
+
}
|
package/esm/adapters/pgvector.js
CHANGED
|
@@ -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
|
-
|
|
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/adapters/tsvector.js
CHANGED
|
@@ -19,7 +19,7 @@ function isTsvectorCodec(codec) {
|
|
|
19
19
|
codec?.extensions?.pg?.name === 'tsvector'));
|
|
20
20
|
}
|
|
21
21
|
export function createTsvectorAdapter(options = {}) {
|
|
22
|
-
const { filterPrefix = 'tsv', tsConfig
|
|
22
|
+
const { filterPrefix = 'tsv', tsConfig } = options;
|
|
23
23
|
return {
|
|
24
24
|
name: 'tsv',
|
|
25
25
|
scoreSemantics: {
|
|
@@ -43,9 +43,12 @@ export function createTsvectorAdapter(options = {}) {
|
|
|
43
43
|
const chunksInfo = getChunksInfo(codec);
|
|
44
44
|
const hasChunkFulltext = chunksInfo?.searchField &&
|
|
45
45
|
chunksInfo.searchIndexes.includes('fulltext');
|
|
46
|
+
const columnConfig = attribute.extensions?.tags?.tsConfig;
|
|
46
47
|
columns.push({
|
|
47
48
|
attributeName,
|
|
48
|
-
adapterData: hasChunkFulltext
|
|
49
|
+
adapterData: hasChunkFulltext || typeof columnConfig === 'string'
|
|
50
|
+
? { ...(hasChunkFulltext ? chunksInfo : {}), ...(typeof columnConfig === 'string' && { tsConfig: columnConfig }) }
|
|
51
|
+
: undefined,
|
|
49
52
|
});
|
|
50
53
|
}
|
|
51
54
|
}
|
|
@@ -72,7 +75,9 @@ export function createTsvectorAdapter(options = {}) {
|
|
|
72
75
|
}
|
|
73
76
|
if (val.trim().length === 0)
|
|
74
77
|
return null;
|
|
75
|
-
const
|
|
78
|
+
const data = column.adapterData;
|
|
79
|
+
const config = data?.tsConfig ?? tsConfig ?? data?.requestTsConfig ?? 'english';
|
|
80
|
+
const tsquery = sql `websearch_to_tsquery(${sql.value(config)}::regconfig, ${sql.value(val)})`;
|
|
76
81
|
const columnExpr = sql `${alias}.${sql.identifier(column.attributeName)}`;
|
|
77
82
|
// Check for chunk-aware querying
|
|
78
83
|
const chunksInfo = column.adapterData;
|
|
@@ -13,7 +13,7 @@ import type { ConnectionFilterOperatorFactory } from 'graphile-connection-filter
|
|
|
13
13
|
* Declared here so it's registered via the declarative
|
|
14
14
|
* `connectionFilterOperatorFactories` API.
|
|
15
15
|
*/
|
|
16
|
-
export declare function createMatchesOperatorFactory(fullTextScalarName: string, tsConfig
|
|
16
|
+
export declare function createMatchesOperatorFactory(fullTextScalarName: string, tsConfig?: string): ConnectionFilterOperatorFactory;
|
|
17
17
|
/**
|
|
18
18
|
* Creates the `similarTo` and `wordSimilarTo` filter operator factories
|
|
19
19
|
* for pg_trgm fuzzy text matching. Declared here so they're registered
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* They are used in the ConstructivePreset's connectionFilterOperatorFactories
|
|
8
8
|
* array to wire search operators into the declarative filter system.
|
|
9
9
|
*/
|
|
10
|
+
import { conditionTsConfig } from '../ts-config';
|
|
10
11
|
/**
|
|
11
12
|
* Creates the `matches` filter operator factory for full-text search.
|
|
12
13
|
* Declared here so it's registered via the declarative
|
|
@@ -23,8 +24,10 @@ export function createMatchesOperatorFactory(fullTextScalarName, tsConfig) {
|
|
|
23
24
|
description: 'Performs a full text search on the field.',
|
|
24
25
|
resolveType: () => GraphQLString,
|
|
25
26
|
resolveInputCodec: TYPES ? () => TYPES.text : undefined,
|
|
26
|
-
resolve(sqlIdentifier, sqlValue, _input,
|
|
27
|
-
|
|
27
|
+
resolve(sqlIdentifier, sqlValue, _input, $where, _details) {
|
|
28
|
+
const columnConfig = $where.extensions?.pgFilterAttribute?.attribute?.extensions?.tags?.tsConfig;
|
|
29
|
+
const config = (typeof columnConfig === 'string' ? columnConfig : undefined) ?? tsConfig ?? conditionTsConfig($where) ?? 'english';
|
|
30
|
+
return sql `${sqlIdentifier} @@ websearch_to_tsquery(${sql.value(config)}::regconfig, ${sqlValue})`;
|
|
28
31
|
},
|
|
29
32
|
},
|
|
30
33
|
}];
|
package/esm/plugin.js
CHANGED
|
@@ -21,6 +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 { columnWithRequestContext, columnWithRequestLangCodes, REQUEST_LANG_CODES, REQUEST_TS_CONFIG, requestLangCodes, requestTsConfig } from './ts-config';
|
|
24
25
|
/**
|
|
25
26
|
* Carried on a filter field that searches vector columns, for whatever embeds
|
|
26
27
|
* the search text (graphile-llm): which columns, and the model each column's
|
|
@@ -543,6 +544,21 @@ export function createUnifiedSearchPlugin(options) {
|
|
|
543
544
|
GraphQLObjectType_fields_field_args_arg(arg, build, context) {
|
|
544
545
|
const { argName, isPgFieldConnection, isPgFieldSimpleCollection, pgFieldResource } = context.scope;
|
|
545
546
|
const { applyPlan } = arg;
|
|
547
|
+
if (argName === (build.options.connectionFilterArgumentName ?? 'where') && applyPlan &&
|
|
548
|
+
(isPgFieldConnection || isPgFieldSimpleCollection)) {
|
|
549
|
+
return {
|
|
550
|
+
...arg,
|
|
551
|
+
applyPlan($parent, $result, input, info) {
|
|
552
|
+
const $select = isPgFieldConnection ? $result.getSubplan() : $result;
|
|
553
|
+
const { context, lambda } = build.grafast;
|
|
554
|
+
$select.apply(lambda(context(), (ctx) => (qb) => {
|
|
555
|
+
qb.setMeta(REQUEST_TS_CONFIG, requestTsConfig(ctx.langCodes));
|
|
556
|
+
qb.setMeta(REQUEST_LANG_CODES, requestLangCodes(ctx.langCodes));
|
|
557
|
+
}, true));
|
|
558
|
+
return applyPlan($parent, $result, input, info);
|
|
559
|
+
},
|
|
560
|
+
};
|
|
561
|
+
}
|
|
546
562
|
if (argName !== 'orderBy' || !applyPlan || (!isPgFieldConnection && !isPgFieldSimpleCollection))
|
|
547
563
|
return arg;
|
|
548
564
|
const codec = pgFieldResource?.codec;
|
|
@@ -971,7 +987,7 @@ export function createUnifiedSearchPlugin(options) {
|
|
|
971
987
|
apply: function plan($condition, val) {
|
|
972
988
|
if (val == null)
|
|
973
989
|
return;
|
|
974
|
-
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, column, val, build);
|
|
990
|
+
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, columnWithRequestContext(adapter.name, column, $condition), val, build);
|
|
975
991
|
const result = applyFor($condition.alias);
|
|
976
992
|
if (!result)
|
|
977
993
|
return;
|
|
@@ -1046,7 +1062,7 @@ export function createUnifiedSearchPlugin(options) {
|
|
|
1046
1062
|
for (const column of columns) {
|
|
1047
1063
|
// Convert text to adapter-specific filter input
|
|
1048
1064
|
const filterInput = adapter.buildTextSearchInput(text);
|
|
1049
|
-
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, column, filterInput, build);
|
|
1065
|
+
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, columnWithRequestContext(adapter.name, column, $condition), filterInput, build);
|
|
1050
1066
|
const result = applyFor($condition.alias);
|
|
1051
1067
|
if (!result)
|
|
1052
1068
|
continue;
|
|
@@ -1071,7 +1087,7 @@ export function createUnifiedSearchPlugin(options) {
|
|
|
1071
1087
|
const declared = column.adapterData?.dimensions;
|
|
1072
1088
|
if (declared && declared !== query.vector.length)
|
|
1073
1089
|
continue;
|
|
1074
|
-
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);
|
|
1075
1091
|
const result = applyFor($condition.alias);
|
|
1076
1092
|
if (!result)
|
|
1077
1093
|
continue;
|
package/esm/preset.js
CHANGED
|
@@ -29,11 +29,11 @@ import { createUnifiedSearchPlugin } from './plugin';
|
|
|
29
29
|
* Creates a preset that includes the unified search plugin with all enabled adapters.
|
|
30
30
|
*/
|
|
31
31
|
export function UnifiedSearchPreset(options = {}) {
|
|
32
|
-
const { tsvector = true, bm25 = true, trgm = true, pgvector = true, enableSearchScore = true, enableUnifiedSearch = true, searchScoreWeights, fullTextScalarName = 'FullText', tsConfig
|
|
32
|
+
const { tsvector = true, bm25 = true, trgm = true, pgvector = true, enableSearchScore = true, enableUnifiedSearch = true, searchScoreWeights, fullTextScalarName = 'FullText', tsConfig, rrfK, } = options;
|
|
33
33
|
const adapters = [];
|
|
34
34
|
if (tsvector) {
|
|
35
35
|
const opts = typeof tsvector === 'object' ? tsvector : {};
|
|
36
|
-
adapters.push(createTsvectorAdapter(opts));
|
|
36
|
+
adapters.push(createTsvectorAdapter({ ...opts, tsConfig: opts.tsConfig ?? tsConfig }));
|
|
37
37
|
}
|
|
38
38
|
if (bm25) {
|
|
39
39
|
const opts = typeof bm25 === 'object' ? bm25 : {};
|
|
@@ -65,7 +65,7 @@ export function UnifiedSearchPreset(options = {}) {
|
|
|
65
65
|
// Collect operator factories for connection filter integration
|
|
66
66
|
const operatorFactories = [];
|
|
67
67
|
if (tsvector)
|
|
68
|
-
operatorFactories.push(createMatchesOperatorFactory(fullTextScalarName, tsConfig));
|
|
68
|
+
operatorFactories.push(createMatchesOperatorFactory(fullTextScalarName, typeof tsvector === 'object' ? tsvector.tsConfig ?? tsConfig : tsConfig));
|
|
69
69
|
if (trgm)
|
|
70
70
|
operatorFactories.push(createTrgmOperatorFactories());
|
|
71
71
|
return {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SearchableColumn } from './types';
|
|
2
|
+
export declare const REQUEST_TS_CONFIG = "__graphile_search_ts_config";
|
|
3
|
+
export declare const REQUEST_LANG_CODES = "__graphile_search_lang_codes";
|
|
4
|
+
export declare function requestTsConfig(langCodes: unknown): string;
|
|
5
|
+
export declare function conditionTsConfig(condition: any): string | undefined;
|
|
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
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export const REQUEST_TS_CONFIG = '__graphile_search_ts_config';
|
|
2
|
+
export const REQUEST_LANG_CODES = '__graphile_search_lang_codes';
|
|
3
|
+
const languageConfigs = new Map(Object.entries({
|
|
4
|
+
ar: 'arabic', hy: 'armenian', eu: 'basque', ca: 'catalan', da: 'danish',
|
|
5
|
+
nl: 'dutch', en: 'english', et: 'estonian', fi: 'finnish', fr: 'french',
|
|
6
|
+
de: 'german', el: 'greek', hi: 'hindi', hu: 'hungarian', id: 'indonesian',
|
|
7
|
+
ga: 'irish', it: 'italian', lt: 'lithuanian', ne: 'nepali', no: 'norwegian',
|
|
8
|
+
nb: 'norwegian', nn: 'norwegian', pt: 'portuguese', ro: 'romanian',
|
|
9
|
+
ru: 'russian', sr: 'serbian', es: 'spanish', sv: 'swedish', ta: 'tamil',
|
|
10
|
+
tr: 'turkish', yi: 'yiddish',
|
|
11
|
+
}));
|
|
12
|
+
const configNames = new Set([...languageConfigs.values(), 'simple']);
|
|
13
|
+
export function requestTsConfig(langCodes) {
|
|
14
|
+
if (Array.isArray(langCodes)) {
|
|
15
|
+
for (const code of langCodes) {
|
|
16
|
+
if (typeof code !== 'string')
|
|
17
|
+
continue;
|
|
18
|
+
const normalized = code.toLowerCase();
|
|
19
|
+
const name = configNames.has(normalized) ? normalized : languageConfigs.get(normalized.split('-')[0]);
|
|
20
|
+
if (name)
|
|
21
|
+
return `pg_catalog.${name}`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return 'english';
|
|
25
|
+
}
|
|
26
|
+
export function conditionTsConfig(condition) {
|
|
27
|
+
for (let current = condition; current; current = current.parent) {
|
|
28
|
+
const config = current.getMetaRaw?.(REQUEST_TS_CONFIG);
|
|
29
|
+
if (config !== undefined)
|
|
30
|
+
return config;
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
export function columnWithRequestTsConfig(column, condition) {
|
|
35
|
+
return {
|
|
36
|
+
...column,
|
|
37
|
+
adapterData: {
|
|
38
|
+
...(column.adapterData && typeof column.adapterData === 'object' ? column.adapterData : {}),
|
|
39
|
+
requestTsConfig: conditionTsConfig(condition),
|
|
40
|
+
},
|
|
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.
|
|
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": "
|
|
68
|
+
"gitHead": "34f95e83e9bb69e1a27dde92abd6ffb0448d065f"
|
|
69
69
|
}
|
package/plugin.js
CHANGED
|
@@ -25,6 +25,7 @@ require("graphile-build-pg");
|
|
|
25
25
|
require("graphile-connection-filter");
|
|
26
26
|
const pg_1 = require("@dataplan/pg");
|
|
27
27
|
const graphile_plugin_utils_1 = require("graphile-plugin-utils");
|
|
28
|
+
const ts_config_1 = require("./ts-config");
|
|
28
29
|
/**
|
|
29
30
|
* Carried on a filter field that searches vector columns, for whatever embeds
|
|
30
31
|
* the search text (graphile-llm): which columns, and the model each column's
|
|
@@ -547,6 +548,21 @@ function createUnifiedSearchPlugin(options) {
|
|
|
547
548
|
GraphQLObjectType_fields_field_args_arg(arg, build, context) {
|
|
548
549
|
const { argName, isPgFieldConnection, isPgFieldSimpleCollection, pgFieldResource } = context.scope;
|
|
549
550
|
const { applyPlan } = arg;
|
|
551
|
+
if (argName === (build.options.connectionFilterArgumentName ?? 'where') && applyPlan &&
|
|
552
|
+
(isPgFieldConnection || isPgFieldSimpleCollection)) {
|
|
553
|
+
return {
|
|
554
|
+
...arg,
|
|
555
|
+
applyPlan($parent, $result, input, info) {
|
|
556
|
+
const $select = isPgFieldConnection ? $result.getSubplan() : $result;
|
|
557
|
+
const { context, lambda } = build.grafast;
|
|
558
|
+
$select.apply(lambda(context(), (ctx) => (qb) => {
|
|
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));
|
|
561
|
+
}, true));
|
|
562
|
+
return applyPlan($parent, $result, input, info);
|
|
563
|
+
},
|
|
564
|
+
};
|
|
565
|
+
}
|
|
550
566
|
if (argName !== 'orderBy' || !applyPlan || (!isPgFieldConnection && !isPgFieldSimpleCollection))
|
|
551
567
|
return arg;
|
|
552
568
|
const codec = pgFieldResource?.codec;
|
|
@@ -975,7 +991,7 @@ function createUnifiedSearchPlugin(options) {
|
|
|
975
991
|
apply: function plan($condition, val) {
|
|
976
992
|
if (val == null)
|
|
977
993
|
return;
|
|
978
|
-
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, column, val, build);
|
|
994
|
+
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, (0, ts_config_1.columnWithRequestContext)(adapter.name, column, $condition), val, build);
|
|
979
995
|
const result = applyFor($condition.alias);
|
|
980
996
|
if (!result)
|
|
981
997
|
return;
|
|
@@ -1050,7 +1066,7 @@ function createUnifiedSearchPlugin(options) {
|
|
|
1050
1066
|
for (const column of columns) {
|
|
1051
1067
|
// Convert text to adapter-specific filter input
|
|
1052
1068
|
const filterInput = adapter.buildTextSearchInput(text);
|
|
1053
|
-
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, column, filterInput, build);
|
|
1069
|
+
const applyFor = (alias) => adapter.buildFilterApply(sql, alias, (0, ts_config_1.columnWithRequestContext)(adapter.name, column, $condition), filterInput, build);
|
|
1054
1070
|
const result = applyFor($condition.alias);
|
|
1055
1071
|
if (!result)
|
|
1056
1072
|
continue;
|
|
@@ -1075,7 +1091,7 @@ function createUnifiedSearchPlugin(options) {
|
|
|
1075
1091
|
const declared = column.adapterData?.dimensions;
|
|
1076
1092
|
if (declared && declared !== query.vector.length)
|
|
1077
1093
|
continue;
|
|
1078
|
-
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);
|
|
1079
1095
|
const result = applyFor($condition.alias);
|
|
1080
1096
|
if (!result)
|
|
1081
1097
|
continue;
|
package/preset.js
CHANGED
|
@@ -32,11 +32,11 @@ const plugin_1 = require("./plugin");
|
|
|
32
32
|
* Creates a preset that includes the unified search plugin with all enabled adapters.
|
|
33
33
|
*/
|
|
34
34
|
function UnifiedSearchPreset(options = {}) {
|
|
35
|
-
const { tsvector = true, bm25 = true, trgm = true, pgvector = true, enableSearchScore = true, enableUnifiedSearch = true, searchScoreWeights, fullTextScalarName = 'FullText', tsConfig
|
|
35
|
+
const { tsvector = true, bm25 = true, trgm = true, pgvector = true, enableSearchScore = true, enableUnifiedSearch = true, searchScoreWeights, fullTextScalarName = 'FullText', tsConfig, rrfK, } = options;
|
|
36
36
|
const adapters = [];
|
|
37
37
|
if (tsvector) {
|
|
38
38
|
const opts = typeof tsvector === 'object' ? tsvector : {};
|
|
39
|
-
adapters.push((0, tsvector_1.createTsvectorAdapter)(opts));
|
|
39
|
+
adapters.push((0, tsvector_1.createTsvectorAdapter)({ ...opts, tsConfig: opts.tsConfig ?? tsConfig }));
|
|
40
40
|
}
|
|
41
41
|
if (bm25) {
|
|
42
42
|
const opts = typeof bm25 === 'object' ? bm25 : {};
|
|
@@ -68,7 +68,7 @@ function UnifiedSearchPreset(options = {}) {
|
|
|
68
68
|
// Collect operator factories for connection filter integration
|
|
69
69
|
const operatorFactories = [];
|
|
70
70
|
if (tsvector)
|
|
71
|
-
operatorFactories.push((0, operator_factories_1.createMatchesOperatorFactory)(fullTextScalarName, tsConfig));
|
|
71
|
+
operatorFactories.push((0, operator_factories_1.createMatchesOperatorFactory)(fullTextScalarName, typeof tsvector === 'object' ? tsvector.tsConfig ?? tsConfig : tsConfig));
|
|
72
72
|
if (trgm)
|
|
73
73
|
operatorFactories.push((0, operator_factories_1.createTrgmOperatorFactories)());
|
|
74
74
|
return {
|
package/ts-config.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SearchableColumn } from './types';
|
|
2
|
+
export declare const REQUEST_TS_CONFIG = "__graphile_search_ts_config";
|
|
3
|
+
export declare const REQUEST_LANG_CODES = "__graphile_search_lang_codes";
|
|
4
|
+
export declare function requestTsConfig(langCodes: unknown): string;
|
|
5
|
+
export declare function conditionTsConfig(condition: any): string | undefined;
|
|
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
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REQUEST_LANG_CODES = exports.REQUEST_TS_CONFIG = void 0;
|
|
4
|
+
exports.requestTsConfig = requestTsConfig;
|
|
5
|
+
exports.conditionTsConfig = conditionTsConfig;
|
|
6
|
+
exports.columnWithRequestTsConfig = columnWithRequestTsConfig;
|
|
7
|
+
exports.requestLangCodes = requestLangCodes;
|
|
8
|
+
exports.conditionLangCodes = conditionLangCodes;
|
|
9
|
+
exports.columnWithRequestLangCodes = columnWithRequestLangCodes;
|
|
10
|
+
exports.columnWithRequestContext = columnWithRequestContext;
|
|
11
|
+
exports.REQUEST_TS_CONFIG = '__graphile_search_ts_config';
|
|
12
|
+
exports.REQUEST_LANG_CODES = '__graphile_search_lang_codes';
|
|
13
|
+
const languageConfigs = new Map(Object.entries({
|
|
14
|
+
ar: 'arabic', hy: 'armenian', eu: 'basque', ca: 'catalan', da: 'danish',
|
|
15
|
+
nl: 'dutch', en: 'english', et: 'estonian', fi: 'finnish', fr: 'french',
|
|
16
|
+
de: 'german', el: 'greek', hi: 'hindi', hu: 'hungarian', id: 'indonesian',
|
|
17
|
+
ga: 'irish', it: 'italian', lt: 'lithuanian', ne: 'nepali', no: 'norwegian',
|
|
18
|
+
nb: 'norwegian', nn: 'norwegian', pt: 'portuguese', ro: 'romanian',
|
|
19
|
+
ru: 'russian', sr: 'serbian', es: 'spanish', sv: 'swedish', ta: 'tamil',
|
|
20
|
+
tr: 'turkish', yi: 'yiddish',
|
|
21
|
+
}));
|
|
22
|
+
const configNames = new Set([...languageConfigs.values(), 'simple']);
|
|
23
|
+
function requestTsConfig(langCodes) {
|
|
24
|
+
if (Array.isArray(langCodes)) {
|
|
25
|
+
for (const code of langCodes) {
|
|
26
|
+
if (typeof code !== 'string')
|
|
27
|
+
continue;
|
|
28
|
+
const normalized = code.toLowerCase();
|
|
29
|
+
const name = configNames.has(normalized) ? normalized : languageConfigs.get(normalized.split('-')[0]);
|
|
30
|
+
if (name)
|
|
31
|
+
return `pg_catalog.${name}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return 'english';
|
|
35
|
+
}
|
|
36
|
+
function conditionTsConfig(condition) {
|
|
37
|
+
for (let current = condition; current; current = current.parent) {
|
|
38
|
+
const config = current.getMetaRaw?.(exports.REQUEST_TS_CONFIG);
|
|
39
|
+
if (config !== undefined)
|
|
40
|
+
return config;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
function columnWithRequestTsConfig(column, condition) {
|
|
45
|
+
return {
|
|
46
|
+
...column,
|
|
47
|
+
adapterData: {
|
|
48
|
+
...(column.adapterData && typeof column.adapterData === 'object' ? column.adapterData : {}),
|
|
49
|
+
requestTsConfig: conditionTsConfig(condition),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
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
|
+
}
|