Skip to content

Commit 2acbee0

Browse files
authored
feat(shell-api): search index management helpers MONGOSH-1455 MONGOSH-1456 (#1477)
* search index management helpers * getSearchIndexes() * no listSearchIndexes * the types aren't exported yet * TODOs * exlude search indexes from expected results
1 parent c2324ef commit 2acbee0

File tree

10 files changed

+450
-4
lines changed

10 files changed

+450
-4
lines changed

packages/autocomplete/src/index.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ describe('completer.completer', () => {
280280
const adjusted = collComplete
281281
.filter(c =>
282282
![
283-
'count', 'update', 'remove', 'insert', 'save', 'findAndModify', 'reIndex', 'mapReduce'
283+
'count', 'update', 'remove', 'insert', 'save', 'findAndModify', 'reIndex', 'mapReduce',
284+
// search index helpers are 6.0+
285+
'getSearchIndexes', 'createSearchIndex', 'createSearchIndexes', 'dropSearchIndex', 'updateSearchIndex'
284286
].includes(c)
285287
)
286288
.map(c => `${i}${c}`);

packages/i18n/src/locales/en_US.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,31 @@ const translations: Catalog = {
698698
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.configureQueryAnalyzer',
699699
description: 'Starts or stops collecting metrics about reads and writes against an unsharded or sharded collection.',
700700
example: 'db.coll.configureQueryAnalyzer(options)'
701+
},
702+
getSearchIndexes: {
703+
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.getSearchIndexes',
704+
description: 'Returns an array that holds a list of documents that identify and describe the existing search indexes on the collection.',
705+
example: 'db.coll.getSearchIndexes()'
706+
},
707+
createSearchIndexes: {
708+
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.createSearchIndexes',
709+
description: 'Creates one or more search indexes on a collection',
710+
example: 'db.coll.createSearchIndexes(descriptions)'
711+
},
712+
createSearchIndex: {
713+
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.createSearchIndex',
714+
description: 'Creates one search indexes on a collection',
715+
example: 'db.coll.createSearchIndex(description)'
716+
},
717+
dropSearchIndex: {
718+
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.dropSearchIndex',
719+
description: 'Drops or removes the specified search index from a collection.',
720+
example: 'db.coll.dropSearchIndex(name)'
721+
},
722+
updateSearchIndex: {
723+
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.updateSearchIndex',
724+
description: 'Updates the sepecified search index.',
725+
example: 'db.coll.updateSearchIndex(name, definition)'
701726
}
702727
}
703728
}

packages/service-provider-core/src/readable.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
FindCursor,
1313
DbOptions,
1414
ReadPreferenceFromOptions,
15-
ReadPreferenceLike
15+
ReadPreferenceLike,
1616
} from './all-transport-types';
1717
import { ChangeStream, ChangeStreamOptions } from './all-transport-types';
1818

@@ -206,5 +206,23 @@ export default interface Readable {
206206
db?: string,
207207
coll?: string
208208
): ChangeStream<Document>;
209+
210+
/**
211+
* Returns an array of documents that identify and describe the existing
212+
* search indexes on the collection.
213+
*
214+
* @param {String} database - The db name.
215+
* @param {String} collection - The collection name.
216+
* @param {Document} options - The command options.
217+
* @param {DbOptions} dbOptions - The database options
218+
*
219+
* @return {Promise}
220+
*/
221+
getSearchIndexes(
222+
database: string,
223+
collection: string,
224+
// TODO(MONGOSH-1471): use ListSearchIndexesOptions once available
225+
options: any,
226+
dbOptions?: DbOptions): Promise<Document[]>;
209227
}
210228

