|
| 1 | +.. _pymongo-atlas-search: |
| 2 | + |
| 3 | +============ |
| 4 | +Atlas Search |
| 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 :ref:`Atlas Search <fts-top-ref>` 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:`<pymongo-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 :ref:`pymongo-atlas-search-index`. |
| 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 also optionally specify |
| 59 | +a collector that groups results by values or ranges. To view a table of all the operators |
| 60 | +and collectors available with Atlas Search, see :atlas:`Use Operators and Collectors in |
| 61 | +Atlas Search Queries </atlas-search/operators-and-collectors>`. |
| 62 | + |
| 63 | +The following example uses the ``compound`` operator to combine several operators into a |
| 64 | +single query. To learn more about the ``compound`` operator, see the :atlas:`Compound |
| 65 | +</atlas-search/compound>` operator guide in the MongoDB Atlas documentation. |
| 66 | + |
| 67 | +The query has the following search criteria: |
| 68 | + |
| 69 | +- The ``genres`` field must not contain ``Comedy``. |
| 70 | +- The ``title`` field must contain the string ``New York``. |
| 71 | + |
| 72 | +The query also includes the following stages: |
| 73 | + |
| 74 | +- :pipeline:`$limit`, to limit the output to 10 results. |
| 75 | +- :pipeline:`$project`, to exclude all fields except |
| 76 | + ``title`` and add a field named ``score``. |
| 77 | + |
| 78 | +.. io-code-block:: |
| 79 | + :copyable: true |
| 80 | + |
| 81 | + .. input:: |
| 82 | + :language: python |
| 83 | + |
| 84 | + client = pymongo.MongoClient("<connection-string>") |
| 85 | + result = client["sample_mflix"]["movies"].aggregate([ |
| 86 | + { |
| 87 | + "$search": { |
| 88 | + "index": "pymongoindex", |
| 89 | + "compound": { |
| 90 | + "mustNot": [ |
| 91 | + { |
| 92 | + "text": { |
| 93 | + "query": [ |
| 94 | + "Comedy" |
| 95 | + ], |
| 96 | + "path": "genres" |
| 97 | + } |
| 98 | + } |
| 99 | + ], |
| 100 | + "must": [ |
| 101 | + { |
| 102 | + "text": { |
| 103 | + "query": [ |
| 104 | + "New York" |
| 105 | + ], |
| 106 | + "path": "title" |
| 107 | + } |
| 108 | + } |
| 109 | + ], |
| 110 | + } |
| 111 | + } |
| 112 | + }, |
| 113 | + { "$limit": 10 }, |
| 114 | + { |
| 115 | + "$project": { |
| 116 | + "_id": 0, |
| 117 | + "title": 1, |
| 118 | + "score": { "$meta": "searchScore" } |
| 119 | + } |
| 120 | + } |
| 121 | + ]) |
| 122 | + |
| 123 | + for i in result: |
| 124 | + print(i) |
| 125 | + |
| 126 | + .. output:: |
| 127 | + :language: none |
| 128 | + :visible: false |
| 129 | + |
| 130 | + {'title': 'New York, New York', 'score': 6.786379814147949} |
| 131 | + {'title': 'New York', 'score': 6.258603096008301} |
| 132 | + {'title': 'New York Doll', 'score': 5.381444931030273} |
| 133 | + {'title': 'Escape from New York', 'score': 4.719935417175293} |
| 134 | + {'title': 'Autumn in New York', 'score': 4.719935417175293} |
| 135 | + {'title': 'Sleepless in New York', 'score': 4.719935417175293} |
| 136 | + {'title': 'Gangs of New York', 'score': 4.719935417175293} |
| 137 | + {'title': 'Sherlock Holmes in New York', 'score': 4.203253746032715} |
| 138 | + {'title': 'New York: A Documentary Film', 'score': 4.203253746032715} |
| 139 | + {'title': 'An Englishman in New York', 'score': 4.203253746032715} |
| 140 | + |
| 141 | +Additional Information |
| 142 | +---------------------- |
| 143 | + |
| 144 | +To learn more about the available Atlas Search operators, see the :atlas:`Operators and |
| 145 | +Collectors </atlas-search/operators-and-collectors>` guide in the MongoDB Atlas |
| 146 | +documentation. |
| 147 | + |
| 148 | +For more information about Atlas Search, and to view more query examples, see the |
| 149 | +:atlas:`Atlas Search documentation </atlas-search>`. |
| 150 | + |
| 151 | +If you'd like to perform vector searches on your data stored in Atlas, you must use Atlas |
| 152 | +Vector Search. To learn more about Atlas Vector Search, see the :atlas:`Atlas Vector |
| 153 | +Search documentation </atlas-vector-search/vector-search-overview/>`. |
0 commit comments