Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 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 @@ -51,4 +53,33 @@ fun main() {
println(result)
}
// end-index-single-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

}
127 changes: 127 additions & 0 deletions source/indexes/atlas-search-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
.. _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
--------

The Atlas Search feature enables you to perform full-text searches on
collections hosted on MongoDB Atlas. The 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. The
driver methods can 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 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:

.. _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 the Atlas Search indexes of a collection.

The following code example shows how to print a list of the search indexes of
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.
3 changes: 2 additions & 1 deletion source/work-with-indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Work with Indexes
.. toctree::

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

Overview
--------
Expand Down Expand Up @@ -67,9 +68,9 @@ The following pages describe the most common index types and provide sample
code for creating each index type.

- :ref:`kotlin-sync-single-field-index`
- :ref:`kotlin-sync-atlas-search-index`

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

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