graphile-search 2.20.2 → 2.20.4

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
@@ -66,7 +66,7 @@ A search orderBy sorts by the score its filter computes, so it needs that filter
66
66
 
67
67
  When a column is scored by both its own filter (e.g. `bm25Body`) and `unifiedSearch`, the explicit filter's score is the one displayed and ordered by; the column is scored once per query.
68
68
 
69
- Limitation: in a *literal* `orderBy` list, plain column terms are planned before search-score terms, so `[EMBEDDING_VECTOR_DISTANCE_ASC, ROW_ID_ASC]` sorts by `ROW_ID` first. Pass `orderBy` as a GraphQL variable to keep the requested order, or do not mix search terms with column terms in a literal list.
69
+ Mixed column and search terms are applied in list order, whether `orderBy` is a literal or a GraphQL variable. For example, `[EMBEDDING_VECTOR_DISTANCE_ASC, ROW_ID_ASC]` sorts by distance first and breaks ties by row ID.
70
70
 
71
71
  ## Architecture
72
72
 
package/esm/plugin.js CHANGED
@@ -137,10 +137,9 @@ export function createUnifiedSearchPlugin(options) {
137
137
  const RANKS_WANTED_KEY = 'unified_search_ranks_wanted';
138
138
  // A search orderBy needs the direction (from the orderBy enum) and the
139
139
  // score expression (from the filter). The filter always applies at
140
- // runtime; a literal orderBy applies at plan time, before it, and a
141
- // variable orderBy applies at runtime, after it. Each side stores its
142
- // half in the query builder's meta, which is copied per execution, and
143
- // whichever side runs second adds the ORDER BY.
140
+ // runtime; orderBy applies at plan time for most literals and at runtime
141
+ // for variables and mixed literals. Each side stores its half in the
142
+ // query builder's meta, and whichever side runs second adds the ORDER BY.
144
143
  //
145
144
  // The ORDER BY spec object is kept in meta so a later filter can swap
146
145
  // the expression in place (pgSelect renders `orders` after every apply
@@ -322,6 +321,27 @@ export function createUnifiedSearchPlugin(options) {
322
321
  }
323
322
  return names;
324
323
  }
324
+ function getSearchOrderByRequirements(codec, build) {
325
+ const { inflection } = build;
326
+ const requiredFilters = new Map();
327
+ const anySearchFilter = new Set();
328
+ for (const { adapter, columns } of getAdapterColumns(codec, build)) {
329
+ for (const column of columns) {
330
+ const filters = filtersScoring(adapter, column, inflection);
331
+ filters.forEach((f) => anySearchFilter.add(f));
332
+ for (const ascending of [true, false]) {
333
+ const name = inflection.pgSearchOrderByEnum(codec, column.attributeName, adapter.name, adapter.scoreSemantics.metric, ascending);
334
+ requiredFilters.set(name, filters);
335
+ }
336
+ }
337
+ }
338
+ if (enableSearchScore && requiredFilters.size > 0) {
339
+ const all = [...anySearchFilter];
340
+ requiredFilters.set(inflection.constantCase('search_score_asc'), all);
341
+ requiredFilters.set(inflection.constantCase('search_score_desc'), all);
342
+ }
343
+ return requiredFilters;
344
+ }
325
345
  /** Filter field names present (non-null) anywhere in a `where` value. */