packages/service-provider-core/src/writable.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,5 +345,54 @@ export default interface Writable {
345345
options?: BulkWriteOptions,
346346
dbOptions?: DbOptions
347347
): Promise<OrderedBulkOperation | UnorderedBulkOperation>;
348+
349+
/**
350+
* Adds new search indexes to a collection.
351+
*
352+
* @param {String} database - The db name.
353+
* @param {String} collection - The collection name.
354+
* @param {Object[]} descriptions - The specs of the indexes to be created.
355+
* @param {DbOptions} dbOptions - The database options
356+
*/
357+
createSearchIndexes(
358+
database: string,
359+
collection: string,
360+
// TODO(MONGOSH-1471): use SearchIndexDescription[] once available
361+
descriptions: any[],
362+
dbOptions?: DbOptions
363+
): Promise<string[]>
364+
365+
/**
366+
* Drops a search index.
367+
*
368+
* @param {String} database - The db name.
369+
* @param {String} collection - The collection name.
370+
* @param {String} indexName - The index name
371+
* @param {DbOptions} dbOptions - The database options
372+
*/
373+
dropSearchIndex(
374+
database: string,
375+
collection: string,
376+
index: string,
377+
dbOptions?: DbOptions
378+
): Promise<void>
379+
380+
/**
381+
* Update a search index.
382+
*
383+
* @param {String} database - The db name.
384+
* @param {String} collection - The collection name.
385+
* @param {String} indexName - The index name.
386+
* @param {Object} description - The update.
387+
* @param {DbOptions} dbOptions - The database options
388+
*/
389+
updateSearchIndex(
390+
database: string,
391+
collection: string,
392+
index: string,
393+
// TODO(MONGOSH-1471): use SearchIndexDescription once available
394+
description: any,
395+
dbOptions?: DbOptions
396+
): Promise<void>
348397
}
349398

packages/service-provider-server/src/cli-service-provider.integration.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ describe('CliServiceProvider [integration]', function() {
577577
});
578578
});
579579

580+
// TODO(MONGOSH-1465): integration tests for search indexes
581+
580582
describe('#listCollections', () => {
581583
it('returns the list of collections', async() => {
582584
await db.createCollection('coll1');

packages/service-provider-server/src/cli-service-provider.spec.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,4 +861,122 @@ describe('CliServiceProvider', () => {
861861
).parentState).to.equal(undefined);
862862
});
863863
});
864+
865+
describe('#getSearchIndexes', () => {
866+
let descriptions;
867+
let nativeMethodResult;
868+
let getSearchIndexesOptions;
869+
870+
beforeEach(() => {
871+
descriptions = [
872+
{ name: 'foo', description: {} },
873+
{ name: 'bar', description: {} }
874+
];
875+
876+
nativeMethodResult = {
877+
toArray: () => {
878+
return Promise.resolve(descriptions);
879+
}
880+
};
881+
882+
getSearchIndexesOptions = { allowDiskUse: true };
883+
884+
collectionStub = stubInterface<Collection>();
885+
// @ts-expect-error still @internal
886+
collectionStub.listSearchIndexes.returns(nativeMethodResult);
887+
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
888+
});
889+
890+
it('calls listSearchIndexes and toArray on the resulting cursor', async() => {
891+
const result = await serviceProvider.getSearchIndexes(
892+
'db1',
893+
'coll1',
894+
getSearchIndexesOptions);
895+
expect(result).to.deep.equal(descriptions);
896+
// @ts-expect-error still @internal
897+
expect(collectionStub.listSearchIndexes).to.have.been.calledWith(getSearchIndexesOptions);
898+
});
899+
});
900+
901+
describe('#createSearchIndexes', () => {
902+
let descriptions;
903+
let nativeMethodResult;
904+
905+
beforeEach(() => {
906+
descriptions = [
907+
{ name: 'foo', description: {} },
908+
{ name: 'bar', description: {} }
909+
];
910+
911+
nativeMethodResult = [
912+
'index_1',
913+
];
914+
915+
collectionStub = stubInterface<Collection>();
916+
// @ts-expect-error still @internal
917+
collectionStub.createSearchIndexes.resolves(nativeMethodResult);
918+
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
919+
});
920+
921+
it('executes the command against the database', async() => {
922+
const result = await serviceProvider.createSearchIndexes(
923+
'db1',
924+
'coll1',
925+
descriptions);
926+
expect(result).to.deep.equal(nativeMethodResult);
927+
// @ts-expect-error still @internal
928+
expect(collectionStub.createSearchIndexes).to.have.been.calledWith(descriptions);
929+
});
930+
});
931+
932+
describe('#dropSearchIndex', () => {
933+
let indexName;
934+
935+
beforeEach(() => {
936+
indexName = 'foo';
937+
938+
collectionStub = stubInterface<Collection>();
939+
// @ts-expect-error still @internal
940+
collectionStub.dropSearchIndex.resolves();
941+
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
942+
});
943+
944+
it('executes the command against the database', async() => {
945+
const result = await serviceProvider.dropSearchIndex(
946+
'db1',
947+
'coll1',
948+
indexName);
949+
expect(result).to.deep.equal(undefined);
950+
// @ts-expect-error still @internal
951+
expect(collectionStub.dropSearchIndex).to.have.been.calledWith(indexName);
952+
});
953+
});
954+
955+
956+
describe('#updateSearchIndex', () => {
957+
let indexName;
958+
let description;
959+
960+
beforeEach(() => {
961+
indexName = 'foo';
962+
description = { x: 1, y: 2 };
963+
964+
collectionStub = stubInterface<Collection>();
965+
966+
// @ts-expect-error still @internal
967+
collectionStub.updateSearchIndex.resolves();
968+
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
969+
});
970+
971+
it('executes the command against the database', async() => {
972+
const result = await serviceProvider.updateSearchIndex(
973+
'db1',
974+
'coll1',
975+
indexName,
976+
description);
977+
expect(result).to.deep.equal(undefined);
978+
// @ts-expect-error still @internal
979+
expect(collectionStub.updateSearchIndex).to.have.been.calledWith(indexName, description);
980+
});
981+
});
864982
});

