Skip to content

feat(shell-api): search index management helpers MONGOSH-1455 MONGOSH-1456 #1477

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

Merged
merged 7 commits into from
Jun 6, 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
4 changes: 3 additions & 1 deletion packages/autocomplete/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ describe('completer.completer', () => {
const adjusted = collComplete
.filter(c =>
![
'count', 'update', 'remove', 'insert', 'save', 'findAndModify', 'reIndex', 'mapReduce'
'count', 'update', 'remove', 'insert', 'save', 'findAndModify', 'reIndex', 'mapReduce',
// search index helpers are 6.0+
'getSearchIndexes', 'createSearchIndex', 'createSearchIndexes', 'dropSearchIndex', 'updateSearchIndex'
].includes(c)
)
.map(c => `${i}${c}`);
Expand Down
25 changes: 25 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,31 @@ const translations: Catalog = {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.configureQueryAnalyzer',
description: 'Starts or stops collecting metrics about reads and writes against an unsharded or sharded collection.',
example: 'db.coll.configureQueryAnalyzer(options)'
},
getSearchIndexes: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.getSearchIndexes',
description: 'Returns an array that holds a list of documents that identify and describe the existing search indexes on the collection.',
example: 'db.coll.getSearchIndexes()'
},
createSearchIndexes: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.createSearchIndexes',
description: 'Creates one or more search indexes on a collection',
example: 'db.coll.createSearchIndexes(descriptions)'
},
createSearchIndex: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.createSearchIndex',
description: 'Creates one search indexes on a collection',
example: 'db.coll.createSearchIndex(description)'
},
dropSearchIndex: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.dropSearchIndex',
description: 'Drops or removes the specified search index from a collection.',
example: 'db.coll.dropSearchIndex(name)'
},
updateSearchIndex: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.updateSearchIndex',
description: 'Updates the sepecified search index.',
example: 'db.coll.updateSearchIndex(name, definition)'
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion packages/service-provider-core/src/readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
FindCursor,
DbOptions,
ReadPreferenceFromOptions,
ReadPreferenceLike
ReadPreferenceLike,
} from './all-transport-types';
import { ChangeStream, ChangeStreamOptions } from './all-transport-types';

Expand Down Expand Up @@ -206,5 +206,23 @@ export default interface Readable {
db?: string,
coll?: string
): ChangeStream<Document>;

/**
* Returns an array of documents that identify and describe the existing
* search indexes on the collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Document} options - The command options.
* @param {DbOptions} dbOptions - The database options
*
* @return {Promise}
*/
getSearchIndexes(
database: string,
collection: string,
// TODO(MONGOSH-1471): use ListSearchIndexesOptions once available
options: any,
dbOptions?: DbOptions): Promise<Document[]>;
}

49 changes: 49 additions & 0 deletions packages/service-provider-core/src/writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,54 @@ export default interface Writable {
options?: BulkWriteOptions,
dbOptions?: DbOptions
): Promise<OrderedBulkOperation | UnorderedBulkOperation>;

/**
* Adds new search indexes to a collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Object[]} descriptions - The specs of the indexes to be created.
* @param {DbOptions} dbOptions - The database options
*/
createSearchIndexes(
database: string,
collection: string,
// TODO(MONGOSH-1471): use SearchIndexDescription[] once available
descriptions: any[],
dbOptions?: DbOptions
): Promise<string[]>

/**
* Drops a search index.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {String} indexName - The index name
* @param {DbOptions} dbOptions - The database options
*/
dropSearchIndex(
database: string,
collection: string,
index: string,
dbOptions?: DbOptions
): Promise<void>

/**
* Update a search index.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {String} indexName - The index name.
* @param {Object} description - The update.
* @param {DbOptions} dbOptions - The database options
*/
updateSearchIndex(
database: string,
collection: string,
index: string,
// TODO(MONGOSH-1471): use SearchIndexDescription once available
description: any,
dbOptions?: DbOptions
): Promise<void>
}

Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ describe('CliServiceProvider [integration]', function() {
});
});

// TODO(MONGOSH-1465): integration tests for search indexes

describe('#listCollections', () => {
it('returns the list of collections', async() => {
await db.createCollection('coll1');
Expand Down
118 changes: 118 additions & 0 deletions packages/service-provider-server/src/cli-service-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,122 @@ describe('CliServiceProvider', () => {
).parentState).to.equal(undefined);
});
});

