graphile-search 2.19.8 → 2.19.9

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.
@@ -152,15 +152,15 @@ function createPgvectorAdapter(options = {}) {
152
152
  FROM ${chunksTableRef} AS ${chunksAlias}
153
153
  WHERE ${chunksAlias}.${parentFk} = ${parentId}
154
154
  )`;
155
- // Also compute direct parent distance if the parent has an embedding
155
+ // Direct parent distance (NULL when the parent has no embedding)
156
156
  const parentColumnExpr = sql `${alias}.${sql.identifier(column.attributeName)}`;
157
157
  const parentDistanceExpr = buildDistanceExpr(sql, parentColumnExpr, vectorExpr, resolvedMetric);
158
- // Use LEAST of parent distance and closest chunk distance
159
- // COALESCE handles cases where parent or chunks may not have embeddings
160
- const combinedDistanceExpr = sql `LEAST(
161
- COALESCE(${parentDistanceExpr}, 'Infinity'::float),
162
- COALESCE(${chunkDistanceSubquery}, 'Infinity'::float)
163
- )`;
158
+ // LEAST skips NULL operands, so this is the closest of parent + chunks
159
+ // and NULL only when neither the parent nor any chunk is embedded.
160
+ const combinedDistanceExpr = sql `LEAST(${parentDistanceExpr}, ${chunkDistanceSubquery})`;
161
+ // Rows with no embedding anywhere have no distance to rank: leave them out.
162
+ // MIN(distance) is NULL exactly when no chunk has an embedding.
163
+ const scorableClause = sql `(${parentColumnExpr} IS NOT NULL OR ${chunkDistanceSubquery} IS NOT NULL)`;
164
164
  let whereClause = null;
165
165
  if (distance !== undefined && distance !== null) {
166
166
  whereClause = sql `${combinedDistanceExpr} <= ${sql.value(distance)}`;
@@ -168,6 +168,7 @@ function createPgvectorAdapter(options = {}) {
168
168
  return {
169
169
  whereClause,
170
170
  scoreExpression: combinedDistanceExpr,
171
+ scorableClause,
171
172
  };
172
173
  }
173
174
  // Standard (non-chunk) query
@@ -180,6 +181,8 @@ function createPgvectorAdapter(options = {}) {
180
181
  return {
181
182
  whereClause,
182
183
  scoreExpression: distanceExpr,
184
+ // A NULL embedding has no distance to rank: leave the row out.
185
+ scorableClause: sql `${columnExpr} IS NOT NULL`,
183
186
  };
184
187
  },
185
188
  };
@@ -149,15 +149,15 @@ export function createPgvectorAdapter(options = {}) {
149
149
  FROM ${chunksTableRef} AS ${chunksAlias}
150
150
  WHERE ${chunksAlias}.${parentFk} = ${parentId}
151
151
  )`;
152
- // Also compute direct parent distance if the parent has an embedding
152
+ // Direct parent distance (NULL when the parent has no embedding)
153
153
  const parentColumnExpr = sql `${alias}.${sql.identifier(column.attributeName)}`;
154
154
  const parentDistanceExpr = buildDistanceExpr(sql, parentColumnExpr, vectorExpr, resolvedMetric);
155
- // Use LEAST of parent distance and closest chunk distance
156
- // COALESCE handles cases where parent or chunks may not have embeddings
157
- const combinedDistanceExpr = sql `LEAST(
158
- COALESCE(${parentDistanceExpr}, 'Infinity'::float),
159
- COALESCE(${chunkDistanceSubquery}, 'Infinity'::float)
160
- )`;
155
+ // LEAST skips NULL operands, so this is the closest of parent + chunks
156
+ // and NULL only when neither the parent nor any chunk is embedded.
157
+ const combinedDistanceExpr = sql `LEAST(${parentDistanceExpr}, ${chunkDistanceSubquery})`;
158
+ // Rows with no embedding anywhere have no distance to rank: leave them out.
159
+ // MIN(distance) is NULL exactly when no chunk has an embedding.
160
+ const scorableClause = sql `(${parentColumnExpr} IS NOT NULL OR ${chunkDistanceSubquery} IS NOT NULL)`;
161
161
  let whereClause = null;
162
162
  if (distance !== undefined && distance !== null) {
163
163
  whereClause = sql `${combinedDistanceExpr} <= ${sql.value(distance)}`;
@@ -165,6 +165,7 @@ export function createPgvectorAdapter(options = {}) {
165
165
  return {
166
166
  whereClause,
167
167
  scoreExpression: combinedDistanceExpr,
168
+ scorableClause,
168
169
  };
169
170
  }
170
171
  // Standard (non-chunk) query
@@ -177,6 +178,8 @@ export function createPgvectorAdapter(options = {}) {
177
178
  return {
178
179
  whereClause,
179
180
  scoreExpression: distanceExpr,
181
+ // A NULL embedding has no distance to rank: leave the row out.
182
+ scorableClause: sql `${columnExpr} IS NOT NULL`,
180
183
  };
181
184
  },
182
185
  };
