|
| 1 | +.. _ruby-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. With Atlas Search indexes, you can specify the |
| 25 | +behavior of the search and which fields to index. |
| 26 | + |
| 27 | +You can call the following methods to manage you Atlas Search indexes: |
| 28 | + |
| 29 | +- ``search_indexes#create_one`` |
| 30 | +- ``search_indexes#create_many`` |
| 31 | +- ``search_indexes#update_one`` |
| 32 | +- ``search_indexes#drop_one`` |
| 33 | + |
| 34 | +The following sections provide code examples that demonstrate how to use |
| 35 | +each of the preceding commands. |
| 36 | + |
| 37 | +.. _ruby-atlas-search-index-create: |
| 38 | + |
| 39 | +Create a Search Index |
| 40 | +--------------------- |
| 41 | + |
| 42 | +To create one or more Atlas Search indexes, use the ``search_indexes#create_one`` |
| 43 | +or the ``search_indexes#create_many`` method. Both methods return immediately, |
| 44 | +while the indexes are asynchronously created in the background. |
| 45 | + |
| 46 | +The following code example shows how to create an Atlas Search index by providing |
| 47 | +an index definition and an optional name for the index: |
| 48 | + |
| 49 | +.. literalinclude:: /includes/indexes/atlas-search-index.rb |
| 50 | + :language: ruby |
| 51 | + :start-after: start-create-search-index |
| 52 | + :end-before: end-create-search-index |
| 53 | + :emphasize-lines: 15 |
| 54 | + |
| 55 | +You can use ``search_indexes#create_many`` to create multiple Atlas Search indexes by |
| 56 | +providing an array of index specifications. Each index specification should include a definition |
| 57 | +key, which defines the index, and a name key to specify the index name. The following |
| 58 | +code example shows how to create multiple search indexes: |
| 59 | + |
| 60 | +.. literalinclude:: /includes/indexes/atlas-search-index.rb |
| 61 | + :language: ruby |
| 62 | + :start-after: start-create-multiple-search-indexes |
| 63 | + :end-before: end-create-multiple-search-indexes |
| 64 | + :emphasize-lines: 25 |
| 65 | + |
| 66 | +For longer index definitions, it is helpful to define the index definitions outside |
| 67 | +of the method call. To learn more about the syntax of index definitions, see the |
| 68 | +:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>` |
| 69 | +guide in the Atlas manual. |
| 70 | + |
| 71 | +Update a Search Index |
| 72 | +--------------------- |
| 73 | + |
| 74 | +To update an Atlas Search index, use the ``search_indexes#update_one`` method. |
| 75 | + |
| 76 | +To update an index, you must provide a new index definition. You must specify |
| 77 | +the index you want to update by using either the ``name`` or ``id`` of the index. |
| 78 | +The following code shows how to update a search index: |
| 79 | + |
| 80 | +.. literalinclude:: /includes/indexes/atlas-search-index.rb |
| 81 | + :language: ruby |
| 82 | + :start-after: start-update-search-indexes |
| 83 | + :end-before: end-update-search-indexes |
| 84 | + |
| 85 | +Delete a Search Index |
| 86 | +--------------------- |
| 87 | + |
| 88 | +To delete an Atlas Search index, use the ``search_indexes#drop_one`` method. |
| 89 | + |
| 90 | +To delete an index, you must provide the ``id`` or ``name`` of the index. The following |
| 91 | +code shows how to delete a search index from a collection: |
| 92 | + |
| 93 | +.. literalinclude:: /includes/indexes/atlas-search-index.rb |
| 94 | + :language: ruby |
| 95 | + :start-after: start-drop-search-index |
| 96 | + :end-before: end-drop-search-index |
| 97 | + |
| 98 | +List Search Indexes |
| 99 | +------------------- |
| 100 | + |
| 101 | +You can use the ``search_indexes`` object to list the entire index specification |
| 102 | +of each index: |
| 103 | + |
| 104 | +.. literalinclude:: /includes/indexes/atlas-search-index.rb |
| 105 | + :language: ruby |
| 106 | + :start-after: start-list-entire-spec |
| 107 | + :end-before: end-list-entire-spec |
| 108 | + |
| 109 | +To list individual fields in the index specification for each index, iterate |
| 110 | +over the ``search_indexes`` object: |
| 111 | + |
| 112 | +.. literalinclude:: /includes/indexes/atlas-search-index.rb |
| 113 | + :language: ruby |
| 114 | + :start-after: start-list-certain-elements |
| 115 | + :end-before: end-list-certain-elements |
| 116 | + |
| 117 | +Additional Information |
| 118 | +---------------------- |
| 119 | + |
| 120 | +To learn more about MongoDB Atlas Search, see the |
| 121 | +:atlas:`Atlas Search </atlas-search/atlas-search-overview/>` documentation. |
0 commit comments