Skip to content

Commit 099827a

Browse files
[Java RS] Add additional SearchOperator helper methods for the rest of the Atlas Search operators (#121)
Co-authored-by: rustagir <[email protected]>
1 parent 3188345 commit 099827a

File tree

5 files changed

+137
-3
lines changed

5 files changed

+137
-3
lines changed

Diff for: .DS_Store

6 KB
Binary file not shown.

Diff for: source/aggregation.txt

+78
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,81 @@ To ``$explain`` an aggregation pipeline, call the
124124
Aggregates.group("$stars", Accumulators.sum("count", 1))))
125125
.explain()
126126
.subscribe(new PrintDocumentSubscriber());
127+
128+
.. _java-rs-atlas-search-stage:
129+
130+
Atlas Search
131+
------------
132+
133+
You can perform an :atlas:`Atlas Search </atlas-search>` query by creating and running
134+
an aggregation pipeline that contains one of the following pipeline stages:
135+
136+
- ``$search``
137+
- ``$searchMeta``
138+
139+
The {+driver-short+} provides the `Aggregates.search()
140+
<{+core-api+}/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchOperator)>`__
141+
and `Aggregates.searchMeta()
142+
<{+core-api+}/client/model/Aggregates.html#searchMeta(com.mongodb.client.model.search.SearchOperator)>`__
143+
methods to perform Atlas Search queries.
144+
145+
To learn more about Atlas Search pipeline stages, see :atlas:`Choose the
146+
Aggregation Pipeline Stage </atlas-search/query-syntax/>` in the Atlas
147+
documentation.
148+
149+
Create Pipeline Search Stages
150+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151+
152+
You can create the search criteria in your Atlas Search pipeline stage
153+
by using Search operators.
154+
155+
.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst
156+
157+
.. replacement:: as-idx-link
158+
159+
the :ref:`java-rs-atlas-search-idx-mgmt` section of the Indexes guide
160+
161+
.. replacement:: atlas-query-operators-example
162+
163+
.. io-code-block::
164+
165+
.. input:: /includes/aggregation/atlas-search-examples.java
166+
:language: java
167+
:start-after: // start atlasHelperMethods
168+
:end-before: // end atlasHelperMethods
169+
:dedent:
170+
171+
.. output::
172+
:language: console
173+
:visible: false
174+
175+
{"_id": ..., "genres": ["Comedy", "Romance"], "title": "Love at First Bite", "year": 1979}
176+
{"_id": ..., "genres": ["Comedy", "Drama"], "title": "Love Affair", "year": 1994}
177+
178+
Additional Information
179+
----------------------
180+
181+
To view a full list of expression operators, see :manual:`Aggregation
182+
Operators </reference/operator/aggregation/>` in the {+mdb-server+} manual.
183+
184+
To learn about assembling an aggregation pipeline and view examples, see
185+
:manual:`Aggregation Pipeline </core/aggregation-pipeline/>` in the {+mdb-server+} manual.
186+
187+
To learn more about creating pipeline stages, see :manual:`Aggregation
188+
Stages </reference/operator/aggregation-pipeline/>` in the {+mdb-server+} manual.
189+
190+
To learn more about explaining MongoDB operations, see
191+
:manual:`Explain Output </reference/explain-results/>` and
192+
:manual:`Query Plans </core/query-plans/>` in the {+mdb-server+} manual.
193+
194+
API Documentation
195+
~~~~~~~~~~~~~~~~~
196+
197+
To learn more about the classes and methods mentioned in this guide, see
198+
the following API documentation:
199+
200+
- `aggregate() <{+driver-api+}/MongoCollection.html#aggregate(java.util.List)>`__
201+
- `Aggregates <{+core-api+}/client/model/Aggregates.html>`__
202+
- `AggregatePublisher <{+driver-api+}/AggregatePublisher.html>`__
203+
- `search() <{+core-api+}/client/model/Aggregates#search(com.mongodb.client.model.search.SearchOperator)>`__
204+
- `project() <{+core-api+}/client/model/Aggregates#project(org.bson.conversions.Bson)>`__
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.example;
2+
3+
import com.mongodb.*;
4+
import com.mongodb.client.model.Projections;
5+
import com.mongodb.reactivestreams.client.*;
6+
import org.bson.Document;
7+
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.List;
11+
12+
import org.bson.conversions.Bson;
13+
14+
import com.mongodb.client.model.Aggregates;
15+
import com.mongodb.client.model.search.SearchOperator;
16+
17+
import static com.mongodb.client.model.search.SearchPath.fieldPath;
18+
19+
import org.reactivestreams.Publisher;
20+
21+
public class SearchHelpers {
22+
public static void main(String[] args) {
23+
// Replace the placeholder with your Atlas connection string
24+
String uri = "<connection string>";
25+
26+
// Create a new client and connect to the server
27+
try (MongoClient mongoClient = MongoClients.create(uri)) {
28+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
29+
MongoCollection<Document> movies = database.getCollection("movies");
30+
31+
// start atlasHelperMethods
32+
Bson searchStageFilters = Aggregates.search(
33+
SearchOperator.compound()
34+
.filter(
35+
List.of(
36+
SearchOperator.in(fieldPath("genres"), List.of("Comedy")),
37+
SearchOperator.phrase(fieldPath("fullplot"), "new york"),
38+
SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000),
39+
SearchOperator.wildcard(fieldPath("title"), "Love *")
40+
)));
41+
42+
Bson projection = Aggregates.project(Projections.fields(
43+
Projections.include("title", "year", "genres")
44+
));
45+
46+
List<Bson> aggregateStages = List.of(searchStageFilters, projection);
47+
48+
Publisher<Document> publisher = movies.aggregate(aggregateStages);
49+
publisher.subscribe(new SubscriberHelpers.PrintDocumentSubscriber());
50+
Mono.from(publisher).block();
51+
// end atlasHelperMethods
52+
}
53+
}
54+
}

Diff for: source/indexes.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ field:
177177
.. TODO: To learn more about wildcard indexes, see the :ref:`java-rs-clustered-index`
178178
.. guide.
179179

180+
.. _java-rs-atlas-search-idx-mgmt:
181+
180182
Atlas Search Index Management
181183
-----------------------------
182184

@@ -275,4 +277,4 @@ The following example deletes an index with the specified name:
275277
:dedent:
276278

277279
.. TODO: To learn more about removing indexes, see :ref:`java-rs-indexes-remove`
278-
.. in the Work with Indexes guide.
280+
.. in the Work with Indexes guide.

Diff for: source/whats-new.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ and features:
5757

5858
.. replacement:: atlas-query-operators
5959

60-
the `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__
61-
interface API documentation
60+
the :ref:`java-rs-atlas-search-stage` section of the Aggregation
61+
guide
6262

6363
.. _javars-version-5.3:
6464

0 commit comments

Comments
 (0)