packages/service-provider-server/src/cli-service-provider.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,54 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
12061206
...opts
12071207
});
12081208
}
1209+
1210+
getSearchIndexes(
1211+
database: string,
1212+
collection: string,
1213+
// TODO(MONGOSH-1471): use ListSearchIndexesOptions once available
1214+
options: any,
1215+
dbOptions?: DbOptions): Promise<Document[]> {
1216+
return this.db(database, dbOptions)
1217+
.collection(collection)
1218+
// @ts-expect-error still @internal
1219+
.listSearchIndexes(options).toArray();
1220+
}
1221+
1222+
createSearchIndexes(
1223+
database: string,
1224+
collection: string,
1225+
// TODO(MONGOSH-1471): use SearchIndexDescription[] once available
1226+
descriptions: any[],
1227+
dbOptions?: DbOptions): Promise<string[]> {
1228+
return this.db(database, dbOptions)
1229+
.collection(collection)
1230+
// @ts-expect-error still @internal
1231+
.createSearchIndexes(descriptions);
1232+
}
1233+
1234+
dropSearchIndex(
1235+
database: string,
1236+
collection: string,
1237+
indexName: string,
1238+
dbOptions?: DbOptions): Promise<void> {
1239+
return this.db(database, dbOptions)
1240+
.collection(collection)
1241+
// @ts-expect-error still @internal
1242+
.dropSearchIndex(indexName);
1243+
}
1244+
1245+
updateSearchIndex(
1246+
database: string,
1247+
collection: string,
1248+
indexName: string,
1249+
// TODO(MONGOSH-1471): use SearchIndexDescription once available
1250+
description: any,
1251+
dbOptions?: DbOptions): Promise<void> {
1252+
return this.db(database, dbOptions)
1253+
.collection(collection)
1254+
// @ts-expect-error still @internal
1255+
.updateSearchIndex(indexName, description);
1256+
}
12091257
}
12101258

12111259
export default CliServiceProvider;

0 commit comments

Comments
 (0)