Skip to content
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
30 changes: 30 additions & 0 deletions source/includes/indexes/indexes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import com.mongodb.MongoClientSettings
import com.mongodb.client.model.Indexes
import com.mongodb.client.model.Filters
import com.mongodb.client.model.Sorts
import com.mongodb.client.model.SearchIndexModel
import com.mongodb.kotlin.client.MongoClient
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.types.ObjectId
import org.bson.Document

// start-movie-class
data class Movie(
Expand Down Expand Up @@ -69,4 +71,32 @@ fun main() {
println(result)
}
// end-index-compound-query

// start-create-search-index
val index = Document("mappings", Document("dynamic", true))
collection.createSearchIndex("<index name>", index)
// end-create-search-index

// start-create-search-indexes
val indexOne = SearchIndexModel("<first index name>", Document("mappings", Document("dynamic", true)))
val indexTwo = SearchIndexModel("<second index name>", Document("mappings", Document("dynamic", true)))
collection.createSearchIndexes(listOf(indexOne, indexTwo))
// end-create-search-indexes

// start-list-search-indexes
val results = collection.listSearchIndexes()

results.forEach { result ->
println(result)
}
// end-list-search-indexes

// start-update-search-indexes
val newIndex = Document("mappings", Document("dynamic", true))
collection.updateSearchIndex("<index to update>", newIndex)
// end-update-search-indexes

// start-drop-search-index
collection.dropIndex("<index to delete>")
// end-drop-search-index
}
130 changes: 130 additions & 0 deletions source/indexes/atlas-search-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
.. _kotlin-sync-atlas-search-index:

====================
Atlas Search Indexes
====================

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: index, query, optimization, efficiency

Overview
--------

:atlas:`Atlas Search </atlas-search>` enables you to perform full-text searches on
collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of
the search and which fields to index.

You can call the following methods on a collection to manage your Atlas Search
indexes:

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

.. note::

The Atlas Search index management methods run asynchronously, and might return before
confirming that they ran successfully. To determine the current status of the indexes,
call the ``listSearchIndexes()`` method.

The following sections provide code examples that demonstrate how to use
each of the preceding methods.

.. _kotlin-sync-atlas-search-index-create:

Create a Search Index
---------------------

You can use the `createSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-search-index.html>`__
and the `createSearchIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-search-indexes.html>`__
methods to create one or more Atlas Search indexes.

The following code example shows how to create a single index:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-create-search-index
:end-before: end-create-search-index
:dedent:

The following code example shows how to create multiple indexes:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-create-search-indexes
:end-before: end-create-search-indexes
:dedent:

To learn more about the syntax used to define Atlas Search indexes, see the
:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>` guide
in the Atlas manual.

.. _kotlin-sync-atlas-search-index-list:

List Search Indexes
-------------------

You can use the
`listSearchIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/list-search-indexes.html>`__
method to return all Atlas Search indexes in a collection.

The following code example shows how to print a list of the search indexes in
a collection:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-list-search-indexes
:end-before: end-list-search-indexes
:dedent:

.. _kotlin-sync-atlas-search-index-update:

Update a Search Index
---------------------

You can use the
`updateSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/update-search-index.html>`__
method to update an Atlas Search index.

The following code shows how to update a search index:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-update-search-indexes
:end-before: end-update-search-indexes
:dedent:

.. _kotlin-sync-atlas-search-index-drop:

Delete a Search Index
---------------------

You can use the
`dropSearchIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-search-index.html>`__
method to delete an Atlas Search index.

The following code shows how to delete a search index from a collection:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-drop-search-index
:end-before: end-drop-search-index
:dedent:

Additional Information
----------------------

To learn more about MongoDB Atlas Search, see the :atlas:`Atlas Search Indexes </atlas-search/atlas-search-overview/>`
documentation.
4 changes: 2 additions & 2 deletions source/work-with-indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Work with Indexes

/indexes/single-field-index.txt
/indexes/compound-index.txt
/indexes/atlas-search-index.txt

Overview
--------
Expand Down Expand Up @@ -69,8 +70,7 @@ code for creating each index type.

- :ref:`kotlin-sync-single-field-index`
- :ref:`kotlin-sync-compound-index`

.. TODO: - :ref:`kotlin-sync-atlas-search-index`
- :ref:`kotlin-sync-atlas-search-index`

Remove an Index
---------------
Expand Down
Loading