Skip to content

Commit

Permalink
feat(shell-api): support type field for search index creation MONGO…
Browse files Browse the repository at this point in the history
…SH-1631

Now that SERVER-83107 is complete, we can add this to mongosh.
  • Loading branch information
addaleax committed Nov 27, 2023
1 parent cf5c0f2 commit ef0391d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
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.createIndexes', 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.createIndexes', 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.createIndexes', 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

0 comments on commit ef0391d

Please sign in to comment.