Skip to content

Commit ff65ccf

Browse files
committed
DOCSP-30419: manage search indexes
1 parent 63c5b7b commit ff65ccf

File tree

3 files changed

+159
-50
lines changed

3 files changed

+159
-50
lines changed

source/code-snippets/indexes/listSearchIndexes.js

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
const database = client.db("<databaseName>");
14+
const collection = database.collection("<collectionName>");
15+
16+
async function run() {
17+
try {
18+
const database = client.db("<databaseName>");
19+
const collection = database.collection("<collectionName>");
20+
21+
// start createSearchIndex example
22+
const index1 = {
23+
name: "search1",
24+
definition: {
25+
"mappings": {
26+
"dynamic": true
27+
}
28+
}
29+
}
30+
await collection.createSearchIndex(index1);
31+
// end createSearchIndex example
32+
33+
// start listSearchIndexes example
34+
const result = await collection.listSearchIndexes().toArray();
35+
console.log("Existing search indexes:\n");
36+
for (const doc in result) {
37+
console.log(doc);
38+
}
39+
// end listSearchIndexes example
40+
41+
// start updateSearchIndex example
42+
const index2 = {
43+
"mappings": {
44+
"dynamic": true,
45+
"fields": {
46+
"description": {
47+
"type": "string"
48+
}
49+
}
50+
}
51+
}
52+
await collection.updateSearchIndex("search1", index2);
53+
// end updateSearchIndex example
54+
55+
// start dropSearchIndex example
56+
await collection.dropSearchIndex("search1");
57+
// end dropSearchIndex example
58+
} finally {
59+
await client.close();
60+
}
61+
}
62+
run().catch(console.dir);

source/fundamentals/indexes.txt

Lines changed: 97 additions & 23 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+
Search Indexes
336+
--------------
318337

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>`__.
338+
Atlas Search is a feature that allows you to perform full-text
339+
searches. To learn more, see the :ref:`Atlas Search <fts-top-ref>`
340+
documentation.
325341

326-
The following code uses the ``listIndexes()`` method to list all the
327-
indexes in a collection:
342+
Before you can perform a search on an Atlas collection, you must first
343+
create an Atlas Search index on the collection. An Atlas Search
344+
index is a data structure that categorizes data in a searchable format.
328345

329-
.. literalinclude:: /code-snippets/indexes/listIndexes.js
346+
Starting in version 5.7 of the {+driver-short+}, you can manage your
347+
Search indexes using the following methods:
348+
349+
- ``createSearchIndex()``
350+
- ``createSearchIndexes()``
351+
- ``listSearchIndexes()``
352+
- ``updateSearchIndex()``
353+
- ``dropSearchIndex()``
354+
355+
The following sections provide example code using each of the preceding
356+
methods to manage Search indexes.
357+
358+
Create Search Indexes
359+
~~~~~~~~~~~~~~~~~~~~~
360+
361+
You can use the `createSearchIndex()
362+
<{+api+}/classes/Collection.html#createSearchIndex>`__ and
363+
`createSearchIndexes() <{+api+}/classes/Collection.html#createSearchIndexes>`__
364+
methods to create new Search indexes.
365+
366+
The following code shows how to
367+
use the ``createSearchIndex()`` method to create an index called
368+
``search1``:
369+
370+
.. literalinclude:: /code-snippets/indexes/searchIndexes.js
330371
:language: javascript
331372
:dedent:
332-
:start-after: start listIndexes example
333-
:end-before: end listIndexes example
373+
:start-after: start createSearchIndex example
374+
:end-before: end createSearchIndex example
334375

335376
List Search Indexes
336377
~~~~~~~~~~~~~~~~~~~
337378

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>`__
379+
You can use the `listSearchIndexes()
380+
<{+api+}/classes/Collection.html#listSearchIndexes>`__
340381
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.
382+
collection. The ``listSearchIndexes()`` method takes an optional string
383+
parameter, ``name``, to return only the indexes with matching index
384+
names. It also takes an optional `aggregateOptions
385+
<{+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

350390
.. literalinclude:: /code-snippets/indexes/listSearchIndexes.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 Search Indexes
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 dropSearchIndex example
411+
:end-before: end dropSearchIndex example
412+
413+
Remove Search Indexes
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

0 commit comments

Comments
 (0)