@@ -18,4 +18,90 @@ Atlas Search
18
18
:depth: 2
19
19
:class: singlecol
20
20
21
- See :atlas:`Atlas Search </atlas-search/>` in the MongoDB Atlas documentation.
21
+ Overview
22
+ --------
23
+
24
+ In this guide, you can learn how to use the {+driver-short+} to
25
+ run :atlas:`Atlas Search </atlas-search/>` queries on a collection.
26
+ Atlas Search enables you to perform full-text searches on collections
27
+ hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of the
28
+ search and which fields to index.
29
+
30
+ Sample Data
31
+ ~~~~~~~~~~~
32
+
33
+ The example in this guide uses the ``movies`` collection in the ``sample_mflix``
34
+ database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to
35
+ create a free MongoDB Atlas cluster and load the sample datasets, see the
36
+ :atlas:`Get Started with Atlas </getting-started>` guide.
37
+
38
+ Run an Atlas Search Query
39
+ -------------------------
40
+
41
+ This section shows how to create an aggregation pipeline to run an
42
+ Atlas Search query on a collection. You can use the ``Aggregates.search()`` builder
43
+ method to create a ``$search`` pipeline stage, which specifies the search
44
+ criteria. Then, call the ``aggregate()`` method and pass your pipeline as a parameter.
45
+
46
+ .. tip::
47
+
48
+ To learn more about aggregation operations and builders, see the :ref:`java-aggregation`
49
+ guide.
50
+
51
+ Before running an Atlas Search query, you must create an Atlas Search index
52
+ on your collection. To learn how to programmatically create an Atlas Search
53
+ index, see the :ref:`java-search-indexes` section in the Indexes guide.
54
+
55
+ Atlas Search Example
56
+ ~~~~~~~~~~~~~~~~~~~~
57
+
58
+ This example runs an Atlas Search query by performing the
59
+ following actions:
60
+
61
+ - Constructs a ``$search`` stage by using the ``Aggregates.search()`` builder method,
62
+ instructing the driver to query for documents in which the ``title``
63
+ field contains the word ``"Alabama"``
64
+
65
+ - Constructs a ``$project`` stage by using the ``Aggregates.project()`` builder method,
66
+ instructing the driver to include the ``title`` field in the query results
67
+
68
+ - Passes the pipeline stages to the ``aggregate()`` method and prints the results
69
+
70
+ .. io-code-block::
71
+ :copyable:
72
+
73
+ .. input:: /includes/AtlasSearch.java
74
+ :start-after: begin-atlas-search
75
+ :end-before: end-atlas-search
76
+ :language: java
77
+ :dedent:
78
+
79
+ .. output::
80
+ :language: console
81
+ :visible: false
82
+
83
+ {"_id": {"$oid": "..."}, "title": "Alabama Moon"}
84
+ {"_id": {"$oid": "..."}, "title": "Crazy in Alabama"}
85
+ {"_id": {"$oid": "..."}, "title": "Sweet Home Alabama"}
86
+
87
+ .. tip:: Java Driver Atlas Search Examples
88
+
89
+ To view more examples that use the {+driver-short+} to perform Atlas
90
+ Search queries, see :atlas:`Atlas Search Tutorials </atlas-search/tutorials/>`
91
+ in the Atlas documentation.
92
+
93
+ Additional Information
94
+ ----------------------
95
+
96
+ To learn more about Atlas Search, see :atlas:`Atlas Search </atlas-search/>`
97
+ in the Atlas documentation.
98
+
99
+ API Documentation
100
+ ~~~~~~~~~~~~~~~~~
101
+
102
+ To learn more about the methods mentioned in this guide, see
103
+ the following API documentation:
104
+
105
+ - `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
106
+ - `Aggregates.search() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchCollector)>`__
107
+ - `Aggregates.project() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__
0 commit comments