Skip to content

DOCSP-30419: manage search indexes #709

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 5 commits into from
Jul 10, 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
27 changes: 0 additions & 27 deletions source/code-snippets/indexes/listSearchIndexes.js

This file was deleted.

60 changes: 60 additions & 0 deletions source/code-snippets/indexes/searchIndexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { MongoClient } = require("mongodb");

// Replace the following with your MongoDB deployment's connection
// string.
const uri =
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";

const client = new MongoClient(uri);

import {
MongoClient
} from "mongodb";

async function run() {
try {
const database = client.db("<databaseName>");
const collection = database.collection("<collectionName>");

// start createSearchIndex example
const index1 = {
name: "search1",
definition: {
"mappings": {
"dynamic": true
}
}
}
await collection.createSearchIndex(index1);
// end createSearchIndex example

// start listSearchIndexes example
const result = await collection.listSearchIndexes().toArray();
console.log("Existing search indexes:\n");
for (const doc in result) {
console.log(doc);
}
// end listSearchIndexes example

// start updateSearchIndex example
const index2 = {
"mappings": {
"dynamic": true,
"fields": {
"description": {
"type": "string"
}
}
}
}
await collection.updateSearchIndex("search1", index2);
// end updateSearchIndex example

// start dropSearchIndex example
await collection.dropSearchIndex("search1");
// end dropSearchIndex example
} finally {
await client.close();
}
}
run().catch(console.dir);
124 changes: 99 additions & 25 deletions source/fundamentals/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ appropriate for your application, see the MongoDB server documentation on
:manual:`Indexing Strategies </applications/indexes>` and
:manual:`Data Modeling and Indexes </core/data-model-operations/#data-model-indexes>`.

List Indexes
~~~~~~~~~~~~

You can use the ``listIndexes()`` method to list all of the indexes
for a collection. The `listIndexes() <{+api+}/classes/Collection.html#listIndexes>`__ method takes an
optional `ListIndexesOptions
<{+api+}/interfaces/ListIndexesOptions.html>`__ parameter. The ``listIndexes()`` method returns an
object of type `ListIndexesCursor
<{+api+}/classes/ListIndexesCursor.html>`__.

The following code uses the ``listIndexes()`` method to list all the
indexes in a collection:

.. literalinclude:: /code-snippets/indexes/listIndexes.js
:language: javascript
:dedent:
:start-after: start listIndexes example
:end-before: end listIndexes example

Index Types
-----------

Expand Down Expand Up @@ -313,42 +332,97 @@ the following:

To learn more, see :manual:`Unique Indexes </core/index-unique>`.

List Indexes
------------
.. _node-indexes-search:

You can use the ``listIndexes()`` method to list all of the indexes
for a collection. The `listIndexes() <{+api+}/classes/Collection.html#listIndexes>`__ method takes an
optional `ListIndexesOptions
<{+api+}/interfaces/ListIndexesOptions.html>`__ parameter. The ``listIndexes()`` method returns an
object of type `ListIndexesCursor
<{+api+}/classes/ListIndexesCursor.html>`__.
Search Indexes
--------------

The following code uses the ``listIndexes()`` method to list all the
indexes in a collection:
Atlas Search is a feature that allows you to perform full-text
searches. To learn more, see the :ref:`Atlas Search <fts-top-ref>`
documentation.

.. literalinclude:: /code-snippets/indexes/listIndexes.js
Before you can perform a search on an Atlas collection, you must first
create an Atlas Search index on the collection. An Atlas Search
index is a data structure that categorizes data in a searchable format.

You can use the following methods to manage your Search indexes:

- ``createSearchIndex()``
- ``createSearchIndexes()``
- ``listSearchIndexes()``
- ``updateSearchIndex()``
- ``dropSearchIndex()``

The following sections provide code samples that use each of the preceding
methods to manage Search indexes.

Create a Search Index
~~~~~~~~~~~~~~~~~~~~~

You can use the `createSearchIndex()
<{+api+}/classes/Collection.html#createSearchIndex>`__ and
`createSearchIndexes() <{+api+}/classes/Collection.html#createSearchIndexes>`__
methods to create new Search indexes.

The following code shows how to
use the ``createSearchIndex()`` method to create an index called
``search1``:

.. literalinclude:: /code-snippets/indexes/searchIndexes.js
:language: javascript
:dedent:
:start-after: start listIndexes example
:end-before: end listIndexes example
:start-after: start createSearchIndex example
:end-before: end createSearchIndex example

List Search Indexes
~~~~~~~~~~~~~~~~~~~

When connecting to {+mdb-server+} version 7.0 or later, you can use the new `listSearchIndexes()
<https://mongodb.github.io/node-mongodb-native/Next/classes/Collection.html#listSearchIndexes>`__
method to return a cursor that contains the search indexes of a given
collection. The ``listSearchIndexes()`` method takes an optional string parameter, ``name``, to
return only the indexes with matching index names. It also takes an
optional `aggregateOptions
<https://mongodb.github.io/node-mongodb-native/Next/interfaces/AggregateOptions.html>`__
parameter.
You can use the `listSearchIndexes()
<{+api+}/classes/Collection.html#listSearchIndexes>`__
method to return a cursor that contains the Search indexes of a given
collection. The ``listSearchIndexes()`` method takes an optional string
parameter, ``name``, to return only the indexes with matching names. It
also takes an optional `aggregateOptions <{+api+}/interfaces/AggregateOptions.html>`__ parameter.

The following code uses the ``listSearchIndexes()`` method to list all
the search indexes in the collection:
The following code uses the ``listSearchIndexes()`` method to list the
Search indexes in a collection:

.. literalinclude:: /code-snippets/indexes/listSearchIndexes.js
.. literalinclude:: /code-snippets/indexes/searchIndexes.js
:language: javascript
:dedent:
:start-after: start listSearchIndexes example
:end-before: end listSearchIndexes example
:end-before: end listSearchIndexes example

Update a Search Index
~~~~~~~~~~~~~~~~~~~~~

You can use the `updateSearchIndex()
<{+api+}/classes/Collection.html#updateSearchIndex>`__ method to update a Search
index.

The following code shows how to
use the ``updateSearchIndex()`` method to update an index called
``search1`` to specify a string type for the ``description`` field:

.. literalinclude:: /code-snippets/indexes/searchIndexes.js
:language: javascript
:dedent:
:start-after: start updateSearchIndex example
:end-before: end updateSearchIndex example

Drop a Search Index
~~~~~~~~~~~~~~~~~~~

You can use the `dropSearchIndex()
<{+api+}/classes/Collection.html#dropSearchIndex>`__ method to remove a Search
index.

The following code shows how to
use the ``dropSearchIndex()`` method to remove an index called
``search1``:

.. literalinclude:: /code-snippets/indexes/searchIndexes.js
:language: javascript
:dedent:
:start-after: start dropSearchIndex example
:end-before: end dropSearchIndex example
3 changes: 2 additions & 1 deletion source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ The {+driver-short+} v5.7 release includes the following features:
- Support for change stream split events which enables processing change
stream documents that exceed the 16MB maximum BSON size limit.

- A new API for managing Atlas Search indexes.
- An API to manage Search indexes from within your application. To
learn more, see :ref:`Search Indexes <node-indexes-search>`.

To learn more about this release, see the
`v5.7.0 Release Highlights <https://github.com/mongodb/node-mongodb-native/releases/tag/v5.7.0>`__.
Expand Down