@atscript/db-mongo 0.1.184 → 0.1.185

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/dist/index.cjs CHANGED
@@ -1276,13 +1276,6 @@ function filterOptionsOf(host) {
1276
1276
  * name of its own (a dynamic Atlas mapping) is listed as `DEFAULT`. The vector
1277
1277
  * default is the first vector index.
1278
1278
  */
1279
- /** Classic `$text` relevance order: `textScore` descending, `_id` breaking ties (since 0.1.165). */
1280
- const RELEVANCE_SORT = { $sort: {
1281
- _score: -1,
1282
- _id: 1
1283
- } };
1284
- /** The order of an unranked search (`$rank: false`, since 0.1.175): the primary key. */
1285
- const UNRANKED_SORT = { $sort: { _id: 1 } };
1286
1279
  function getSearchIndexesImpl(host) {
1287
1280
  const result = [];
1288
1281
  const indexes = host.getMongoSearchIndexes();
@@ -1339,7 +1332,7 @@ function requireSearchStage(host, text, indexName, controls) {
1339
1332
  * an unresolvable one throws exactly as the leaf path does.
1340
1333
  *
1341
1334
  * `classicText` is deliberately dropped: it only drives the leaf runner's
1342
- * `_score` projection and relevance `$sort`, and no per-document score
1335
+ * relevance order (the `textScore` it sorts by), and no per-document score
1343
1336
  * survives `$group`. See `resolveAggregateSearch` in `@atscript/db/agg` for
1344
1337
  * the normative cross-adapter contract.
1345
1338
  */
@@ -1364,17 +1357,29 @@ async function searchWithCountImpl(host, text, query, indexName) {
1364
1357
  * orders by `_id` instead (an Atlas `$search` included).
1365
1358
  */
1366
1359
  function textRunOptions(plan, query) {
1367
- if (query.controls?.$rank === false) return { defaultSort: UNRANKED_SORT };
1368
- return plan.classicText ? {
1369
- classicText: true,
1370
- defaultSort: RELEVANCE_SORT
1371
- } : {};
1360
+ if (query.controls?.$rank === false) return { defaultOrder: UNRANKED_ORDER };
1361
+ return plan.classicText ? { defaultOrder: RELEVANCE_ORDER } : {};
1372
1362
  }
1373
1363
  /**
1374
- * Field the `vectorSearchScore` rides in through the pipeline. Renamed to the
1375
- * public `$score` after fetch — a field name cannot start with `$`.
1364
+ * Field a search score rides in through the pipeline: the `vectorSearchScore`
1365
+ * (renamed to the public `$score` after fetch — a field name cannot start
1366
+ * with `$`) or the `textScore` of a relevance order (dropped from the rows).
1376
1367
  */
1377
1368
  const SCORE_FIELD = "__atscript_score";
1369
+ /**
1370
+ * Classic `$text` relevance order: `textScore` descending, `_id` breaking ties
1371
+ * (since 0.1.165). The score is dropped after `$skip` / `$limit`, so a row
1372
+ * carries no score field (since 0.1.185; before, rows carried `_score`).
1373
+ */
1374
+ const RELEVANCE_ORDER = {
1375
+ stages: [{ $addFields: { [SCORE_FIELD]: { $meta: "textScore" } } }, { $sort: {
1376
+ [SCORE_FIELD]: -1,
1377
+ _id: 1
1378
+ } }],
1379
+ cleanup: { $project: { [SCORE_FIELD]: 0 } }
1380
+ };
1381
+ /** The order of an unranked search (`$rank: false`, since 0.1.175): the primary key. */
1382
+ const UNRANKED_ORDER = { stages: [{ $sort: { _id: 1 } }] };
1378
1383
  /** Vector search via $vectorSearch aggregation stage. */
1379
1384
  async function vectorSearchImpl(host, vector, query, indexName) {
1380
1385
  const controls = query.controls || {};
@@ -1796,16 +1801,19 @@ function buildVectorSearchStage(host, vector, indexName, limit, skip) {
1796
1801
  index
1797
1802
  };
1798
1803
  }
1804
+ /** The order of a search: an explicit `$sort` (with its `$nulls` placement flags) replaces the default. */
1805
+ function searchOrder(controls, defaultOrder) {
1806
+ const explicit = require_mongo_accumulator.sortStages(controls);
1807
+ return explicit.stages.length > 0 || !defaultOrder ? explicit : defaultOrder;
1808
+ }
1799
1809
  /** Runs a search/vector pipeline and returns results. Shared by search + vectorSearch. */
1800
- async function runSearchPipeline(host, stage, query, label, { vector, classicText = false, defaultSort, filterStages } = {}) {
1810
+ async function runSearchPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
1801
1811
  const controls = query.controls || {};
1802
1812
  const pipeline = [stage];
1803
1813
  if (vector) pipeline.push(...vectorScoreStages(vector.threshold));
1804
- else if (classicText) pipeline.push({ $addFields: { _score: { $meta: "textScore" } } });
1805
1814
  pipeline.push(...filterStages ?? require_mongo_accumulator.mongoFilterStages(query.filter, filterOptionsOf(host)));
1806
- const { stages: sorted, cleanup } = require_mongo_accumulator.sortStages(controls);
1807
- if (sorted.length > 0) pipeline.push(...sorted);
1808
- else if (defaultSort) pipeline.push(defaultSort);
1815
+ const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
1816
+ pipeline.push(...sorted);
1809
1817
  if (controls.$skip) pipeline.push({ $skip: controls.$skip });
1810
1818
  if (controls.$limit) pipeline.push({ $limit: controls.$limit });
1811
1819
  else pipeline.push({ $limit: 1e3 });
@@ -1824,13 +1832,11 @@ async function runSearchPipeline(host, stage, query, label, { vector, classicTex
1824
1832
  * `count: total` — the same counts `$$SEARCH_META` reports), concurrently
1825
1833
  * outside a transaction.
1826
1834
  */
1827
- async function runSearchWithCountPipeline(host, stage, query, label, { vector, classicText = false, defaultSort, filterStages } = {}) {
1835
+ async function runSearchWithCountPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
1828
1836
  const controls = query.controls || {};
1829
- const preStages = [];
1830
- if (vector) preStages.push(...vectorScoreStages(vector.threshold));
1831
- else if (classicText) preStages.push({ $addFields: { _score: { $meta: "textScore" } } });
1832
- const { stages: dataStages, cleanup } = require_mongo_accumulator.sortStages(controls);
1833
- if (dataStages.length === 0 && defaultSort) dataStages.push(defaultSort);
1837
+ const preStages = vector ? vectorScoreStages(vector.threshold) : [];
1838
+ const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
1839
+ const dataStages = [...sorted];
1834
1840
  if (controls.$skip) dataStages.push({ $skip: controls.$skip });
1835
1841
  if (controls.$limit) dataStages.push({ $limit: controls.$limit });
1836
1842
  if (cleanup) dataStages.push(cleanup);
package/dist/index.mjs CHANGED
@@ -1275,13 +1275,6 @@ function filterOptionsOf(host) {
1275
1275
  * name of its own (a dynamic Atlas mapping) is listed as `DEFAULT`. The vector
1276
1276
  * default is the first vector index.
1277
1277
  */
1278
- /** Classic `$text` relevance order: `textScore` descending, `_id` breaking ties (since 0.1.165). */
1279
- const RELEVANCE_SORT = { $sort: {
1280
- _score: -1,
1281
- _id: 1
1282
- } };
1283
- /** The order of an unranked search (`$rank: false`, since 0.1.175): the primary key. */
1284
- const UNRANKED_SORT = { $sort: { _id: 1 } };
1285
1278
  function getSearchIndexesImpl(host) {
1286
1279
  const result = [];
1287
1280
  const indexes = host.getMongoSearchIndexes();
@@ -1338,7 +1331,7 @@ function requireSearchStage(host, text, indexName, controls) {
1338
1331
  * an unresolvable one throws exactly as the leaf path does.
1339
1332
  *
1340
1333
  * `classicText` is deliberately dropped: it only drives the leaf runner's
1341
- * `_score` projection and relevance `$sort`, and no per-document score
1334
+ * relevance order (the `textScore` it sorts by), and no per-document score
1342
1335
  * survives `$group`. See `resolveAggregateSearch` in `@atscript/db/agg` for
1343
1336
  * the normative cross-adapter contract.
1344
1337
  */
@@ -1363,17 +1356,29 @@ async function searchWithCountImpl(host, text, query, indexName) {
1363
1356
  * orders by `_id` instead (an Atlas `$search` included).
1364
1357
  */
1365
1358
  function textRunOptions(plan, query) {
1366
- if (query.controls?.$rank === false) return { defaultSort: UNRANKED_SORT };
1367
- return plan.classicText ? {
1368
- classicText: true,
1369
- defaultSort: RELEVANCE_SORT
1370
- } : {};
1359
+ if (query.controls?.$rank === false) return { defaultOrder: UNRANKED_ORDER };
1360
+ return plan.classicText ? { defaultOrder: RELEVANCE_ORDER } : {};
1371
1361
  }
1372
1362
  /**
1373
- * Field the `vectorSearchScore` rides in through the pipeline. Renamed to the
1374
- * public `$score` after fetch — a field name cannot start with `$`.
1363
+ * Field a search score rides in through the pipeline: the `vectorSearchScore`
1364
+ * (renamed to the public `$score` after fetch — a field name cannot start
1365
+ * with `$`) or the `textScore` of a relevance order (dropped from the rows).
1375
1366
  */
1376
1367
  const SCORE_FIELD = "__atscript_score";
1368
+ /**
1369
+ * Classic `$text` relevance order: `textScore` descending, `_id` breaking ties
1370
+ * (since 0.1.165). The score is dropped after `$skip` / `$limit`, so a row
1371
+ * carries no score field (since 0.1.185; before, rows carried `_score`).
1372
+ */
1373
+ const RELEVANCE_ORDER = {
1374
+ stages: [{ $addFields: { [SCORE_FIELD]: { $meta: "textScore" } } }, { $sort: {
1375
+ [SCORE_FIELD]: -1,
1376
+ _id: 1
1377
+ } }],
1378
+ cleanup: { $project: { [SCORE_FIELD]: 0 } }
1379
+ };
1380
+ /** The order of an unranked search (`$rank: false`, since 0.1.175): the primary key. */
1381
+ const UNRANKED_ORDER = { stages: [{ $sort: { _id: 1 } }] };
1377
1382
  /** Vector search via $vectorSearch aggregation stage. */
1378
1383
  async function vectorSearchImpl(host, vector, query, indexName) {
1379
1384
  const controls = query.controls || {};
@@ -1795,16 +1800,19 @@ function buildVectorSearchStage(host, vector, indexName, limit, skip) {
1795
1800
  index
1796
1801
  };
1797
1802
  }
1803
+ /** The order of a search: an explicit `$sort` (with its `$nulls` placement flags) replaces the default. */
1804
+ function searchOrder(controls, defaultOrder) {
1805
+ const explicit = sortStages(controls);
1806
+ return explicit.stages.length > 0 || !defaultOrder ? explicit : defaultOrder;
1807
+ }
1798
1808
  /** Runs a search/vector pipeline and returns results. Shared by search + vectorSearch. */
1799
- async function runSearchPipeline(host, stage, query, label, { vector, classicText = false, defaultSort, filterStages } = {}) {
1809
+ async function runSearchPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
1800
1810
  const controls = query.controls || {};
1801
1811
  const pipeline = [stage];
1802
1812
  if (vector) pipeline.push(...vectorScoreStages(vector.threshold));
1803
- else if (classicText) pipeline.push({ $addFields: { _score: { $meta: "textScore" } } });
1804
1813
  pipeline.push(...filterStages ?? mongoFilterStages(query.filter, filterOptionsOf(host)));
1805
- const { stages: sorted, cleanup } = sortStages(controls);
1806
- if (sorted.length > 0) pipeline.push(...sorted);
1807
- else if (defaultSort) pipeline.push(defaultSort);
1814
+ const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
1815
+ pipeline.push(...sorted);
1808
1816
  if (controls.$skip) pipeline.push({ $skip: controls.$skip });
1809
1817
  if (controls.$limit) pipeline.push({ $limit: controls.$limit });
1810
1818
  else pipeline.push({ $limit: 1e3 });
@@ -1823,13 +1831,11 @@ async function runSearchPipeline(host, stage, query, label, { vector, classicTex
1823
1831
  * `count: total` — the same counts `$$SEARCH_META` reports), concurrently
1824
1832
  * outside a transaction.
1825
1833
  */
1826
- async function runSearchWithCountPipeline(host, stage, query, label, { vector, classicText = false, defaultSort, filterStages } = {}) {
1834
+ async function runSearchWithCountPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
1827
1835
  const controls = query.controls || {};
1828
- const preStages = [];
1829
- if (vector) preStages.push(...vectorScoreStages(vector.threshold));
1830
- else if (classicText) preStages.push({ $addFields: { _score: { $meta: "textScore" } } });
1831
- const { stages: dataStages, cleanup } = sortStages(controls);
1832
- if (dataStages.length === 0 && defaultSort) dataStages.push(defaultSort);
1836
+ const preStages = vector ? vectorScoreStages(vector.threshold) : [];
1837
+ const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
1838
+ const dataStages = [...sorted];
1833
1839
  if (controls.$skip) dataStages.push({ $skip: controls.$skip });
1834
1840
  if (controls.$limit) dataStages.push({ $limit: controls.$limit });
1835
1841
  if (cleanup) dataStages.push(cleanup);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-mongo",
3
- "version": "0.1.184",
3
+ "version": "0.1.185",
4
4
  "description": "Mongodb plugin for atscript.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -46,19 +46,19 @@
46
46
  "access": "public"
47
47
  },
48
48
  "devDependencies": {
49
- "@atscript/core": "^0.2.13",
50
- "@atscript/typescript": "^0.2.13",
49
+ "@atscript/core": "^0.2.14",
50
+ "@atscript/typescript": "^0.2.14",
51
51
  "@uniqu/core": "^0.2.2",
52
52
  "mongodb": "^6.17.0",
53
53
  "mongodb-memory-server-core": "^10.0.0",
54
- "unplugin-atscript": "^0.2.13"
54
+ "unplugin-atscript": "^0.2.14"
55
55
  },
56
56
  "peerDependencies": {
57
- "@atscript/core": "^0.2.13",
58
- "@atscript/typescript": "^0.2.13",
57
+ "@atscript/core": "^0.2.14",
58
+ "@atscript/typescript": "^0.2.14",
59
59
  "@uniqu/core": "^0.2.2",
60
60
  "mongodb": "^6.17.0",
61
- "@atscript/db": "^0.1.184"
61
+ "@atscript/db": "^0.1.185"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "vp pack",