|
| 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 search functionality for your applications. To query the seach index, use a ``$search`` aggregation pipeline stage with {+driver-short+}. |
| 24 | + |
| 25 | +To learn more about the ``$search`` pipeline stage, see :manual:`$search |
| 26 | +</reference/operator/aggregation/search/>`. |
| 27 | + |
| 28 | +.. note:: Only Available on Atlas for MongoDB v4.2 and Later |
| 29 | + The ``$search`` aggregation-pipeline operator is available only for collections hosted |
| 30 | + on :atlas:`MongoDB Atlas </>` clusters running MongoDB v4.2 or later that are |
| 31 | + covered by an :atlas:`Atlas search index </reference/atlas-search/index-definitions/>`. |
| 32 | + To learn more about the required setup and the functionality of this operator, |
| 33 | + see the :ref:`Atlas Search <fts-top-ref>` documentation. |
| 34 | + |
| 35 | +Sample Data |
| 36 | +~~~~~~~~~~~ |
| 37 | + |
| 38 | +The examples in this guide use the ``sample_mflix.movies`` collection |
| 39 | +from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 40 | +free MongoDB Atlas cluster and load the sample datasets, see |
| 41 | +:ref:`<pymongo-get-started>`. |
| 42 | + |
| 43 | +Create an Atlas Search Index |
| 44 | +---------------------------- |
| 45 | + |
| 46 | +Before you can perform a search on an Atlas collection, you must first create an **Atlas |
| 47 | +Search index** on the collection. An Atlas Search index is a data structure that |
| 48 | +categorizes data in a searchable format. To learn how to create an Atlas Search Index |
| 49 | +see the :ref:`pymongo-atlas-search-index` documentation. |
| 50 | + |
| 51 | +Search Your Data |
| 52 | +---------------- |
| 53 | + |
| 54 | +To use the ``$search`` aggregation pipeline stage, you must select an Atlas Search query operator that specifies the type of query you run. You can also optionally select a collector that groups results by values or ranges. To see a table of all the operators and collectors available in Atlas Search, see :atlas:`Use Operators and Collectors in Atlas Search Queries </atlas-search/operators-and-collectors>`. |
| 55 | + |
| 56 | +The following example uses the ``phrase`` operator, which performs search for documents containing an ordered sequence of terms. To learn more about the ``phrase`` operator, see :atlas:`Phrase </atlas-search/phrase>` in the Atlas guide. |
| 57 | + |
| 58 | +The example performs a basic search of the ``title`` field for the query string ``new york``. There is no |
| 59 | +The query also includes a: |
| 60 | + |
| 61 | +- :pipeline:`$limit` stage to limit the output to 10 results. |
| 62 | +- :pipeline:`$project` stage to exclude all fields except |
| 63 | + ``title`` and add a field named ``score``. |
| 64 | + |
| 65 | +.. code-block:: python |
| 66 | + :copyable: true |
| 67 | + client = pymongo.MongoClient('<connection-string>') |
| 68 | + result = client['sample_mflix']['movies'].aggregate([ |
| 69 | + { |
| 70 | + "$search": { |
| 71 | + "phrase": { |
| 72 | + "path": "title", |
| 73 | + "query": "new york" |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | + { $limit: 10 }, |
| 78 | + { |
| 79 | + $project: { |
| 80 | + "_id": 0, |
| 81 | + "title": 1, |
| 82 | + score: { $meta: "searchScore" } |
| 83 | + } |
| 84 | + } |
| 85 | + ]) |
| 86 | + for i in result: |
| 87 | + print(i) |
| 88 | + |
| 89 | +.. output:: |
| 90 | + :language: shell |
| 91 | + :linenos: |
| 92 | + :visible: false |
| 93 | + |
| 94 | + [ |
| 95 | + { title: 'New York, New York', score: 6.786321640014648 } |
| 96 | + { title: 'New York', score: 6.258549213409424 } |
| 97 | + { title: 'New York Stories', score: 5.3813982009887695 } |
| 98 | + { title: 'New York Minute', score: 5.3813982009887695 } |
| 99 | + { title: 'Synecdoche, New York', score: 5.3813982009887695 } |
| 100 | + { title: 'New York Doll', score: 5.3813982009887695 } |
| 101 | + { title: 'Little New York', score: 5.3813982009887695 } |
| 102 | + { title: 'Escape from New York', score: 4.719893455505371 } |
| 103 | + { title: 'Naked in New York', score: 4.719893455505371 } |
| 104 | + { title: 'Autumn in New York', score: 4.719893455505371 } |
| 105 | + ] |
| 106 | + |
| 107 | +Next Steps |
| 108 | +---------- |
| 109 | + |
| 110 | +Now that you've run a query using Atlas Search, review the Atlas Search :atlas:`documentation |
| 111 | +</atlas-search>` to learn more about the different :atlas:`operators |
| 112 | +</atlas-search/operators-and-collectors>` and other queries you can run. More query examples using |
| 113 | +MongoDB Query Language (MQL) are available througout the Atlas :atlas:`documentation |
| 114 | +</atlas-search>`. |
0 commit comments