Skip to content

Commit f56ba21

Browse files
authored
DOCSP-47824: Atlas search (#625)
* DOCSP-47824: Atlas search * fixes * reword * SA feedback * typo * tech review
1 parent e05bf7b commit f56ba21

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

source/atlas-search.txt

+87-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,90 @@ Atlas Search
1818
:depth: 2
1919
:class: singlecol
2020

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)>`__

source/includes/AtlasSearch.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Runs an Atlas Search query by using the Java driver
2+
3+
package org.example;
4+
5+
import com.mongodb.client.MongoClient;
6+
import com.mongodb.client.MongoClients;
7+
import com.mongodb.client.MongoCollection;
8+
import com.mongodb.client.MongoDatabase;
9+
import com.mongodb.client.model.Aggregates;
10+
import com.mongodb.client.model.Projections;
11+
import com.mongodb.client.model.search.SearchOperator;
12+
import com.mongodb.client.model.search.SearchPath;
13+
import org.bson.Document;
14+
import java.util.Arrays;
15+
16+
public class AtlasSearch {
17+
public static void main(String[] args) {
18+
String uri = "<connection string>";
19+
20+
try (MongoClient mongoClient = MongoClients.create(uri)) {
21+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
22+
MongoCollection<Document> collection = database.getCollection("movies");
23+
24+
// Queries for documents that have a "title" value containing the word "Alabama"
25+
// begin-atlas-search
26+
collection.aggregate(
27+
Arrays.asList(
28+
Aggregates.search(SearchOperator.text(
29+
SearchPath.fieldPath("title"), "Alabama")),
30+
Aggregates.project(Projections.include("title"))
31+
)
32+
).forEach(doc -> System.out.println(doc.toJson()));
33+
// end-atlas-search
34+
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)