Skip to content

Commit aa084da

Browse files
committed
atlas search operators
1 parent f56ba21 commit aa084da

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

source/atlas-search.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Atlas Search
2121
Overview
2222
--------
2323

24+
<<<<<<< HEAD
2425
In this guide, you can learn how to use the {+driver-short+} to
2526
run :atlas:`Atlas Search </atlas-search/>` queries on a collection.
2627
Atlas Search enables you to perform full-text searches on collections
@@ -105,3 +106,28 @@ the following API documentation:
105106
- `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
106107
- `Aggregates.search() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchCollector)>`__
107108
- `Aggregates.project() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__
109+
=======
110+
In this guide, you can learn how to use Atlas Search
111+
in the {+driver-short+}. For more information about Atlas Search and its
112+
benefits, see the :atlas:`Atlas Search </atlas-search/>` guide.
113+
114+
Atlas Search enables advanced search functionality for your applications without any additional management or
115+
separate search system alongside your database. Atlas Search queries take the
116+
form of an :ref:`aggregation pipeline <java-aggregation>` stage.
117+
118+
The following operators:
119+
120+
- ``phrase``
121+
122+
- ``regex``
123+
124+
- ``queryString``
125+
126+
- ``equals``
127+
128+
- ``moreLikeThis``
129+
130+
- ``in``
131+
132+
- ``wildcard``
133+
>>>>>>> 666e714 (atlas search operators)
Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package fundamentals.builders;
1+
package org.example;
22

3-
import java.util.Arrays;
43
import java.util.List;
54

65
import org.bson.Document;
@@ -14,24 +13,26 @@
1413
import com.mongodb.client.model.Filters;
1514
import com.mongodb.client.model.Projections;
1615
import com.mongodb.client.model.search.SearchOperator;
17-
import com.mongodb.client.model.search.SearchPath;
16+
17+
import static com.mongodb.client.model.search.SearchPath.fieldPath;
18+
1819
public class AggregateSearchBuilderExample {
1920

20-
private static final String CONNECTION_URI = "<connection URI>";
21+
private static final String CONNECTION_URI = "<connection uri>";
2122

2223
// Match aggregation
2324
private static void runMatch(MongoCollection<Document> collection) {
2425
Bson matchStage = Aggregates.match(Filters.eq("title", "Future"));
2526
Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
2627

27-
List<Bson> aggregateStages = Arrays.asList(matchStage, projection);
28+
List<Bson> aggregateStages = List.of(matchStage, projection);
2829
System.out.println("aggregateStages: " + aggregateStages);
2930
collection.aggregate(
3031
aggregateStages
31-
).forEach(result -> System.out.println(result));
32+
).forEach(result -> System.out.println(result));
3233
}
3334

34-
/*
35+
/*
3536
* Atlas text search aggregation
3637
* Requires Atlas cluster and full text search index
3738
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
@@ -40,13 +41,37 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
4041
// begin atlasTextSearch
4142
Bson textSearch = Aggregates.search(
4243
SearchOperator.text(
43-
SearchPath.fieldPath("title"), "Future"));
44+
fieldPath("title"), "Future"));
4445
// end atlasTextSearch
4546

4647
// To condense result data, add this projection into the pipeline
4748
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
4849

49-
List<Bson> aggregateStages = Arrays.asList(textSearch);
50+
List<Bson> aggregateStages = List.of(textSearch);
51+
System.out.println("aggregateStages: " + aggregateStages);
52+
53+
System.out.println("explain:\n" + collection.aggregate(aggregateStages).explain());
54+
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
55+
}
56+
57+
/*
58+
* Atlas search aggregation
59+
* Requires Atlas cluster and full text search index
60+
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
61+
*/
62+
private static void runAtlasSearch(MongoCollection<Document> collection) {
63+
// begin atlasSearch
64+
Bson search_stage = Aggregates.search(
65+
SearchOperator.compound()
66+
.filter(List.of(SearchOperator.text(fieldPath("genres"), "Drama")))
67+
.must(List.of(SearchOperator.phrase(fieldPath("cast"), "keanu reeves")))
68+
);
69+
// end atlasSearch
70+
71+
// To condense result data, add this projection into the pipeline
72+
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
73+
74+
List<Bson> aggregateStages = List.of(search_stage);
5075
System.out.println("aggregateStages: " + aggregateStages);
5176

5277
System.out.println("explain:\n" + collection.aggregate(aggregateStages).explain());
@@ -55,19 +80,19 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
5580

5681
private static void runAtlasTextSearchMeta(MongoCollection<Document> collection) {
5782
Bson textSearchMeta =
58-
// begin atlasSearchMeta
59-
Aggregates.searchMeta(
60-
SearchOperator.near(2010, 1, SearchPath.fieldPath("year")));
83+
// begin atlasSearchMeta
84+
Aggregates.searchMeta(
85+
SearchOperator.near(2010, 1, fieldPath("year")));
6186
// end atlasSearchMeta
6287

63-
List<Bson> aggregateStages = Arrays.asList(textSearchMeta);
88+
List<Bson> aggregateStages = List.of(textSearchMeta);
6489
System.out.println("aggregateStages: " + aggregateStages);
6590

6691
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
6792
}
6893

6994
public static void main(String[] args) {
70-
String uri = CONNECTION_URI;
95+
String uri = CONNECTION_URI;
7196

7297
try (MongoClient mongoClient = MongoClients.create(uri)) {
7398
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
@@ -77,7 +102,8 @@ public static void main(String[] args) {
77102
// Uncomment the methods that correspond to what you're testing
78103
// runMatch(collection);
79104
// runAtlasTextSearch(collection);
80-
runAtlasTextSearchMeta(collection);
105+
runAtlasSearch(collection);
106+
// runAtlasTextSearchMeta(collection);
81107
}
82108
}
83109
}

source/versioning/whats-new.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ and features:
5252
the :ref:`java-oidc-kubernetes` section of the OIDC (Workload
5353
Identity Federation) guide
5454

55+
.. replacement:: atlas-query-operators
56+
57+
the :ref:`java-atlas-search` page
58+
5559
.. _java-version-5.3:
5660

5761
What's New in 5.3

0 commit comments

Comments
 (0)