diff --git a/source/includes/indexes/indexes.kt b/source/includes/indexes/indexes.kt index 23672969..e8c987f6 100644 --- a/source/includes/indexes/indexes.kt +++ b/source/includes/indexes/indexes.kt @@ -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( @@ -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) + // end-create-search-index + + // start-create-search-indexes + val indexOne = SearchIndexModel("", Document("mappings", Document("dynamic", true))) + val indexTwo = SearchIndexModel("", 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("", newIndex) + // end-update-search-indexes + + // start-drop-search-index + collection.dropIndex("") + // end-drop-search-index } diff --git a/source/indexes/atlas-search-index.txt b/source/indexes/atlas-search-index.txt new file mode 100644 index 00000000..7d9cc53a --- /dev/null +++ b/source/indexes/atlas-search-index.txt @@ -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 ` 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 ` 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 ` +documentation. \ No newline at end of file diff --git a/source/work-with-indexes.txt b/source/work-with-indexes.txt index fbd0b417..b0f9da79 100644 --- a/source/work-with-indexes.txt +++ b/source/work-with-indexes.txt @@ -21,6 +21,7 @@ Work with Indexes /indexes/single-field-index.txt /indexes/compound-index.txt + /indexes/atlas-search-index.txt Overview -------- @@ -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 ---------------