package/esm/plugin.js CHANGED
@@ -583,7 +583,9 @@ export function createUnifiedSearchPlugin(options) {
583
583
  qb.setMeta(scoreMetaKey, { selectIndex: scoreIndex });
584
584
  const rankMetaKey = `${scoreMetaKey}__rank`;
585
585
  const orderDirection = adapter.scoreSemantics.lowerIsBetter ? 'ASC' : 'DESC';
586
- const rankSql = sql `(ROW_NUMBER() OVER (ORDER BY ${sql.parens(result.scoreExpression)} ${orderDirection === 'ASC' ? sql.fragment `ASC` : sql.fragment `DESC`} NULLS LAST))::text`;
586
+ // A row without a score has no rank: otherwise ROW_NUMBER() would
587
+ // hand unscored rows the positions right after the scored ones.
588
+ const rankSql = sql `(CASE WHEN ${sql.parens(result.scoreExpression)} IS NULL THEN NULL ELSE ROW_NUMBER() OVER (ORDER BY ${sql.parens(result.scoreExpression)} ${orderDirection === 'ASC' ? sql.fragment `ASC` : sql.fragment `DESC`} NULLS LAST) END)::text`;
587
589
  const rankIndex = qb.selectAndReturnIndex(rankSql);
588
590
  qb.setMeta(rankMetaKey, { selectIndex: rankIndex });
589
591
  const orderKey = `unified_order_${adapter.name}_${baseFieldName}`;
@@ -654,6 +656,10 @@ export function createUnifiedSearchPlugin(options) {
654
656
  if (result.whereClause) {
655
657
  $condition.where(result.whereClause);
656
658
  }
659
+ // Only rows this adapter can score are returned
660
+ if (result.scorableClause) {
661
+ $condition.where(result.scorableClause);
662
+ }
657
663
  const qb = getQueryBuilder(build, $condition);
658
664
  injectScoreAndRank(qb, adapter, column, result, codec, $condition);
659
665
  },
package/esm/types.d.ts CHANGED
@@ -48,6 +48,14 @@ export interface FilterApplyResult {
48
48
  whereClause: SQL | null;
49
49
  /** SQL expression that computes the score/distance/rank for this row */
50
50
  scoreExpression: SQL;
51
+ /**
52
+ * Predicate that holds only for rows this adapter can score, e.g.
53
+ * `embedding IS NOT NULL` for pgvector. The adapter's own filter field ANDs
54
+ * it into WHERE so unscored rows are neither returned nor paged over. The
55
+ * composite `unifiedSearch` filter does not apply it: a row may still match
56
+ * through another adapter.
57
+ */
58
+ scorableClause?: SQL | null;
51
59
  }
52
60
  /**
53
61
  * The core interface that each search algorithm adapter must implement.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-search",
3
- "version": "2.19.8",
3
+ "version": "2.19.9",
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.8",
38
- "graphile-test": "^5.19.8",
37
+ "graphile-connection-filter": "^2.19.9",
38
+ "graphile-test": "^5.19.9",
39
39
  "makage": "^0.8.0",
40
40
  "pg": "^8.21.0",
41
- "pgsql-test": "^5.20.7"
41
+ "pgsql-test": "^5.20.8"
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": "0a2d6c72b58fdbefe92ed954152869afa92bf27e"
68
+ "gitHead": "eb44afc5ecdfa6e91aeb7ad8e24c9021b97ec027"
69
69
  }
package/plugin.js CHANGED
@@ -586,7 +586,9 @@ function createUnifiedSearchPlugin(options) {
586
586
  qb.setMeta(scoreMetaKey, { selectIndex: scoreIndex });
587
587
  const rankMetaKey = `${scoreMetaKey}__rank`;
588
588
  const orderDirection = adapter.scoreSemantics.lowerIsBetter ? 'ASC' : 'DESC';
589
- const rankSql = sql `(ROW_NUMBER() OVER (ORDER BY ${sql.parens(result.scoreExpression)} ${orderDirection === 'ASC' ? sql.fragment `ASC` : sql.fragment `DESC`} NULLS LAST))::text`;
589
+ // A row without a score has no rank: otherwise ROW_NUMBER() would
590
+ // hand unscored rows the positions right after the scored ones.
591
+ const rankSql = sql `(CASE WHEN ${sql.parens(result.scoreExpression)} IS NULL THEN NULL ELSE ROW_NUMBER() OVER (ORDER BY ${sql.parens(result.scoreExpression)} ${orderDirection === 'ASC' ? sql.fragment `ASC` : sql.fragment `DESC`} NULLS LAST) END)::text`;
590
592
  const rankIndex = qb.selectAndReturnIndex(rankSql);
591
593
  qb.setMeta(rankMetaKey, { selectIndex: rankIndex });
592
594
  const orderKey = `unified_order_${adapter.name}_${baseFieldName}`;
@@ -657,6 +659,10 @@ function createUnifiedSearchPlugin(options) {
657
659
  if (result.whereClause) {
658
660
  $condition.where(result.whereClause);
659
661
  }
662
+ // Only rows this adapter can score are returned
663
+ if (result.scorableClause) {
664
+ $condition.where(result.scorableClause);
665
+ }
660
666
  const qb = (0, graphile_plugin_utils_1.getQueryBuilder)(build, $condition);
661
667
  injectScoreAndRank(qb, adapter, column, result, codec, $condition);
662
668
  },
package/types.d.ts CHANGED
@@ -48,6 +48,14 @@ export interface FilterApplyResult {
48
48
  whereClause: SQL | null;
49
49
  /** SQL expression that computes the score/distance/rank for this row */
50
50
  scoreExpression: SQL;
51
+ /**
52
+ * Predicate that holds only for rows this adapter can score, e.g.
53
+ * `embedding IS NOT NULL` for pgvector. The adapter's own filter field ANDs
54
+ * it into WHERE so unscored rows are neither returned nor paged over. The
55
+ * composite `unifiedSearch` filter does not apply it: a row may still match
56
+ * through another adapter.
57
+ */
58
+ scorableClause?: SQL | null;
51
59
  }
52
60
  /**
53
61
  * The core interface that each search algorithm adapter must implement.