|
| 1 | +.. _ruby-atlas-search: |
| 2 | + |
| 3 | +========================= |
| 4 | +Run an Atlas Search Query |
| 5 | +========================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: search, atlas, read |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to query an Atlas Search index and use advanced full-text |
| 24 | +search functionality in your {+driver-short+} applications. You can query a search index by |
| 25 | +using a ``$search`` aggregation pipeline stage. |
| 26 | + |
| 27 | +To learn more about the ``$search`` pipeline stage, see the :manual:`$search |
| 28 | +</reference/operator/aggregation/search/>` guide in the {+mdb-server+} manual. |
| 29 | + |
| 30 | +.. note:: Only Available on Atlas for MongoDB v4.2 and Later |
| 31 | + |
| 32 | + The ``$search`` aggregation-pipeline operator is available only for collections hosted |
| 33 | + on :atlas:`MongoDB Atlas </>` clusters running MongoDB v4.2 or later that are |
| 34 | + covered by an :atlas:`Atlas search index </reference/atlas-search/index-definitions/>`. |
| 35 | + To learn more about the required setup and the functionality of this operator, |
| 36 | + see the :atlas:`Atlas Search </atlas-search>` documentation. |
| 37 | + |
| 38 | +Sample Data |
| 39 | +~~~~~~~~~~~ |
| 40 | + |
| 41 | +The examples in this guide use the ``sample_mflix.movies`` collection |
| 42 | +from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 43 | +free MongoDB Atlas cluster and load the sample datasets, see |
| 44 | +:ref:`<ruby-get-started>`. |
| 45 | + |
| 46 | +Create an Atlas Search Index |
| 47 | +---------------------------- |
| 48 | + |
| 49 | +Before you can perform a search on an Atlas collection, you must first create an **Atlas |
| 50 | +Search index** on the collection. An Atlas Search index is a data structure that |
| 51 | +categorizes data in a searchable format. To learn how to create an Atlas Search index, |
| 52 | +see the :ref:`ruby-atlas-search-index` guide. |
| 53 | + |
| 54 | +Search Your Data |
| 55 | +---------------- |
| 56 | + |
| 57 | +To use the ``$search`` aggregation pipeline stage, you must specify an Atlas Search query |
| 58 | +operator that indicates the type of query you want to run. You can optionally |
| 59 | +use a collector to specify the values and ranges of the query output. To view a |
| 60 | +table of all the operators and collectors available with Atlas Search, see the |
| 61 | +:atlas:`Operators and Collectors </atlas-search/operators-and-collectors>` page |
| 62 | +in the Atlas documentation. |
| 63 | + |
| 64 | +The following example uses the ``compound`` operator to combine several operators into a |
| 65 | +single query. To learn more, see the :atlas:`Compound Operator |
| 66 | +</atlas-search/compound>` guide in the Atlas documentation. |
| 67 | + |
| 68 | +The query has the following search criteria: |
| 69 | + |
| 70 | +- The ``genres`` field must not contain ``Comedy``. |
| 71 | +- The ``title`` field must contain the string ``New York``. |
| 72 | + |
| 73 | +The query also includes the following stages: |
| 74 | + |
| 75 | +- :pipeline:`$limit`, to limit the output to 10 results. |
| 76 | +- :pipeline:`$project`, to exclude all fields except |
| 77 | + ``title`` and add a field named ``score``. |
| 78 | + |
| 79 | +.. io-code-block:: |
| 80 | + :copyable: true |
| 81 | + |
| 82 | + .. input:: |
| 83 | + :language: ruby |
| 84 | + |
| 85 | + pipeline = [ |
| 86 | + { |
| 87 | + "$search" => { |
| 88 | + "index" => "index_name", |
| 89 | + "compound" => { |
| 90 | + "mustNot" => [ |
| 91 | + { |
| 92 | + "text" => { |
| 93 | + "query" => ["Comedy"], |
| 94 | + "path" => "genres" |
| 95 | + } |
| 96 | + } |
| 97 | + ], |
| 98 | + "must" => [ |
| 99 | + { |
| 100 | + "text" => { |
| 101 | + "query" => ["New York"], |
| 102 | + "path" => "title" |
| 103 | + } |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + } |
| 108 | + }, |
| 109 | + { "$limit" => 10 }, |
| 110 | + { |
| 111 | + "$project" => { |
| 112 | + "_id" => 0, |
| 113 | + "title" => 1, |
| 114 | + "score" => { "$meta" => "searchScore" } |
| 115 | + } |
| 116 | + } |
| 117 | + ] |
| 118 | + |
| 119 | + result = collection.aggregate(pipeline) |
| 120 | + puts result.to_a |
| 121 | + |
| 122 | + .. output:: |
| 123 | + :language: none |
| 124 | + :visible: false |
| 125 | + |
| 126 | + {'title': 'New York, New York', 'score': 6.786379814147949} |
| 127 | + {'title': 'New York', 'score': 6.258603096008301} |
| 128 | + {'title': 'New York Doll', 'score': 5.381444931030273} |
| 129 | + {'title': 'Escape from New York', 'score': 4.719935417175293} |
| 130 | + {'title': 'Autumn in New York', 'score': 4.719935417175293} |
| 131 | + {'title': 'Sleepless in New York', 'score': 4.719935417175293} |
| 132 | + {'title': 'Gangs of New York', 'score': 4.719935417175293} |
| 133 | + {'title': 'Sherlock Holmes in New York', 'score': 4.203253746032715} |
| 134 | + {'title': 'New York: A Documentary Film', 'score': 4.203253746032715} |
| 135 | + {'title': 'An Englishman in New York', 'score': 4.203253746032715} |
| 136 | + |
| 137 | +Additional Information |
| 138 | +---------------------- |
| 139 | + |
| 140 | +To learn more about the available Atlas Search operators, see the :atlas:`Operators and |
| 141 | +Collectors </atlas-search/operators-and-collectors>` guide in the MongoDB Atlas |
| 142 | +documentation. |
| 143 | + |
| 144 | +For more information about Atlas Search, and to view more query examples, see the |
| 145 | +:atlas:`Atlas Search documentation </atlas-search>`. |
| 146 | + |
| 147 | +If you'd like to perform vector searches on your data stored in Atlas, you must use Atlas |
| 148 | +Vector Search. To learn more about Atlas Vector Search, see the :atlas:`Atlas Vector |
| 149 | +Search documentation </atlas-vector-search/vector-search-overview/>`. |
0 commit comments