Skip to content

Commit 246c099

Browse files
committed
DOCSP-47824: Atlas search
1 parent 52989b6 commit 246c099

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

source/atlas-search.txt

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

source/includes/AtlasSearch.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Creates and uses Atlas Search indexes 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+
// begin-atlas-search
25+
collection.aggregate(
26+
Arrays.asList(
27+
Aggregates.search(SearchOperator.text(
28+
SearchPath.fieldPath("title"), "Alabama")),
29+
Aggregates.project(Projections.include("title"))
30+
)
31+
).forEach(doc -> System.out.println(doc.toJson()));
32+
// end-atlas-search
33+
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)