Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell-api): support type field for search index creation MONGOSH-1631 #1756

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion packages/service-provider-core/src/writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ export default interface Writable {
database: string,
collection: string,
// TODO(MONGOSH-1471): use SearchIndexDescription[] once available
specs: { name: string; definition: Document }[],
specs: {
name: string;
type?: 'search' | 'vectorSearch';
definition: Document;
}[],
dbOptions?: DbOptions
): Promise<string[]>;

Expand Down
6 changes: 5 additions & 1 deletion packages/service-provider-server/src/cli-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,11 @@ class CliServiceProvider
database: string,
collection: string,
// TODO(MONGOSH-1471): use SearchIndexDescription[] once available
specs: { name: string; definition: Document }[],
specs: {
name: string;
type?: 'search' | 'vectorSearch';
definition: Document;
}[],
dbOptions?: DbOptions
): Promise<string[]> {
return this.db(database, dbOptions)
Expand Down
59 changes: 59 additions & 0 deletions packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,61 @@ describe('Collection', function () {
);
});
});

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

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

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

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

context('with options and type but no name', function () {
it('calls serviceProvider.createSearchIndexes', async function () {
await collection.createSearchIndex(
{ mappings: { dynamic: true } },
'vectorSearch'
);

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

describe('createSearchIndexes', function () {
Expand All @@ -2595,6 +2650,8 @@ describe('Collection', function () {
await collection.createSearchIndexes([
{ name: 'foo', definition: { mappings: { dynamic: true } } },
{ name: 'bar', definition: {} },
{ name: 'sch', type: 'search', definition: {} },
{ name: 'vec', type: 'vectorSearch', definition: {} },
]);

expect(serviceProvider.createSearchIndexes).to.have.been.calledWith(
Expand All @@ -2603,6 +2660,8 @@ describe('Collection', function () {
[
{ name: 'foo', definition: { mappings: { dynamic: true } } },
{ name: 'bar', definition: {} },
{ name: 'sch', definition: {} },
{ name: 'vec', type: 'vectorSearch', definition: {} },
]
);
});
Expand Down
20 changes: 18 additions & 2 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2315,8 +2315,13 @@ export default class Collection extends ShellApiWithMongoClass {
// TODO(MONGOSH-1471): use SearchIndexDescription once available
async createSearchIndex(
indexName?: string | Document,
type?: 'search' | 'vectorSearch' | Document,
definition?: Document
): Promise<string> {
if (typeof type === 'object' && type !== null) {
definition = type;
type = undefined;
}
if (typeof indexName === 'object' && indexName !== null) {
definition = indexName;
indexName = undefined;
Expand All @@ -2329,6 +2334,9 @@ export default class Collection extends ShellApiWithMongoClass {
[
{
name: (indexName as string | undefined) ?? 'default',
// Omitting type when it is 'search' for compat with older servers
...(type &&
type !== 'search' && { type: type as 'search' | 'vectorSearch' }),
definition: { ...definition },
},
]
Expand All @@ -2341,13 +2349,21 @@ export default class Collection extends ShellApiWithMongoClass {
@apiVersions([])
// TODO(MONGOSH-1471): use SearchIndexDescription once available
async createSearchIndexes(
specs: { name: string; definition: Document }[]
specs: {
name: string;
type?: 'search' | 'vectorSearch';
definition: Document;
}[]
): Promise<string[]> {
this._emitCollectionApiCall('createSearchIndexes', { specs });
return await this._mongo._serviceProvider.createSearchIndexes(
this._database._name,
this._name,
specs
// Omitting type when it is 'search' for compat with older servers
specs.map(({ type, ...spec }) => ({
...spec,
...(type && type !== 'search' && { type }),
}))
);
}

Expand Down