Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type {
ReplaceOptions,
ResumeToken,
RunCommandOptions,
SearchIndexDescription,
ServerSessionId,
TagSet,
TransactionOptions,
Expand Down
34 changes: 29 additions & 5 deletions packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2657,7 +2657,7 @@ describe('Collection', function () {
});
});

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

Expand All @@ -2669,7 +2669,7 @@ describe('Collection', function () {
});
});

context('with name, options', function () {
context('with name, definition options', function () {
it('calls serviceProvider.createIndexes', async function () {
await collection.createSearchIndex('my-index', {
mappings: { dynamic: true },
Expand All @@ -2683,7 +2683,7 @@ describe('Collection', function () {
});
});

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

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

context('with options and type but no name', function () {
context('with definition options and type but no name', function () {
it('calls serviceProvider.createSearchIndexes', async function () {
await collection.createSearchIndex(
{ mappings: { dynamic: true } },
Expand All @@ -2737,6 +2737,30 @@ describe('Collection', function () {
);
});
});

context('with description options', function () {
it('calls serviceProvider.createSearchIndexes', async function () {
await collection.createSearchIndex({
name: 'my-index',
type: 'vectorSearch',
definition: {
mappings: { dynamic: true },
},
});

expect(serviceProvider.createSearchIndexes).to.have.been.calledWith(
'db1',
'coll1',
[
{
name: 'my-index',
type: 'vectorSearch',
definition: { mappings: { dynamic: true } },
},
]
);
});
});
});

describe('createSearchIndexes', function () {
Expand Down
63 changes: 50 additions & 13 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
FindAndModifyMethodShellOptions,
RemoveShellOptions,
MapReduceShellOptions,
SearchIndexDefinition,
} from './helpers';
import {
adaptAggregateOptions,
Expand Down Expand Up @@ -67,6 +68,7 @@ import type {
DropCollectionOptions,
CheckMetadataConsistencyOptions,
AggregateOptions,
SearchIndexDescription,
} from '@mongosh/service-provider-core';
import type { RunCommandCursor, Database } from './index';
import {
Expand Down Expand Up @@ -2383,34 +2385,69 @@ export default class Collection extends ShellApiWithMongoClass {
);
}

async createSearchIndex(
name?: string,
definition?: SearchIndexDefinition
): Promise<string>;
async createSearchIndex(
name?: string,
type?: 'search' | 'vectorSearch',
definition?: SearchIndexDefinition
): Promise<string>;
async createSearchIndex(
definition?: SearchIndexDefinition,
type?: 'search' | 'vectorSearch'
): Promise<string>;
async createSearchIndex(
description?: SearchIndexDescription
): Promise<string>;
@serverVersions(['6.0.0', ServerVersions.latest])
@returnsPromise
@apiVersions([])
// TODO(MONGOSH-1471): use SearchIndexDescription once available
async createSearchIndex(
indexName?: string | Document,
type?: 'search' | 'vectorSearch' | Document,
definition?: Document
nameOrOptions?: string | SearchIndexDescription | SearchIndexDefinition,
typeOrOptions?: 'search' | 'vectorSearch' | SearchIndexDefinition,
definition?: SearchIndexDefinition
): Promise<string> {
if (typeof type === 'object' && type !== null) {
definition = type;
type = undefined;
let indexName: string | undefined;
let indexType: string | undefined;

if (typeof typeOrOptions === 'object' && typeOrOptions !== null) {
definition = typeOrOptions;
} else {
indexType = typeOrOptions;
}
if (typeof indexName === 'object' && indexName !== null) {
definition = indexName;
indexName = undefined;

if (
typeof nameOrOptions === 'object' &&
nameOrOptions !== null &&
nameOrOptions.definition
) {
indexName = nameOrOptions.name;
indexType = nameOrOptions.type;
definition = nameOrOptions.definition;
} else if (typeof nameOrOptions === 'object' && nameOrOptions !== null) {
definition = nameOrOptions;
} else {
indexName = nameOrOptions;
}

this._emitCollectionApiCall('createSearchIndex', { indexName, definition });
this._emitCollectionApiCall('createSearchIndex', {
indexName,
indexType,
definition,
});
const results = await this._mongo._serviceProvider.createSearchIndexes(
this._database._name,
this._name,
[
{
name: indexName ?? 'default',
// Omitting type when it is 'search' for compat with older servers
...(type &&
type !== 'search' && { type: type as 'search' | 'vectorSearch' }),
...(indexType &&
indexType !== 'search' && {
type: indexType as 'search' | 'vectorSearch',
}),
definition: { ...definition },
},
]
Expand Down
2 changes: 2 additions & 0 deletions packages/shell-api/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,3 +1295,5 @@ export function buildConfigChunksCollectionMatch(
export const aggregateBackgroundOptionNotSupportedHelp =
'the background option is not supported by the aggregate method and will be ignored, ' +
'use runCommand to use { background: true } with Atlas Data Federation';

export type SearchIndexDefinition = Document;
Loading