@atscript/db-mongo 0.1.184 → 0.1.186
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/agg.cjs +1 -1
- package/dist/agg.mjs +1 -1
- package/dist/index.cjs +59 -43
- package/dist/index.mjs +59 -43
- package/dist/{mongo-accumulator-CFVRBDiw.mjs → mongo-accumulator-BDgJ8wpS.mjs} +1 -1
- package/dist/{mongo-accumulator-BBRS2_8v.cjs → mongo-accumulator-CAkmw604.cjs} +1 -1
- package/package.json +7 -7
package/dist/agg.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_mongo_accumulator = require("./mongo-accumulator-
|
|
2
|
+
const require_mongo_accumulator = require("./mongo-accumulator-CAkmw604.cjs");
|
|
3
3
|
let _atscript_db = require("@atscript/db");
|
|
4
4
|
let _uniqu_core = require("@uniqu/core");
|
|
5
5
|
let _atscript_db_agg = require("@atscript/db/agg");
|
package/dist/agg.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as rowOrderStages, f as sortStages, g as whereToExpr, i as buildMongoFilter, m as orNull, n as distinctCountExpr, p as exprToMongo, s as mongoFilterStages, t as buildAccumulator } from "./mongo-accumulator-
|
|
1
|
+
import { d as rowOrderStages, f as sortStages, g as whereToExpr, i as buildMongoFilter, m as orNull, n as distinctCountExpr, p as exprToMongo, s as mongoFilterStages, t as buildAccumulator } from "./mongo-accumulator-BDgJ8wpS.mjs";
|
|
2
2
|
import { DbError, aggregateColumns, evaluateExpr } from "@atscript/db";
|
|
3
3
|
import { BUCKET_MAX_INSTANT, BUCKET_MIN_INSTANT } from "@uniqu/core";
|
|
4
4
|
import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_mongo_accumulator = require("./mongo-accumulator-
|
|
2
|
+
const require_mongo_accumulator = require("./mongo-accumulator-CAkmw604.cjs");
|
|
3
3
|
const require_search_messages = require("./search-messages-BqLxxrb8.cjs");
|
|
4
4
|
let _atscript_db = require("@atscript/db");
|
|
5
5
|
let mongodb = require("mongodb");
|
|
@@ -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();
|
|
@@ -1338,9 +1331,9 @@ function requireSearchStage(host, text, indexName, controls) {
|
|
|
1338
1331
|
* path). `$index` picks the search index by the same rules as `search()`, and
|
|
1339
1332
|
* an unresolvable one throws exactly as the leaf path does.
|
|
1340
1333
|
*
|
|
1341
|
-
* `
|
|
1342
|
-
*
|
|
1343
|
-
*
|
|
1334
|
+
* The plan's `relevance` order is deliberately dropped: it only orders the
|
|
1335
|
+
* leaf runner's rows, and no per-document score survives `$group`. See
|
|
1336
|
+
* `resolveAggregateSearch` in `@atscript/db/agg` for
|
|
1344
1337
|
* the normative cross-adapter contract.
|
|
1345
1338
|
*/
|
|
1346
1339
|
function buildAggregateSearchStage(host, controls) {
|
|
@@ -1359,22 +1352,47 @@ async function searchWithCountImpl(host, text, query, indexName) {
|
|
|
1359
1352
|
return runSearchWithCountPipeline(host, plan.stage, query, "searchWithCount", textRunOptions(plan, query));
|
|
1360
1353
|
}
|
|
1361
1354
|
/**
|
|
1362
|
-
* The runner options of a text search: relevance
|
|
1363
|
-
*
|
|
1364
|
-
*
|
|
1355
|
+
* The runner options of a text search: the plan's relevance order — none
|
|
1356
|
+
* under `$rank: false` (since 0.1.175), which orders by `_id` instead (an
|
|
1357
|
+
* Atlas `$search` included).
|
|
1365
1358
|
*/
|
|
1366
1359
|
function textRunOptions(plan, query) {
|
|
1367
|
-
if (query.controls?.$rank === false) return {
|
|
1368
|
-
return plan.
|
|
1369
|
-
classicText: true,
|
|
1370
|
-
defaultSort: RELEVANCE_SORT
|
|
1371
|
-
} : {};
|
|
1360
|
+
if (query.controls?.$rank === false) return { defaultOrder: UNRANKED_ORDER };
|
|
1361
|
+
return { defaultOrder: plan.relevance };
|
|
1372
1362
|
}
|
|
1373
1363
|
/**
|
|
1374
|
-
* Field
|
|
1375
|
-
* public `$score` after fetch — a field name cannot start
|
|
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 score of a relevance order (dropped from the rows).
|
|
1376
1367
|
*/
|
|
1377
1368
|
const SCORE_FIELD = "__atscript_score";
|
|
1369
|
+
/**
|
|
1370
|
+
* Classic `$text` relevance order: the rows an integer member matched by
|
|
1371
|
+
* exact number first (`exact`: one `$eq` expression per member of a
|
|
1372
|
+
* whole-number search, since 0.1.186 — as the SQL adapters order them), then
|
|
1373
|
+
* `textScore` descending, `_id` breaking ties (since 0.1.165). The score is
|
|
1374
|
+
* dropped after `$skip` / `$limit`, so a row carries no score field (since
|
|
1375
|
+
* 0.1.185; before, rows carried `_score`).
|
|
1376
|
+
*/
|
|
1377
|
+
function relevanceOrder(exact) {
|
|
1378
|
+
const text = { $meta: "textScore" };
|
|
1379
|
+
return {
|
|
1380
|
+
stages: exact.length === 0 ? [{ $addFields: { [SCORE_FIELD]: text } }, { $sort: {
|
|
1381
|
+
[SCORE_FIELD]: -1,
|
|
1382
|
+
_id: 1
|
|
1383
|
+
} }] : [{ $addFields: { [SCORE_FIELD]: {
|
|
1384
|
+
exact: { $or: exact },
|
|
1385
|
+
text
|
|
1386
|
+
} } }, { $sort: {
|
|
1387
|
+
[`${SCORE_FIELD}.exact`]: -1,
|
|
1388
|
+
[`${SCORE_FIELD}.text`]: -1,
|
|
1389
|
+
_id: 1
|
|
1390
|
+
} }],
|
|
1391
|
+
cleanup: { $project: { [SCORE_FIELD]: 0 } }
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
/** The order of a search without a text rank (`$rank: false`, since 0.1.175; an integer-only index): the primary key. */
|
|
1395
|
+
const UNRANKED_ORDER = { stages: [{ $sort: { _id: 1 } }] };
|
|
1378
1396
|
/** Vector search via $vectorSearch aggregation stage. */
|
|
1379
1397
|
async function vectorSearchImpl(host, vector, query, indexName) {
|
|
1380
1398
|
const controls = query.controls || {};
|
|
@@ -1512,8 +1530,8 @@ function resolveThreshold(host, controls, indexName) {
|
|
|
1512
1530
|
return host.getVectorThreshold(indexName);
|
|
1513
1531
|
}
|
|
1514
1532
|
/**
|
|
1515
|
-
* Builds the first pipeline stage for a text search and
|
|
1516
|
-
*
|
|
1533
|
+
* Builds the first pipeline stage for a text search and the order of its rows
|
|
1534
|
+
* without a `$sort` (a classic index; an Atlas `$search` keeps its own).
|
|
1517
1535
|
*
|
|
1518
1536
|
* Two distinct execution paths share the `search()` API:
|
|
1519
1537
|
* - **Classic text** (`@db.index.fulltext`, or an adapter-scanned `text`
|
|
@@ -1541,12 +1559,12 @@ function buildSearchStage(host, text, indexName, controls) {
|
|
|
1541
1559
|
} }));
|
|
1542
1560
|
if (!hasText) return {
|
|
1543
1561
|
stage: { $match: equalities.length === 0 ? { _id: { $in: [] } } : equalities.length === 1 ? equalities[0] : { $or: equalities } },
|
|
1544
|
-
|
|
1562
|
+
relevance: UNRANKED_ORDER
|
|
1545
1563
|
};
|
|
1546
1564
|
const textMatch = { $text: { $search: index.match === "all" ? mongoRequireEveryTerm(text) : text } };
|
|
1547
1565
|
return {
|
|
1548
1566
|
stage: { $match: equalities.length > 0 ? { $or: [textMatch, ...equalities] } : textMatch },
|
|
1549
|
-
|
|
1567
|
+
relevance: relevanceOrder(keys.map((key) => ({ $eq: [`$${key}`, n] })))
|
|
1550
1568
|
};
|
|
1551
1569
|
}
|
|
1552
1570
|
const searchIndex = index;
|
|
@@ -1597,13 +1615,10 @@ function buildSearchStage(host, text, indexName, controls) {
|
|
|
1597
1615
|
should: clauses,
|
|
1598
1616
|
minimumShouldMatch: 1
|
|
1599
1617
|
} };
|
|
1600
|
-
return {
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
} },
|
|
1605
|
-
classicText: false
|
|
1606
|
-
};
|
|
1618
|
+
return { stage: { $search: {
|
|
1619
|
+
index: index.key,
|
|
1620
|
+
...body
|
|
1621
|
+
} } };
|
|
1607
1622
|
}
|
|
1608
1623
|
/**
|
|
1609
1624
|
* Resolves query-time fuzzy (typo tolerance): the `$fuzzy` request control
|
|
@@ -1796,16 +1811,19 @@ function buildVectorSearchStage(host, vector, indexName, limit, skip) {
|
|
|
1796
1811
|
index
|
|
1797
1812
|
};
|
|
1798
1813
|
}
|
|
1814
|
+
/** The order of a search: an explicit `$sort` (with its `$nulls` placement flags) replaces the default. */
|
|
1815
|
+
function searchOrder(controls, defaultOrder) {
|
|
1816
|
+
const explicit = require_mongo_accumulator.sortStages(controls);
|
|
1817
|
+
return explicit.stages.length > 0 || !defaultOrder ? explicit : defaultOrder;
|
|
1818
|
+
}
|
|
1799
1819
|
/** Runs a search/vector pipeline and returns results. Shared by search + vectorSearch. */
|
|
1800
|
-
async function runSearchPipeline(host, stage, query, label, { vector,
|
|
1820
|
+
async function runSearchPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
|
|
1801
1821
|
const controls = query.controls || {};
|
|
1802
1822
|
const pipeline = [stage];
|
|
1803
1823
|
if (vector) pipeline.push(...vectorScoreStages(vector.threshold));
|
|
1804
|
-
else if (classicText) pipeline.push({ $addFields: { _score: { $meta: "textScore" } } });
|
|
1805
1824
|
pipeline.push(...filterStages ?? require_mongo_accumulator.mongoFilterStages(query.filter, filterOptionsOf(host)));
|
|
1806
|
-
const { stages: sorted, cleanup } =
|
|
1807
|
-
|
|
1808
|
-
else if (defaultSort) pipeline.push(defaultSort);
|
|
1825
|
+
const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
|
|
1826
|
+
pipeline.push(...sorted);
|
|
1809
1827
|
if (controls.$skip) pipeline.push({ $skip: controls.$skip });
|
|
1810
1828
|
if (controls.$limit) pipeline.push({ $limit: controls.$limit });
|
|
1811
1829
|
else pipeline.push({ $limit: 1e3 });
|
|
@@ -1824,13 +1842,11 @@ async function runSearchPipeline(host, stage, query, label, { vector, classicTex
|
|
|
1824
1842
|
* `count: total` — the same counts `$$SEARCH_META` reports), concurrently
|
|
1825
1843
|
* outside a transaction.
|
|
1826
1844
|
*/
|
|
1827
|
-
async function runSearchWithCountPipeline(host, stage, query, label, { vector,
|
|
1845
|
+
async function runSearchWithCountPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
|
|
1828
1846
|
const controls = query.controls || {};
|
|
1829
|
-
const preStages = [];
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
const { stages: dataStages, cleanup } = require_mongo_accumulator.sortStages(controls);
|
|
1833
|
-
if (dataStages.length === 0 && defaultSort) dataStages.push(defaultSort);
|
|
1847
|
+
const preStages = vector ? vectorScoreStages(vector.threshold) : [];
|
|
1848
|
+
const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
|
|
1849
|
+
const dataStages = [...sorted];
|
|
1834
1850
|
if (controls.$skip) dataStages.push({ $skip: controls.$skip });
|
|
1835
1851
|
if (controls.$limit) dataStages.push({ $limit: controls.$limit });
|
|
1836
1852
|
if (cleanup) dataStages.push(cleanup);
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as buildMongoQuery, c as planStages, f as sortStages, h as queryNodeToExpr, i as buildMongoFilter, l as correlate, m as orNull, n as distinctCountExpr, o as collationOfAdapter, p as exprToMongo, r as REL_FILTER_TEMP_PREFIX, s as mongoFilterStages, t as buildAccumulator, u as hasNullsPlacement } from "./mongo-accumulator-
|
|
1
|
+
import { a as buildMongoQuery, c as planStages, f as sortStages, h as queryNodeToExpr, i as buildMongoFilter, l as correlate, m as orNull, n as distinctCountExpr, o as collationOfAdapter, p as exprToMongo, r as REL_FILTER_TEMP_PREFIX, s as mongoFilterStages, t as buildAccumulator, u as hasNullsPlacement } from "./mongo-accumulator-BDgJ8wpS.mjs";
|
|
2
2
|
import { t as dynamicSearchEncryptedMessage } from "./search-messages-mQSpJ4GD.mjs";
|
|
3
3
|
import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, andFilters, bucketTimeZoneUnavailable, computeInsights, constraintViolation, containsRelationFilter, createChangeRecorder, createFailureCollector, defaultFulltextIndex, geoIndexNotFoundMessage, getDbFieldOp, getKeyProps, getPath, isAtscriptDbView, isFieldRef, isPlainObject, isViewType, parseDefaultLiteral, queryReadColumns, relationStaticFilter, resolveDesignType, searchIndexNotFoundMessage, searchTermInteger, storedObjectType, tableNameOf, translateQueryTree, uniqueKeyTuple, unknownWithRelation, vectorIndexNotFoundMessage } from "@atscript/db";
|
|
4
4
|
import { MongoClient, ObjectId } from "mongodb";
|
|
@@ -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();
|
|
@@ -1337,9 +1330,9 @@ function requireSearchStage(host, text, indexName, controls) {
|
|
|
1337
1330
|
* path). `$index` picks the search index by the same rules as `search()`, and
|
|
1338
1331
|
* an unresolvable one throws exactly as the leaf path does.
|
|
1339
1332
|
*
|
|
1340
|
-
* `
|
|
1341
|
-
*
|
|
1342
|
-
*
|
|
1333
|
+
* The plan's `relevance` order is deliberately dropped: it only orders the
|
|
1334
|
+
* leaf runner's rows, and no per-document score survives `$group`. See
|
|
1335
|
+
* `resolveAggregateSearch` in `@atscript/db/agg` for
|
|
1343
1336
|
* the normative cross-adapter contract.
|
|
1344
1337
|
*/
|
|
1345
1338
|
function buildAggregateSearchStage(host, controls) {
|
|
@@ -1358,22 +1351,47 @@ async function searchWithCountImpl(host, text, query, indexName) {
|
|
|
1358
1351
|
return runSearchWithCountPipeline(host, plan.stage, query, "searchWithCount", textRunOptions(plan, query));
|
|
1359
1352
|
}
|
|
1360
1353
|
/**
|
|
1361
|
-
* The runner options of a text search: relevance
|
|
1362
|
-
*
|
|
1363
|
-
*
|
|
1354
|
+
* The runner options of a text search: the plan's relevance order — none
|
|
1355
|
+
* under `$rank: false` (since 0.1.175), which orders by `_id` instead (an
|
|
1356
|
+
* Atlas `$search` included).
|
|
1364
1357
|
*/
|
|
1365
1358
|
function textRunOptions(plan, query) {
|
|
1366
|
-
if (query.controls?.$rank === false) return {
|
|
1367
|
-
return plan.
|
|
1368
|
-
classicText: true,
|
|
1369
|
-
defaultSort: RELEVANCE_SORT
|
|
1370
|
-
} : {};
|
|
1359
|
+
if (query.controls?.$rank === false) return { defaultOrder: UNRANKED_ORDER };
|
|
1360
|
+
return { defaultOrder: plan.relevance };
|
|
1371
1361
|
}
|
|
1372
1362
|
/**
|
|
1373
|
-
* Field
|
|
1374
|
-
* public `$score` after fetch — a field name cannot start
|
|
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 score of a relevance order (dropped from the rows).
|
|
1375
1366
|
*/
|
|
1376
1367
|
const SCORE_FIELD = "__atscript_score";
|
|
1368
|
+
/**
|
|
1369
|
+
* Classic `$text` relevance order: the rows an integer member matched by
|
|
1370
|
+
* exact number first (`exact`: one `$eq` expression per member of a
|
|
1371
|
+
* whole-number search, since 0.1.186 — as the SQL adapters order them), then
|
|
1372
|
+
* `textScore` descending, `_id` breaking ties (since 0.1.165). The score is
|
|
1373
|
+
* dropped after `$skip` / `$limit`, so a row carries no score field (since
|
|
1374
|
+
* 0.1.185; before, rows carried `_score`).
|
|
1375
|
+
*/
|
|
1376
|
+
function relevanceOrder(exact) {
|
|
1377
|
+
const text = { $meta: "textScore" };
|
|
1378
|
+
return {
|
|
1379
|
+
stages: exact.length === 0 ? [{ $addFields: { [SCORE_FIELD]: text } }, { $sort: {
|
|
1380
|
+
[SCORE_FIELD]: -1,
|
|
1381
|
+
_id: 1
|
|
1382
|
+
} }] : [{ $addFields: { [SCORE_FIELD]: {
|
|
1383
|
+
exact: { $or: exact },
|
|
1384
|
+
text
|
|
1385
|
+
} } }, { $sort: {
|
|
1386
|
+
[`${SCORE_FIELD}.exact`]: -1,
|
|
1387
|
+
[`${SCORE_FIELD}.text`]: -1,
|
|
1388
|
+
_id: 1
|
|
1389
|
+
} }],
|
|
1390
|
+
cleanup: { $project: { [SCORE_FIELD]: 0 } }
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
/** The order of a search without a text rank (`$rank: false`, since 0.1.175; an integer-only index): the primary key. */
|
|
1394
|
+
const UNRANKED_ORDER = { stages: [{ $sort: { _id: 1 } }] };
|
|
1377
1395
|
/** Vector search via $vectorSearch aggregation stage. */
|
|
1378
1396
|
async function vectorSearchImpl(host, vector, query, indexName) {
|
|
1379
1397
|
const controls = query.controls || {};
|
|
@@ -1511,8 +1529,8 @@ function resolveThreshold(host, controls, indexName) {
|
|
|
1511
1529
|
return host.getVectorThreshold(indexName);
|
|
1512
1530
|
}
|
|
1513
1531
|
/**
|
|
1514
|
-
* Builds the first pipeline stage for a text search and
|
|
1515
|
-
*
|
|
1532
|
+
* Builds the first pipeline stage for a text search and the order of its rows
|
|
1533
|
+
* without a `$sort` (a classic index; an Atlas `$search` keeps its own).
|
|
1516
1534
|
*
|
|
1517
1535
|
* Two distinct execution paths share the `search()` API:
|
|
1518
1536
|
* - **Classic text** (`@db.index.fulltext`, or an adapter-scanned `text`
|
|
@@ -1540,12 +1558,12 @@ function buildSearchStage(host, text, indexName, controls) {
|
|
|
1540
1558
|
} }));
|
|
1541
1559
|
if (!hasText) return {
|
|
1542
1560
|
stage: { $match: equalities.length === 0 ? { _id: { $in: [] } } : equalities.length === 1 ? equalities[0] : { $or: equalities } },
|
|
1543
|
-
|
|
1561
|
+
relevance: UNRANKED_ORDER
|
|
1544
1562
|
};
|
|
1545
1563
|
const textMatch = { $text: { $search: index.match === "all" ? mongoRequireEveryTerm(text) : text } };
|
|
1546
1564
|
return {
|
|
1547
1565
|
stage: { $match: equalities.length > 0 ? { $or: [textMatch, ...equalities] } : textMatch },
|
|
1548
|
-
|
|
1566
|
+
relevance: relevanceOrder(keys.map((key) => ({ $eq: [`$${key}`, n] })))
|
|
1549
1567
|
};
|
|
1550
1568
|
}
|
|
1551
1569
|
const searchIndex = index;
|
|
@@ -1596,13 +1614,10 @@ function buildSearchStage(host, text, indexName, controls) {
|
|
|
1596
1614
|
should: clauses,
|
|
1597
1615
|
minimumShouldMatch: 1
|
|
1598
1616
|
} };
|
|
1599
|
-
return {
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
} },
|
|
1604
|
-
classicText: false
|
|
1605
|
-
};
|
|
1617
|
+
return { stage: { $search: {
|
|
1618
|
+
index: index.key,
|
|
1619
|
+
...body
|
|
1620
|
+
} } };
|
|
1606
1621
|
}
|
|
1607
1622
|
/**
|
|
1608
1623
|
* Resolves query-time fuzzy (typo tolerance): the `$fuzzy` request control
|
|
@@ -1795,16 +1810,19 @@ function buildVectorSearchStage(host, vector, indexName, limit, skip) {
|
|
|
1795
1810
|
index
|
|
1796
1811
|
};
|
|
1797
1812
|
}
|
|
1813
|
+
/** The order of a search: an explicit `$sort` (with its `$nulls` placement flags) replaces the default. */
|
|
1814
|
+
function searchOrder(controls, defaultOrder) {
|
|
1815
|
+
const explicit = sortStages(controls);
|
|
1816
|
+
return explicit.stages.length > 0 || !defaultOrder ? explicit : defaultOrder;
|
|
1817
|
+
}
|
|
1798
1818
|
/** Runs a search/vector pipeline and returns results. Shared by search + vectorSearch. */
|
|
1799
|
-
async function runSearchPipeline(host, stage, query, label, { vector,
|
|
1819
|
+
async function runSearchPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
|
|
1800
1820
|
const controls = query.controls || {};
|
|
1801
1821
|
const pipeline = [stage];
|
|
1802
1822
|
if (vector) pipeline.push(...vectorScoreStages(vector.threshold));
|
|
1803
|
-
else if (classicText) pipeline.push({ $addFields: { _score: { $meta: "textScore" } } });
|
|
1804
1823
|
pipeline.push(...filterStages ?? mongoFilterStages(query.filter, filterOptionsOf(host)));
|
|
1805
|
-
const { stages: sorted, cleanup } =
|
|
1806
|
-
|
|
1807
|
-
else if (defaultSort) pipeline.push(defaultSort);
|
|
1824
|
+
const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
|
|
1825
|
+
pipeline.push(...sorted);
|
|
1808
1826
|
if (controls.$skip) pipeline.push({ $skip: controls.$skip });
|
|
1809
1827
|
if (controls.$limit) pipeline.push({ $limit: controls.$limit });
|
|
1810
1828
|
else pipeline.push({ $limit: 1e3 });
|
|
@@ -1823,13 +1841,11 @@ async function runSearchPipeline(host, stage, query, label, { vector, classicTex
|
|
|
1823
1841
|
* `count: total` — the same counts `$$SEARCH_META` reports), concurrently
|
|
1824
1842
|
* outside a transaction.
|
|
1825
1843
|
*/
|
|
1826
|
-
async function runSearchWithCountPipeline(host, stage, query, label, { vector,
|
|
1844
|
+
async function runSearchWithCountPipeline(host, stage, query, label, { vector, defaultOrder, filterStages } = {}) {
|
|
1827
1845
|
const controls = query.controls || {};
|
|
1828
|
-
const preStages = [];
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
const { stages: dataStages, cleanup } = sortStages(controls);
|
|
1832
|
-
if (dataStages.length === 0 && defaultSort) dataStages.push(defaultSort);
|
|
1846
|
+
const preStages = vector ? vectorScoreStages(vector.threshold) : [];
|
|
1847
|
+
const { stages: sorted, cleanup } = searchOrder(controls, defaultOrder);
|
|
1848
|
+
const dataStages = [...sorted];
|
|
1833
1849
|
if (controls.$skip) dataStages.push({ $skip: controls.$skip });
|
|
1834
1850
|
if (controls.$limit) dataStages.push({ $limit: controls.$limit });
|
|
1835
1851
|
if (cleanup) dataStages.push(cleanup);
|
|
@@ -347,7 +347,7 @@ function placedKey(placement, dir) {
|
|
|
347
347
|
*/
|
|
348
348
|
function sortStages(controls) {
|
|
349
349
|
const { $sort: sort, $nulls: nulls } = controls ?? {};
|
|
350
|
-
if (!sort) return { stages: [] };
|
|
350
|
+
if (!sort || Object.keys(sort).length === 0) return { stages: [] };
|
|
351
351
|
if (!hasNullsPlacement(controls)) return { stages: [{ $sort: sort }] };
|
|
352
352
|
const flags = {};
|
|
353
353
|
const drop = {};
|
|
@@ -347,7 +347,7 @@ function placedKey(placement, dir) {
|
|
|
347
347
|
*/
|
|
348
348
|
function sortStages(controls) {
|
|
349
349
|
const { $sort: sort, $nulls: nulls } = controls ?? {};
|
|
350
|
-
if (!sort) return { stages: [] };
|
|
350
|
+
if (!sort || Object.keys(sort).length === 0) return { stages: [] };
|
|
351
351
|
if (!hasNullsPlacement(controls)) return { stages: [{ $sort: sort }] };
|
|
352
352
|
const flags = {};
|
|
353
353
|
const drop = {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-mongo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.186",
|
|
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.
|
|
50
|
-
"@atscript/typescript": "^0.2.
|
|
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.
|
|
54
|
+
"unplugin-atscript": "^0.2.14"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@atscript/core": "^0.2.
|
|
58
|
-
"@atscript/typescript": "^0.2.
|
|
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.
|
|
61
|
+
"@atscript/db": "^0.1.186"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "vp pack",
|