326
346
  function collectFilterFields(where, into) {
327
347
  if (Array.isArray(where)) {
@@ -467,29 +487,12 @@ export function createUnifiedSearchPlugin(options) {
467
487
  const codec = pgFieldResource?.codec;
468
488
  if (!codec?.attributes || codec.isAnonymous)
469
489
  return field;
470
- const adapterColumns = getAdapterColumns(codec, build);
471
- if (adapterColumns.length === 0)
490
+ const requiredFilters = getSearchOrderByRequirements(codec, build);
491
+ if (requiredFilters.size === 0)
472
492
  return field;
473
493
  const plan = field.plan;
474
494
  if (typeof plan !== 'function')
475
495
  return field;
476
- const requiredFilters = new Map();
477
- const anySearchFilter = new Set();
478
- for (const { adapter, columns } of adapterColumns) {
479
- for (const column of columns) {
480
- const filters = filtersScoring(adapter, column, inflection);
481
- filters.forEach((f) => anySearchFilter.add(f));
482
- for (const ascending of [true, false]) {
483
- const name = inflection.pgSearchOrderByEnum(codec, column.attributeName, adapter.name, adapter.scoreSemantics.metric, ascending);
484
- requiredFilters.set(name, filters);
485
- }
486
- }
487
- }
488
- if (enableSearchScore) {
489
- const all = [...anySearchFilter];
490
- requiredFilters.set(inflection.constantCase('search_score_asc'), all);
491
- requiredFilters.set(inflection.constantCase('search_score_desc'), all);
492
- }
493
496
  const compositeOrders = enableSearchScore
494
497
  ? [inflection.constantCase('search_score_asc'), inflection.constantCase('search_score_desc')]
495
498
  : [];
@@ -537,6 +540,36 @@ export function createUnifiedSearchPlugin(options) {
537
540
  },
538
541
  };
539
542
  },
543
+ GraphQLObjectType_fields_field_args_arg(arg, build, context) {
544
+ const { argName, isPgFieldConnection, isPgFieldSimpleCollection, pgFieldResource } = context.scope;
545
+ const { applyPlan } = arg;
546
+ if (argName !== 'orderBy' || !applyPlan || (!isPgFieldConnection && !isPgFieldSimpleCollection))
547
+ return arg;
548
+ const codec = pgFieldResource?.codec;
549
+ if (!codec?.attributes || codec.isAnonymous)
550
+ return arg;
551
+ const requiredFilters = getSearchOrderByRequirements(codec, build);
552
+ if (requiredFilters.size === 0)
553
+ return arg;
554
+ const { applyInput, ConstantStep, lambda } = build.grafast;
555
+ return {
556
+ ...arg,
557
+ applyPlan($parent, $result, input, info) {
558
+ const $orderBy = input.getRaw();
559
+ const values = $orderBy instanceof ConstantStep ? $orderBy.data : null;
560
+ if (!Array.isArray(values) ||
561
+ !values.some((value) => requiredFilters.has(value)) ||
562
+ !values.some((value) => !requiredFilters.has(value))) {
563
+ return applyPlan($parent, $result, input, info);
564
+ }
565
+ // Defer the whole list until the filters have supplied scores,
566
+ // so all enum applies add their order terms in list order.
567
+ const $select = isPgFieldConnection ? $result.getSubplan() : $result;
568
+ const $apply = applyInput(info.arg.type, $orderBy);
569
+ $select.apply(lambda($apply, (apply) => apply, true));
570
+ },
571
+ };
572
+ },
540
573
  /**
541
574
  * Add score/rank/similarity/distance fields for each adapter's columns
542
575
  * on the appropriate output types.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-search",
3
- "version": "2.20.2",
3
+ "version": "2.20.4",
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",
@@ -34,11 +34,11 @@
34
34
  "devDependencies": {
35
35
  "@types/node": "^22.19.11",
36
36
  "@types/pg": "^8.20.4",
37
- "graphile-connection-filter": "^2.19.12",
38
- "graphile-test": "^5.19.12",
37
+ "graphile-connection-filter": "^2.19.13",
38
+ "graphile-test": "^5.19.13",
39
39
  "makage": "^0.8.0",
40
40
  "pg": "^8.21.0",
41
- "pgsql-test": "^5.20.11"
41
+ "pgsql-test": "^5.20.12"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@dataplan/pg": "^1.1.1",
@@ -65,5 +65,5 @@
65
65
  "hybrid-search",
66
66
  "searchScore"
67
67
  ],
68
- "gitHead": "a6b53ff76945ce3542e5e20a61980d74a727e877"
68
+ "gitHead": "e15394db7b2bff2c7c0f4d45af6e223ea6719081"
69
69
  }
package/plugin.js CHANGED
@@ -141,10 +141,9 @@ function createUnifiedSearchPlugin(options) {
141
141
  const RANKS_WANTED_KEY = 'unified_search_ranks_wanted';
142
142
  // A search orderBy needs the direction (from the orderBy enum) and the
143
143
  // score expression (from the filter). The filter always applies at
144
- // runtime; a literal orderBy applies at plan time, before it, and a
145
- // variable orderBy applies at runtime, after it. Each side stores its
146
- // half in the query builder's meta, which is copied per execution, and
147
- // whichever side runs second adds the ORDER BY.
144
+ // runtime; orderBy applies at plan time for most literals and at runtime
145
+ // for variables and mixed literals. Each side stores its half in the
146
+ // query builder's meta, and whichever side runs second adds the ORDER BY.
148
147
  //
149
148
  // The ORDER BY spec object is kept in meta so a later filter can swap
150
149
  // the expression in place (pgSelect renders `orders` after every apply
@@ -326,6 +325,27 @@ function createUnifiedSearchPlugin(options) {
326
325
  }
327
326
  return names;
328
327
  }
328
+ function getSearchOrderByRequirements(codec, build) {
329
+ const { inflection } = build;
330
+ const requiredFilters = new Map();
331
+ const anySearchFilter = new Set();
332
+ for (const { adapter, columns } of getAdapterColumns(codec, build)) {
333
+ for (const column of columns) {
334
+ const filters = filtersScoring(adapter, column, inflection);
335
+ filters.forEach((f) => anySearchFilter.add(f));
336
+ for (const ascending of [true, false]) {
337
+ const name = inflection.pgSearchOrderByEnum(codec, column.attributeName, adapter.name, adapter.scoreSemantics.metric, ascending);
338
+ requiredFilters.set(name, filters);
339
+ }
340
+ }
341
+ }
342
+ if (enableSearchScore && requiredFilters.size > 0) {
343
+ const all = [...anySearchFilter];
344
+ requiredFilters.set(inflection.constantCase('search_score_asc'), all);
345
+ requiredFilters.set(inflection.constantCase('search_score_desc'), all);
346
+ }
347
+ return requiredFilters;
348
+ }
329
349
  /** Filter field names present (non-null) anywhere in a `where` value. */
330
350
  function collectFilterFields(where, into) {
331
351
  if (Array.isArray(where)) {
@@ -471,29 +491,12 @@ function createUnifiedSearchPlugin(options) {
471
491
  const codec = pgFieldResource?.codec;
472
492
  if (!codec?.attributes || codec.isAnonymous)
473
493
  return field;
474
- const adapterColumns = getAdapterColumns(codec, build);
475
- if (adapterColumns.length === 0)
494
+ const requiredFilters = getSearchOrderByRequirements(codec, build);
495
+ if (requiredFilters.size === 0)
476
496
  return field;
477
497
  const plan = field.plan;
478
498
  if (typeof plan !== 'function')
479
499
  return field;
480
- const requiredFilters = new Map();
481
- const anySearchFilter = new Set();
482
- for (const { adapter, columns } of adapterColumns) {
483
- for (const column of columns) {
484
- const filters = filtersScoring(adapter, column, inflection);
485
- filters.forEach((f) => anySearchFilter.add(f));
486
- for (const ascending of [true, false]) {
487
- const name = inflection.pgSearchOrderByEnum(codec, column.attributeName, adapter.name, adapter.scoreSemantics.metric, ascending);
488
- requiredFilters.set(name, filters);
489
- }
490
- }
491
- }
492
- if (enableSearchScore) {
493
- const all = [...anySearchFilter];
494
- requiredFilters.set(inflection.constantCase('search_score_asc'), all);
495
- requiredFilters.set(inflection.constantCase('search_score_desc'), all);
496
- }
497
500
  const compositeOrders = enableSearchScore
498
501
  ? [inflection.constantCase('search_score_asc'), inflection.constantCase('search_score_desc')]
499
502
  : [];
@@ -541,6 +544,36 @@ function createUnifiedSearchPlugin(options) {
541
544
  },
542
545
  };
543
546
  },
547
+ GraphQLObjectType_fields_field_args_arg(arg, build, context) {
548
+ const { argName, isPgFieldConnection, isPgFieldSimpleCollection, pgFieldResource } = context.scope;
549
+ const { applyPlan } = arg;
550
+ if (argName !== 'orderBy' || !applyPlan || (!isPgFieldConnection && !isPgFieldSimpleCollection))
551
+ return arg;
552
+ const codec = pgFieldResource?.codec;
553
+ if (!codec?.attributes || codec.isAnonymous)
554
+ return arg;
555
+ const requiredFilters = getSearchOrderByRequirements(codec, build);
556
+ if (requiredFilters.size === 0)
557
+ return arg;
558
+ const { applyInput, ConstantStep, lambda } = build.grafast;
559
+ return {
560
+ ...arg,
561
+ applyPlan($parent, $result, input, info) {
562
+ const $orderBy = input.getRaw();
563
+ const values = $orderBy instanceof ConstantStep ? $orderBy.data : null;
564
+ if (!Array.isArray(values) ||
565
+ !values.some((value) => requiredFilters.has(value)) ||
566
+ !values.some((value) => !requiredFilters.has(value))) {
567
+ return applyPlan($parent, $result, input, info);
568
+ }
569
+ // Defer the whole list until the filters have supplied scores,
570
+ // so all enum applies add their order terms in list order.
571
+ const $select = isPgFieldConnection ? $result.getSubplan() : $result;
572
+ const $apply = applyInput(info.arg.type, $orderBy);
573
+ $select.apply(lambda($apply, (apply) => apply, true));
574
+ },
575
+ };
576
+ },
544
577
  /**
545
578
  * Add score/rank/similarity/distance fields for each adapter's columns
546
579
  * on the appropriate output types.