Skip to content

Commit 7ce8c73

Browse files
authored
DOCSP-41144: Atlas Search Indexes (#16)
1 parent 0379473 commit 7ce8c73

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

source/includes/indexes/indexes.kt

Lines changed: 30 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(
@@ -69,4 +71,32 @@ fun main() {
6971
println(result)
7072
}
7173
// end-index-compound-query
74+
75+
// start-create-search-index
76+
val index = Document("mappings", Document("dynamic", true))
77+
collection.createSearchIndex("<index name>", index)
78+
// end-create-search-index
79+
80+
// start-create-search-indexes
81+
val indexOne = SearchIndexModel("<first index name>", Document("mappings", Document("dynamic", true)))
82+
val indexTwo = SearchIndexModel("<second index name>", Document("mappings", Document("dynamic", true)))
83+
collection.createSearchIndexes(listOf(indexOne, indexTwo))
84+
// end-create-search-indexes
85+
86+
// start-list-search-indexes
87+
val results = collection.listSearchIndexes()
88+
89+
results.forEach { result ->
90+
println(result)
91+
}
92+
// end-list-search-indexes
93+
94+
// start-update-search-indexes
95+
val newIndex = Document("mappings", Document("dynamic", true))
96+
collection.updateSearchIndex("<index to update>", newIndex)
97+
// end-update-search-indexes
98+
99+
// start-drop-search-index
100+
collection.dropIndex("<index to delete>")
101+
// end-drop-search-index
72102
}

source/indexes/atlas-search-index.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
:atlas:`Atlas Search </atlas-search>` enables you to perform full-text searches on
24+
collections hosted on MongoDB Atlas. Atlas Search 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, and might return before
39+
confirming that they ran successfully. To determine the current status of the indexes,
40+
call the ``listSearchIndexes()`` method.
41+
42+
The following sections provide code examples that demonstrate how to use
43+
each of the preceding methods.
44+
45+
.. _kotlin-sync-atlas-search-index-create:
46+
47+
Create a Search Index
48+
---------------------
49+
50+
You can use the `createSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-search-index.html>`__
51+
and the `createSearchIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-search-indexes.html>`__
52+
methods to create one or more Atlas Search indexes.
53+
54+
The following code example shows how to create a single index:
55+
56+
.. literalinclude:: /includes/indexes/indexes.kt
57+
:language: kotlin
58+
:start-after: start-create-search-index
59+
:end-before: end-create-search-index
60+
:dedent:
61+
62+
The following code example shows how to create multiple indexes:
63+
64+
.. literalinclude:: /includes/indexes/indexes.kt
65+
:language: kotlin
66+
:start-after: start-create-search-indexes
67+
:end-before: end-create-search-indexes
68+
:dedent:
69+
70+
To learn more about the syntax used to define Atlas Search indexes, see the
71+
:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>` guide
72+
in the Atlas manual.
73+
74+
.. _kotlin-sync-atlas-search-index-list:
75+
76+
List Search Indexes
77+
-------------------
78+
79+
You can use the
80+
`listSearchIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/list-search-indexes.html>`__
81+
method to return all Atlas Search indexes in a collection.
82+
83+
The following code example shows how to print a list of the search indexes in
84+
a collection:
85+
86+
.. literalinclude:: /includes/indexes/indexes.kt
87+
:language: kotlin
88+
:start-after: start-list-search-indexes
89+
:end-before: end-list-search-indexes
90+
:dedent:
91+
92+
.. _kotlin-sync-atlas-search-index-update:
93+
94+
Update a Search Index
95+
---------------------
96+
97+
You can use the
98+
`updateSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/update-search-index.html>`__
99+
method to update an Atlas Search index.
100+
101+
The following code shows how to update a search index:
102+
103+
.. literalinclude:: /includes/indexes/indexes.kt
104+
:language: kotlin
105+
:start-after: start-update-search-indexes
106+
:end-before: end-update-search-indexes
107+
:dedent:
108+
109+
.. _kotlin-sync-atlas-search-index-drop:
110+
111+
Delete a Search Index
112+
---------------------
113+
114+
You can use the
115+
`dropSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-search-index.html>`__
116+
method to delete an Atlas Search index.
117+
118+
The following code shows how to delete a search index from a collection:
119+
120+
.. literalinclude:: /includes/indexes/indexes.kt
121+
:language: kotlin
122+
:start-after: start-drop-search-index
123+
:end-before: end-drop-search-index
124+
:dedent:
125+
126+
Additional Information
127+
----------------------
128+
129+
To learn more about MongoDB Atlas Search, see the :atlas:`Atlas Search Indexes </atlas-search/atlas-search-overview/>`
130+
documentation.

source/work-with-indexes.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Work with Indexes
2121

2222
/indexes/single-field-index.txt
2323
/indexes/compound-index.txt
24+
/indexes/atlas-search-index.txt
2425

2526
Overview
2627
--------
@@ -69,8 +70,7 @@ code for creating each index type.
6970

7071
- :ref:`kotlin-sync-single-field-index`
7172
- :ref:`kotlin-sync-compound-index`
72-
73-
.. TODO: - :ref:`kotlin-sync-atlas-search-index`
73+
- :ref:`kotlin-sync-atlas-search-index`
7474

7575
Remove an Index
7676
---------------

0 commit comments

Comments
 (0)