|
| 1 | +.. _rust-atlas-search-indexes: |
| 2 | + |
| 3 | +==================== |
| 4 | +Atlas Search Indexes |
| 5 | +==================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, full text, atlas deployment |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to create and manage **Atlas Search indexes**. These indexes |
| 24 | +allow you to use MongoDB's Atlas Search feature to perform fast, full-text searches |
| 25 | +on data stored in an Atlas cluster. An Atlas Search index configures the behavior of Atlas |
| 26 | +Search by specifying which fields to index, how these fields are indexed, and other optional |
| 27 | +settings. To learn more about Atlas Search indexes, see the :atlas:`Atlas Search documentation |
| 28 | +</atlas-search/>`. |
| 29 | + |
| 30 | +This guide explains how to perform the following actions to manage your Atlas Search indexes: |
| 31 | + |
| 32 | +- :ref:`rust-create-model` |
| 33 | +- :ref:`rust-create-search-index` |
| 34 | +- :ref:`rust-create-search-indexes` |
| 35 | +- :ref:`rust-list-search-index` |
| 36 | +- :ref:`rust-update-search-index` |
| 37 | +- :ref:`rust-drop-search-index` |
| 38 | + |
| 39 | +.. note:: |
| 40 | + |
| 41 | + The examples in this guide access the ``posts`` collection in the ``sample_training`` |
| 42 | + database, which is one of the Atlas sample datasets. For instructions on importing |
| 43 | + the Atlas sample data, see :atlas:`Load Sample Data </sample-data>` in the Atlas |
| 44 | + documentation. |
| 45 | + |
| 46 | +.. _rust-create-model: |
| 47 | + |
| 48 | +Create a Search Index Model |
| 49 | +--------------------------- |
| 50 | + |
| 51 | +To create an Atlas Search index, you must first build a ``SearchIndexModel`` instance |
| 52 | +that sets your index specifications. To begin building a ``SearchIndexModel`` instance, |
| 53 | +call the ``SearchIndexModel::builder()`` method. |
| 54 | + |
| 55 | +.. note:: Instantiating Models |
| 56 | + |
| 57 | + The {+driver-short+} implements the Builder design pattern for the |
| 58 | + creation of many different types, including ``SearchIndexModel``. You |
| 59 | + can use the ``builder()`` method to construct an instance of each type |
| 60 | + by chaining option builder methods. |
| 61 | + |
| 62 | +The {+driver-short+} provides the following ``SearchIndexModel`` builder methods: |
| 63 | + |
| 64 | +- ``definition()``, which accepts a BSON document parameter and sets your index definition |
| 65 | +- ``name()``, which accepts a string parameter and sets your index name |
| 66 | + |
| 67 | +The BSON document that you pass to the ``definition()`` builder method must include |
| 68 | +the ``mappings`` field. To automatically index all supported fields in your collection, |
| 69 | +enable dynamic mappings by setting the ``mappings.dynamic`` nested field to ``true``. To |
| 70 | +index only specified fields, enable static mappings by setting the ``mappings.dynamic`` |
| 71 | +nested field to ``false`` and including a list of fields you want to index. |
| 72 | + |
| 73 | +.. tip:: Atlas Search Field Mappings |
| 74 | + |
| 75 | + To learn more about Atlas Search field mappings, see :atlas:`Define Field Mappings |
| 76 | + </atlas-search/define-field-mappings/>` in the Atlas documentation. |
| 77 | + |
| 78 | +Example |
| 79 | +~~~~~~~ |
| 80 | + |
| 81 | +The following example creates specifications for a index named ``example_index`` in a |
| 82 | +``SearchIndexModel`` instance. The code sets a static mapping to index only the ``body`` |
| 83 | +and ``date`` fields: |
| 84 | + |
| 85 | +.. literalinclude:: /includes/fundamentals/code-snippets/indexes.rs |
| 86 | + :start-after: begin-atlas-model |
| 87 | + :end-before: end-atlas-model |
| 88 | + :language: rust |
| 89 | + :dedent: |
| 90 | + |
| 91 | +.. _rust-create-search-index: |
| 92 | + |
| 93 | +Create a Search Index |
| 94 | +--------------------- |
| 95 | + |
| 96 | +You can create an Atlas Search index on a collection by calling the ``create_search_index()`` |
| 97 | +method on a ``Collection`` instance. This method accepts the following parameters: |
| 98 | + |
| 99 | +- Index model, specified in a ``SearchIndexModel`` instance |
| 100 | +- Index options, specified in a `CreateSearchIndexOptions |
| 101 | + <{+api+}/options/struct.CreateSearchIndexOptions.html>`__ instance |
| 102 | + |
| 103 | +Example |
| 104 | +~~~~~~~ |
| 105 | + |
| 106 | +The following example creates an Atlas Search index on the ``posts`` collection. |
| 107 | +The code creates a ``SearchIndexModel`` that sets the index name and enables dynamic |
| 108 | +mapping. Then, the code passes the ``SearchIndexModel`` instance to the ``create_search_index()`` |
| 109 | +method to create the Atlas Search index: |
| 110 | + |
| 111 | +.. io-code-block:: |
| 112 | + :copyable: true |
| 113 | + |
| 114 | + .. input:: /includes/fundamentals/code-snippets/indexes.rs |
| 115 | + :start-after: begin-atlas-create-one |
| 116 | + :end-before: end-atlas-create-one |
| 117 | + :language: rust |
| 118 | + :dedent: |
| 119 | + |
| 120 | + .. output:: |
| 121 | + :language: console |
| 122 | + :visible: false |
| 123 | + |
| 124 | + Created Atlas Search index: |
| 125 | + "example_index" |
| 126 | + |
| 127 | +.. _rust-create-search-indexes: |
| 128 | + |
| 129 | +Create Multiple Search Indexes |
| 130 | +------------------------------ |
| 131 | + |
| 132 | +You can create multiple Atlas Search indexes at once by calling the ``create_search_indexes()`` |
| 133 | +method on a ``Collection`` instance. This method accepts the following parameters: |
| 134 | + |
| 135 | +- List of index models, specified as a vector of ``SearchIndexModel`` instances |
| 136 | +- Index options, specified in a ``CreateSearchIndexOptions`` instance |
| 137 | + |
| 138 | +Example |
| 139 | +~~~~~~~ |
| 140 | + |
| 141 | +The following example creates two Atlas Search indexes named ``dynamic_index`` and |
| 142 | +``static_index`` on the ``posts`` collection. The code creates ``SearchIndexModel`` |
| 143 | +instances for each index that specify the index names and definitions. Then, the code |
| 144 | +passes these models as a vector to the ``create_search_indexes()`` method and creates |
| 145 | +the indexes: |
| 146 | + |
| 147 | +.. io-code-block:: |
| 148 | + :copyable: true |
| 149 | + |
| 150 | + .. input:: /includes/fundamentals/code-snippets/indexes.rs |
| 151 | + :start-after: begin-atlas-create-many |
| 152 | + :end-before: end-atlas-create-many |
| 153 | + :language: rust |
| 154 | + :dedent: |
| 155 | + |
| 156 | + .. output:: |
| 157 | + :language: console |
| 158 | + :visible: false |
| 159 | + |
| 160 | + Created Atlas Search indexes: |
| 161 | + ["dynamic_index", "static_index"] |
| 162 | + |
| 163 | +.. _rust-list-search-index: |
| 164 | + |
| 165 | +List Search Indexes |
| 166 | +------------------- |
| 167 | + |
| 168 | +You can access information about a collection's existing Atlas Search indexes |
| 169 | +by calling the ``list_search_indexes()`` method on the collection. This |
| 170 | +method accepts the following parameters: |
| 171 | + |
| 172 | +- Name of the index to retrieve information about |
| 173 | +- Aggregation options, specified in an `AggregateOptions |
| 174 | + <{+api+}/options/struct.AggregateOptions.html>`__ instance |
| 175 | +- Index options, specified in a `ListSearchIndexOptions |
| 176 | + <{+api+}/options/struct.ListSearchIndexOptions.html>`__ instance |
| 177 | + |
| 178 | +Example |
| 179 | +~~~~~~~ |
| 180 | + |
| 181 | +The following example accesses information about the Atlas Search indexes created |
| 182 | +in the :ref:`rust-create-search-indexes` section of this page. The code calls the |
| 183 | +``list_search_indexes()`` method and passes a value of ``None`` for each parameter, |
| 184 | +which instructs the driver to return information about all Atlas Search indexes with |
| 185 | +default options. Then, the code outputs the search indexes: |
| 186 | + |
| 187 | +.. io-code-block:: |
| 188 | + :copyable: true |
| 189 | + |
| 190 | + .. input:: /includes/fundamentals/code-snippets/indexes.rs |
| 191 | + :start-after: begin-atlas-list |
| 192 | + :end-before: end-atlas-list |
| 193 | + :language: rust |
| 194 | + :dedent: |
| 195 | + |
| 196 | + .. output:: |
| 197 | + :language: console |
| 198 | + :visible: false |
| 199 | + |
| 200 | + { "id": "...", "name": "dynamic_index", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, |
| 201 | + "latestDefinition": { "mappings": { "dynamic": true } }, "statusDetail": [...] } |
| 202 | + |
| 203 | + { "id": "...", "name": "static_index", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, |
| 204 | + "latestDefinition": { "mappings": { "dynamic": false, "fields": { "title": { "type": "string" } } } }, |
| 205 | + "statusDetail": [...] } |
| 206 | + |
| 207 | +.. tip:: |
| 208 | + |
| 209 | + To learn more about iterating through a cursor, see the :ref:`rust-cursor-guide` guide. |
| 210 | + |
| 211 | +.. _rust-update-search-index: |
| 212 | + |
| 213 | +Update a Search Index |
| 214 | +--------------------- |
| 215 | + |
| 216 | +You can update an Atlas Search index by calling the ``update_search_index()`` |
| 217 | +method on a ``Collection`` instance. This method accepts the following parameters: |
| 218 | + |
| 219 | +- Name of the index to update |
| 220 | +- Modified index definition document |
| 221 | +- Index options, specified in an `UpdateSearchIndexOptions |
| 222 | + <{+api+}/options/struct.UpdateSearchIndexOptions.html>`__ instance |
| 223 | + |
| 224 | +Example |
| 225 | +~~~~~~~ |
| 226 | + |
| 227 | +The following example updates the Atlas Search index named ``static_index`` |
| 228 | +created in the :ref:`rust-create-search-indexes` section of this page. The code |
| 229 | +creates a new index definition document that instructs the index to use dynamic |
| 230 | +mappings instead of static mappings. Then, the code calls the ``update_search_index()`` |
| 231 | +method to update the index: |
| 232 | + |
| 233 | +.. literalinclude:: /includes/fundamentals/code-snippets/indexes.rs |
| 234 | + :start-after: begin-atlas-update |
| 235 | + :end-before: end-atlas-update |
| 236 | + :language: rust |
| 237 | + :dedent: |
| 238 | + |
| 239 | +.. _rust-drop-search-index: |
| 240 | + |
| 241 | +Delete a Search Index |
| 242 | +--------------------- |
| 243 | + |
| 244 | +You can delete an Atlas Search index by calling the ``delete_search_index()`` |
| 245 | +method on a ``Collection`` instance. This method accepts the following parameters: |
| 246 | + |
| 247 | +- Name of the index to delete |
| 248 | +- Index options, specified in a `DropSearchIndexOptions |
| 249 | + <{+api+}/options/struct.DropSearchIndexOptions.html>`__ instance |
| 250 | + |
| 251 | +Example |
| 252 | +~~~~~~~ |
| 253 | + |
| 254 | +The following example deletes the Atlas Search index named ``example_index`` |
| 255 | +created in the :ref:`rust-create-search-index` section of this page. The code |
| 256 | +passes the index name to the ``delete_search_index()`` method to delete the index: |
| 257 | + |
| 258 | +.. literalinclude:: /includes/fundamentals/code-snippets/indexes.rs |
| 259 | + :start-after: begin-atlas-drop |
| 260 | + :end-before: end-atlas-drop |
| 261 | + :language: rust |
| 262 | + :dedent: |
| 263 | + |
| 264 | +Additional Information |
| 265 | +---------------------- |
| 266 | + |
| 267 | +To learn about other indexes you can create by using the {+driver-short+}, see the |
| 268 | +:ref:`rust-indexes` guide. |
| 269 | + |
| 270 | +To learn more about Atlas Search, see the following Atlas documentation: |
| 271 | + |
| 272 | +- :atlas:`Atlas Search Best Practices </atlas-search/best-practices/>` |
| 273 | +- :atlas:`Tune Atlas Search Performance </atlas-search/performance/>` |
| 274 | +- :atlas:`Atlas Search M0, M2, and M5 Limitations </atlas-search/limitations/>` |
| 275 | +- :atlas:`FAQ: Atlas Search </atlas-search/faq/>` |
| 276 | + |
| 277 | +API Documentation |
| 278 | +~~~~~~~~~~~~~~~~~ |
| 279 | + |
| 280 | +To learn more about the methods and types mentioned in this |
| 281 | +guide, see the following API documentation: |
| 282 | + |
| 283 | +- `create_search_index() <{+api+}/struct.Collection.html#method.create_search_index>`__ |
| 284 | +- `SearchIndexModel <{+api+}/struct.SearchIndexModel.html>`__ |
| 285 | +- `create_search_indexes() <{+api+}/struct.Collection.html#method.create_search_indexes>`__ |
| 286 | +- `list_search_indexes() <{+api+}/struct.Collection.html#method.list_search_indexes>`__ |
| 287 | +- `update_search_index() <{+api+}/struct.Collection.html#method.create_search_index>`__ |
| 288 | +- `drop_search_index() <{+api+}/struct.Collection.html#method.create_search_index>`__ |
0 commit comments