describe('#getSearchIndexes', () => {
let descriptions;
let nativeMethodResult;
let getSearchIndexesOptions;

beforeEach(() => {
descriptions = [
{ name: 'foo', description: {} },
{ name: 'bar', description: {} }
];

nativeMethodResult = {
toArray: () => {
return Promise.resolve(descriptions);
}
};

getSearchIndexesOptions = { allowDiskUse: true };

collectionStub = stubInterface<Collection>();
// @ts-expect-error still @internal
collectionStub.listSearchIndexes.returns(nativeMethodResult);
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('calls listSearchIndexes and toArray on the resulting cursor', async() => {
const result = await serviceProvider.getSearchIndexes(
'db1',
'coll1',
getSearchIndexesOptions);
expect(result).to.deep.equal(descriptions);
// @ts-expect-error still @internal
expect(collectionStub.listSearchIndexes).to.have.been.calledWith(getSearchIndexesOptions);
});
});

describe('#createSearchIndexes', () => {
let descriptions;
let nativeMethodResult;

beforeEach(() => {
descriptions = [
{ name: 'foo', description: {} },
{ name: 'bar', description: {} }
];

nativeMethodResult = [
'index_1',
];

collectionStub = stubInterface<Collection>();
// @ts-expect-error still @internal
collectionStub.createSearchIndexes.resolves(nativeMethodResult);
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('executes the command against the database', async() => {
const result = await serviceProvider.createSearchIndexes(
'db1',
'coll1',
descriptions);
expect(result).to.deep.equal(nativeMethodResult);
// @ts-expect-error still @internal
expect(collectionStub.createSearchIndexes).to.have.been.calledWith(descriptions);
});
});

describe('#dropSearchIndex', () => {
let indexName;

beforeEach(() => {
indexName = 'foo';

collectionStub = stubInterface<Collection>();
// @ts-expect-error still @internal
collectionStub.dropSearchIndex.resolves();
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('executes the command against the database', async() => {
const result = await serviceProvider.dropSearchIndex(
'db1',
'coll1',
indexName);
expect(result).to.deep.equal(undefined);
// @ts-expect-error still @internal
expect(collectionStub.dropSearchIndex).to.have.been.calledWith(indexName);
});
});


describe('#updateSearchIndex', () => {
let indexName;
let description;

beforeEach(() => {
indexName = 'foo';
description = { x: 1, y: 2 };

collectionStub = stubInterface<Collection>();

// @ts-expect-error still @internal
collectionStub.updateSearchIndex.resolves();
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('executes the command against the database', async() => {
const result = await serviceProvider.updateSearchIndex(
'db1',
'coll1',
indexName,
description);
expect(result).to.deep.equal(undefined);
// @ts-expect-error still @internal
expect(collectionStub.updateSearchIndex).to.have.been.calledWith(indexName, description);
});
});
});
48 changes: 48 additions & 0 deletions packages/service-provider-server/src/cli-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,54 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
...opts
});
}

getSearchIndexes(
database: string,
collection: string,
// TODO(MONGOSH-1471): use ListSearchIndexesOptions once available
options: any,
dbOptions?: DbOptions): Promise<Document[]> {
return this.db(database, dbOptions)
.collection(collection)
// @ts-expect-error still @internal
.listSearchIndexes(options).toArray();
}

createSearchIndexes(
database: string,
collection: string,
// TODO(MONGOSH-1471): use SearchIndexDescription[] once available
descriptions: any[],
dbOptions?: DbOptions): Promise<string[]> {
return this.db(database, dbOptions)
.collection(collection)
// @ts-expect-error still @internal
.createSearchIndexes(descriptions);
}

dropSearchIndex(
database: string,
collection: string,
indexName: string,
dbOptions?: DbOptions): Promise<void> {
return this.db(database, dbOptions)
.collection(collection)
// @ts-expect-error still @internal
.dropSearchIndex(indexName);
}

updateSearchIndex(
database: string,
collection: string,
indexName: string,
// TODO(MONGOSH-1471): use SearchIndexDescription once available
description: any,
dbOptions?: DbOptions): Promise<void> {
return this.db(database, dbOptions)
.collection(collection)
// @ts-expect-error still @internal
.updateSearchIndex(indexName, description);
}
}

export default CliServiceProvider;
Expand Down
Loading