Skip to content

Commit a61c877

Browse files
rustagirmongoKart
authored andcommitted
DOCSP-30419: manage search indexes (mongodb#709)
* DOCSP-30419: manage search indexes * fix literalinclude * MW PR fixes 1 * include fix * wn fix
1 parent 8bdc23c commit a61c877

File tree

4 files changed

+161
-53
lines changed

4 files changed

+161
-53
lines changed

source/code-snippets/indexes/listSearchIndexes.js

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
// Replace the following with your MongoDB deployment's connection
4+
// string.
5+
const uri =
6+
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
7+
8+
const client = new MongoClient(uri);
9+
10+
import {
11+
MongoClient
12+
} from "mongodb";
13+
14+
async function run() {
15+
try {
16+
const database = client.db("<databaseName>");
17+
const collection = database.collection("<collectionName>");
18+
19+
// start createSearchIndex example
20+
const index1 = {
21+
name: "search1",
22+
definition: {
23+
"mappings": {
24+
"dynamic": true
25+
}
26+
}
27+
}
28+
await collection.createSearchIndex(index1);
29+
// end createSearchIndex example
30+
31+
// start listSearchIndexes example
32+
const result = await collection.listSearchIndexes().toArray();
33+
console.log("Existing search indexes:\n");
34+
for (const doc in result) {
35+
console.log(doc);
36+
}
37+
// end listSearchIndexes example
38+
39+
// start updateSearchIndex example
40+
const index2 = {
41+
"mappings": {
42+
"dynamic": true,
43+
"fields": {
44+
"description": {
45+
"type": "string"
46+
}
47+
}
48+
}
49+
}
50+
await collection.updateSearchIndex("search1", index2);
51+
// end updateSearchIndex example
52+
53+
// start dropSearchIndex example
54+
await collection.dropSearchIndex("search1");
55+
// end dropSearchIndex example
56+
} finally {
57+
await client.close();
58+
}
59+
}
60+
run().catch(console.dir);

source/fundamentals/indexes.txt

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ appropriate for your application, see the MongoDB server documentation on
6161
:manual:`Indexing Strategies </applications/indexes>` and
6262
:manual:`Data Modeling and Indexes </core/data-model-operations/#data-model-indexes>`.
6363

64+
List Indexes
65+
~~~~~~~~~~~~
66+
67+
You can use the ``listIndexes()`` method to list all of the indexes
68+
for a collection. The `listIndexes() <{+api+}/classes/Collection.html#listIndexes>`__ method takes an
69+
optional `ListIndexesOptions
70+
<{+api+}/interfaces/ListIndexesOptions.html>`__ parameter. The ``listIndexes()`` method returns an
71+
object of type `ListIndexesCursor
72+
<{+api+}/classes/ListIndexesCursor.html>`__.
73+
74+
The following code uses the ``listIndexes()`` method to list all the
75+
indexes in a collection:
76+
77+
.. literalinclude:: /code-snippets/indexes/listIndexes.js
78+
:language: javascript
79+
:dedent:
80+
:start-after: start listIndexes example
81+
:end-before: end listIndexes example
82+
6483
Index Types
6584
-----------
6685

@@ -313,42 +332,97 @@ the following:
313332

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

316-
List Indexes
317-
------------
335+
.. _node-indexes-search:
318336

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

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

329-
.. literalinclude:: /code-snippets/indexes/listIndexes.js
344+
Before you can perform a search on an Atlas collection, you must first
345+
create an Atlas Search index on the collection. An Atlas Search
346+
index is a data structure that categorizes data in a searchable format.
347+
348+
You can use the following methods to manage your Search indexes:
349+
350+
- ``createSearchIndex()``
351+
- ``createSearchIndexes()``
352+
- ``listSearchIndexes()``
353+
- ``updateSearchIndex()``
354+
- ``dropSearchIndex()``
355+
356+
The following sections provide code samples that use each of the preceding
357+
methods to manage Search indexes.
358+
359+
Create a Search Index
360+
~~~~~~~~~~~~~~~~~~~~~
361+
362+
You can use the `createSearchIndex()
363+
<{+api+}/classes/Collection.html#createSearchIndex>`__ and
364+
`createSearchIndexes() <{+api+}/classes/Collection.html#createSearchIndexes>`__
365+
methods to create new Search indexes.
366+
367+
The following code shows how to
368+
use the ``createSearchIndex()`` method to create an index called
369+
``search1``:
370+
371+
.. literalinclude:: /code-snippets/indexes/searchIndexes.js
330372
:language: javascript
331373
:dedent:
332-
:start-after: start listIndexes example
333-
:end-before: end listIndexes example
374+
:start-after: start createSearchIndex example
375+
:end-before: end createSearchIndex example
334376

335377
List Search Indexes
336378
~~~~~~~~~~~~~~~~~~~
337379

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

347-
The following code uses the ``listSearchIndexes()`` method to list all
348-
the search indexes in the collection:
387+
The following code uses the ``listSearchIndexes()`` method to list the
388+
Search indexes in a collection:
349389

350-
.. literalinclude:: /code-snippets/indexes/listSearchIndexes.js
390+
.. literalinclude:: /code-snippets/indexes/searchIndexes.js
351391
:language: javascript
352392
:dedent:
353393
:start-after: start listSearchIndexes example
354-
:end-before: end listSearchIndexes example
394+
:end-before: end listSearchIndexes example
395+
396+
Update a Search Index
397+
~~~~~~~~~~~~~~~~~~~~~
398+
399+
You can use the `updateSearchIndex()
400+
<{+api+}/classes/Collection.html#updateSearchIndex>`__ method to update a Search
401+
index.
402+
403+
The following code shows how to
404+
use the ``updateSearchIndex()`` method to update an index called
405+
``search1`` to specify a string type for the ``description`` field:
406+
407+
.. literalinclude:: /code-snippets/indexes/searchIndexes.js
408+
:language: javascript
409+
:dedent:
410+
:start-after: start updateSearchIndex example
411+
:end-before: end updateSearchIndex example
412+
413+
Drop a Search Index
414+
~~~~~~~~~~~~~~~~~~~
415+
416+
You can use the `dropSearchIndex()
417+
<{+api+}/classes/Collection.html#dropSearchIndex>`__ method to remove a Search
418+
index.
419+
420+
The following code shows how to
421+
use the ``dropSearchIndex()`` method to remove an index called
422+
``search1``:
423+
424+
.. literalinclude:: /code-snippets/indexes/searchIndexes.js
425+
:language: javascript
426+
:dedent:
427+
:start-after: start dropSearchIndex example
428+
:end-before: end dropSearchIndex example

source/whats-new.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ The {+driver-short+} v5.7 release includes the following features:
7272
- Support for change stream split events which enables processing change
7373
stream documents that exceed the 16MB maximum BSON size limit.
7474

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

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

0 commit comments

Comments
 (0)