Skip to content

Commit 86c4d1f

Browse files
committed
fix(shell-api): create vector search index with description options MONGOSH-1851
1 parent 7c1ad8d commit 86c4d1f

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

packages/shell-api/src/collection.spec.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,7 +2657,7 @@ describe('Collection', function () {
26572657
});
26582658
});
26592659

2660-
context('with options', function () {
2660+
context('with definition options', function () {
26612661
it('calls serviceProvider.createIndexes', async function () {
26622662
await collection.createSearchIndex({ mappings: { dynamic: true } });
26632663

@@ -2669,7 +2669,7 @@ describe('Collection', function () {
26692669
});
26702670
});
26712671

2672-
context('with name, options', function () {
2672+
context('with name, definition options', function () {
26732673
it('calls serviceProvider.createIndexes', async function () {
26742674
await collection.createSearchIndex('my-index', {
26752675
mappings: { dynamic: true },
@@ -2683,7 +2683,7 @@ describe('Collection', function () {
26832683
});
26842684
});
26852685

2686-
context('with name, options and type !== search', function () {
2686+
context('with name, definition options and type !== search', function () {
26872687
it('calls serviceProvider.createSearchIndexes', async function () {
26882688
await collection.createSearchIndex('my-index', 'vectorSearch', {
26892689
mappings: { dynamic: true },
@@ -2703,7 +2703,7 @@ describe('Collection', function () {
27032703
});
27042704
});
27052705

2706-
context('with name, options and type === search', function () {
2706+
context('with name, definition options and type === search', function () {
27072707
it('calls serviceProvider.createSearchIndexes', async function () {
27082708
await collection.createSearchIndex('my-index', 'search', {
27092709
mappings: { dynamic: true },
@@ -2717,7 +2717,7 @@ describe('Collection', function () {
27172717
});
27182718
});
27192719

2720-
context('with options and type but no name', function () {
2720+
context('with definition options and type but no name', function () {
27212721
it('calls serviceProvider.createSearchIndexes', async function () {
27222722
await collection.createSearchIndex(
27232723
{ mappings: { dynamic: true } },
@@ -2737,6 +2737,30 @@ describe('Collection', function () {
27372737
);
27382738
});
27392739
});
2740+
2741+
context('with description options', function () {
2742+
it('calls serviceProvider.createSearchIndexes', async function () {
2743+
await collection.createSearchIndex({
2744+
name: 'my-index',
2745+
type: 'vectorSearch',
2746+
definition: {
2747+
mappings: { dynamic: true },
2748+
},
2749+
});
2750+
2751+
expect(serviceProvider.createSearchIndexes).to.have.been.calledWith(
2752+
'db1',
2753+
'coll1',
2754+
[
2755+
{
2756+
name: 'my-index',
2757+
type: 'vectorSearch',
2758+
definition: { mappings: { dynamic: true } },
2759+
},
2760+
]
2761+
);
2762+
});
2763+
});
27402764
});
27412765

27422766
describe('createSearchIndexes', function () {

packages/shell-api/src/collection.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,17 +2388,30 @@ export default class Collection extends ShellApiWithMongoClass {
23882388
@apiVersions([])
23892389
// TODO(MONGOSH-1471): use SearchIndexDescription once available
23902390
async createSearchIndex(
2391-
indexName?: string | Document,
2391+
indexNameOrOptions?: string | Document,
23922392
type?: 'search' | 'vectorSearch' | Document,
23932393
definition?: Document
23942394
): Promise<string> {
2395+
let indexName: string | undefined;
23952396
if (typeof type === 'object' && type !== null) {
23962397
definition = type;
23972398
type = undefined;
23982399
}
2399-
if (typeof indexName === 'object' && indexName !== null) {
2400-
definition = indexName;
2401-
indexName = undefined;
2400+
if (
2401+
typeof indexNameOrOptions === 'object' &&
2402+
indexNameOrOptions !== null &&
2403+
indexNameOrOptions.definition
2404+
) {
2405+
indexName = indexNameOrOptions.name;
2406+
type = indexNameOrOptions.type;
2407+
definition = indexNameOrOptions.definition;
2408+
} else if (
2409+
typeof indexNameOrOptions === 'object' &&
2410+
indexNameOrOptions !== null
2411+
) {
2412+
definition = indexNameOrOptions;
2413+
} else {
2414+
indexName = indexNameOrOptions;
24022415
}
24032416

24042417
this._emitCollectionApiCall('createSearchIndex', { indexName, definition });

0 commit comments

Comments
 (0)