|
| 1 | +******************** |
| 2 | +Atlas Search Indexes |
| 3 | +******************** |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +If you are using a database hosted by MongoDB Atlas, the driver provides the |
| 14 | +ability to create, drop and view `Atlas search indexes <https://www.mongodb.com/docs/atlas/atlas-search/>`_ |
| 15 | +on a collection through the ``search_indexes`` attribute: |
| 16 | + |
| 17 | +.. code-block:: ruby |
| 18 | + |
| 19 | + client = Mongo::Client.new(your_atlas_uri, database: 'music') |
| 20 | + client[:bands].search_indexes |
| 21 | + # => #<Mongo::SearchIndex::View:0x000055e2822b9318 @collection=#<Mongo::Collection:0x660 namespace=music.bands> ...> |
| 22 | + |
| 23 | + |
| 24 | +Creating Search Indexes |
| 25 | +======================= |
| 26 | + |
| 27 | +Search indexes can be created one at a time, or several can be created in |
| 28 | +parallel in a single operation. |
| 29 | + |
| 30 | +To create a single index, use ``search_indexes#create_one``, passing the index |
| 31 | +definition as the first argument, and an optional name for the index as the |
| 32 | +second argument. |
| 33 | + |
| 34 | +.. code-block:: ruby |
| 35 | + |
| 36 | + client[:bands].search_indexes.create_one({ dynamic: true }) |
| 37 | + |
| 38 | + client[:bands].search_indexes.create_one( |
| 39 | + { |
| 40 | + dynamic: false, |
| 41 | + fields: { |
| 42 | + name: { type: 'string', analyzer: 'lucene.simple' } |
| 43 | + } |
| 44 | + }, |
| 45 | + 'band-name-index' |
| 46 | + ) |
| 47 | + |
| 48 | +To create multiple indexes, use ``search_indexes#create_many`` which accepts |
| 49 | +an array of index specifications. Unlike ``create_one``, each index |
| 50 | +specification is a hash with at least a ``definition`` key, which |
| 51 | +defines the index. Each has may also specify a ``name`` key, to name |
| 52 | +the index. |
| 53 | + |
| 54 | +.. code-block:: ruby |
| 55 | + |
| 56 | + client[:bands].search_indexes.create_many([ |
| 57 | + { definition: { dynamic: true } }, |
| 58 | + { name: 'band-name-index, |
| 59 | + definition: { |
| 60 | + dynamic: false, |
| 61 | + fields: { |
| 62 | + name: { type: 'string', analyzer: 'lucene.simple' } |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + ]) |
| 67 | + |
| 68 | +Note that whether you call ``create_one`` or ``create_many``, the |
| 69 | +method will return immediately, before the indexes are created. The |
| 70 | +indexes are then created in the background, asynchronously. |
| 71 | + |
| 72 | + |
| 73 | +Update Search Indexes |
| 74 | +===================== |
| 75 | + |
| 76 | +You can programmatically update an Atlas search index. For example, you |
| 77 | +might do this to change the analyzer used, or to provide an explicit field |
| 78 | +mapping, instead of a dynamic one. To do this, use the ``search_indexes#update_one`` |
| 79 | +method: |
| 80 | + |
| 81 | +.. code-block:: ruby |
| 82 | + |
| 83 | + client[:bands].search_indexes.update_one(new_definition, id: index_id) |
| 84 | + |
| 85 | + client[:bands].search_indexes.update_one(new_definition, name: index_name) |
| 86 | + |
| 87 | +Indexes may be identified by either id, or name, but you must specify one |
| 88 | +or the other. The new index definition must be a complete definition--it will |
| 89 | +take precedence as specified over the existing definition. |
| 90 | + |
| 91 | +To get the id or name of an index that you wish to update, you can |
| 92 | +`list the search indexes <#listing-search-indexes>`_. |
| 93 | + |
| 94 | + |
| 95 | +Dropping Search Indexes |
| 96 | +======================= |
| 97 | + |
| 98 | +To drop Atlas search indexes, call ``search_indexes#drop_one`` and |
| 99 | +provide either the ``id`` or the ``name`` of the index you wish to |
| 100 | +drop. |
| 101 | + |
| 102 | +.. code-block:: ruby |
| 103 | + |
| 104 | + client[:bands].search_indexes.drop_one(id: index_id) |
| 105 | + |
| 106 | + client[:bands].search_indexes.drop_one(name: index_name) |
| 107 | + |
| 108 | +In either case, the method will return immediately and the index will |
| 109 | +be dropped in the background, asynchronously. |
| 110 | + |
| 111 | +To get the id or name of an index that you wish to drop, you can |
| 112 | +`list the search indexes <#listing-search-indexes>`_. |
| 113 | + |
| 114 | + |
| 115 | +Listing Search Indexes |
| 116 | +====================== |
| 117 | + |
| 118 | +To list the available search indexes, iterate over the |
| 119 | +``search_indexes`` object: |
| 120 | + |
| 121 | +.. code-block:: ruby |
| 122 | + |
| 123 | + client[:bands].search_indexes.each do |index_spec| |
| 124 | + p index_spec['id'] |
| 125 | + p index_spec['name'] |
| 126 | + p index_spec['status'] |
| 127 | + p index_spec['queryable'] |
| 128 | + p index_spec['latestDefinition'] |
| 129 | + end |
0 commit comments