Skip to content

Commit cf6712a

Browse files
committed
DOCSP-41144: Atlas Search Indexes
1 parent 12ef143 commit cf6712a

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

source/includes/indexes/indexes.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import com.mongodb.MongoClientSettings
33
import com.mongodb.client.model.Indexes
44
import com.mongodb.client.model.Filters
55
import com.mongodb.client.model.Sorts
6+
import com.mongodb.client.model.SearchIndexModel
67
import com.mongodb.kotlin.client.MongoClient
78
import org.bson.codecs.pojo.annotations.BsonId
89
import org.bson.types.ObjectId
10+
import org.bson.Document
911

1012
// start-movie-class
1113
data class Movie(
@@ -51,4 +53,33 @@ fun main() {
5153
println(result)
5254
}
5355
// end-index-single-query
56+
57+
// start-create-search-index
58+
val index = Document("mappings", Document("dynamic", true))
59+
collection.createSearchIndex("<index name>", index)
60+
// end-create-search-index
61+
62+
// start-create-search-indexes
63+
val indexOne = SearchIndexModel("<first index name>", Document("mappings", Document("dynamic", true)))
64+
val indexTwo = SearchIndexModel("<second index name>", Document("mappings", Document("dynamic", true)))
65+
collection.createSearchIndexes(listOf(indexOne, indexTwo))
66+
// end-create-search-indexes
67+
68+
// start-list-search-indexes
69+
val results = collection.listSearchIndexes()
70+
71+
results.forEach { result ->
72+
println(result)
73+
}
74+
// end-list-search-indexes
75+
76+
// start-update-search-indexes
77+
val newIndex = Document("mappings", Document("dynamic", true))
78+
collection.updateSearchIndex("<index to update>", newIndex)
79+
// end-update-search-indexes
80+
81+
// start-drop-search-index
82+
collection.dropIndex("<index to delete>")
83+
// end-drop-search-index
84+
5485
}

source/indexes/atlas-search-index.txt

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
.. _kotlin-sync-atlas-search-index:
2+
3+
====================
4+
Atlas Search Indexes
5+
====================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: index, query, optimization, efficiency
19+
20+
Overview
21+
--------
22+
23+
The Atlas Search feature enables you to perform full-text searches on
24+
collections hosted on MongoDB Atlas. The indexes specify the behavior of
25+
the search and which fields to index.
26+
27+
You can call the following methods on a collection to manage your Atlas Search
28+
indexes:
29+
30+
- ``createSearchIndex()``
31+
- ``createSearchIndexes()``
32+
- ``listSearchIndexes()``
33+
- ``updateSearchIndex()``
34+
- ``dropSearchIndex()``
35+
36+
.. note::
37+
38+
The Atlas Search Index management methods run asynchronously. The
39+
driver methods can return before confirming that they ran
40+
successfully. To determine the current status of the indexes, call the
41+
``listSearchIndexes()`` method.
42+
43+
The following sections provide code examples that demonstrate how to use
44+
each of the preceding methods.
45+
46+
.. _kotlin-sync-atlas-search-index-create:
47+
48+
Create a Search Index
49+
---------------------
50+
51+
You can use the `createSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-search-index.html>`__
52+
and the `createSearchIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-search-indexes.html>`__
53+
methods to create Atlas Search indexes.
54+
55+
The following code example shows how to create a single index:
56+
57+
.. literalinclude:: /includes/indexes/indexes.kt
58+
:language: kotlin
59+
:start-after: start-create-search-index
60+
:end-before: end-create-search-index
61+
:dedent:
62+
63+
The following code example shows how to create multiple indexes:
64+
65+
.. literalinclude:: /includes/indexes/indexes.kt
66+
:language: kotlin
67+
:start-after: start-create-search-indexes
68+
:end-before: end-create-search-indexes
69+
:dedent:
70+
71+
.. _kotlin-sync-atlas-search-index-list:
72+
73+
List Search Indexes
74+
-------------------
75+
76+
You can use the
77+
`listSearchIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/list-search-indexes.html>`__
78+
method to return the Atlas Search indexes of a collection.
79+
80+
The following code example shows how to print a list of the search indexes of
81+
a collection:
82+
83+
.. literalinclude:: /includes/indexes/indexes.kt
84+
:language: kotlin
85+
:start-after: start-list-search-indexes
86+
:end-before: end-list-search-indexes
87+
:dedent:
88+
89+
.. _kotlin-sync-atlas-search-index-update:
90+
91+
Update a Search Index
92+
---------------------
93+
94+
You can use the
95+
`updateSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/update-search-index.html>`__
96+
method to update an Atlas Search index.
97+
98+
The following code shows how to update a search index:
99+
100+
.. literalinclude:: /includes/indexes/indexes.kt
101+
:language: kotlin
102+
:start-after: start-update-search-indexes
103+
:end-before: end-update-search-indexes
104+
:dedent:
105+
106+
.. _kotlin-sync-atlas-search-index-drop:
107+
108+
Delete a Search Index
109+
---------------------
110+
111+
You can use the
112+
`dropSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-search-index.html>`__
113+
method to delete an Atlas Search index.
114+
115+
The following code shows how to delete a search index from a collection:
116+
117+
.. literalinclude:: /includes/indexes/indexes.kt
118+
:language: kotlin
119+
:start-after: start-drop-search-index
120+
:end-before: end-drop-search-index
121+
:dedent:
122+
123+
Additional Information
124+
----------------------
125+
126+
To learn more about MongoDB Atlas Search, see the :atlas:`Atlas Search Indexes </atlas-search/atlas-search-overview/>`
127+
documentation.

source/work-with-indexes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Work with Indexes
2020
.. toctree::
2121

2222
/indexes/single-field-index.txt
23+
/indexes/atlas-search-index.txt
2324

2425
Overview
2526
--------
@@ -67,9 +68,9 @@ The following pages describe the most common index types and provide sample
6768
code for creating each index type.
6869

6970
- :ref:`kotlin-sync-single-field-index`
71+
- :ref:`kotlin-sync-atlas-search-index`
7072

7173
.. TODO: - :ref:`kotlin-sync-compound-index`
72-
.. TODO: - :ref:`kotlin-sync-atlas-search-index`
7374

7475
Remove an Index
7576
---------------

0 commit comments

Comments
 (0)