Skip to content

Commit c034598

Browse files
committed
atlas search operators
1 parent 6f002e7 commit c034598

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

source/atlas-search.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,27 @@ the following API documentation:
105105
- `MongoCollection.aggregate() <{+driver-api+}/MongoCollection.html#aggregate(java.util.List)>`__
106106
- `Aggregates.search() <{+core-api+}/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchCollector)>`__
107107
- `Aggregates.project() <{+core-api+}/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__
108+
109+
In this guide, you can learn how to use Atlas Search
110+
in the {+driver-short+}. For more information about Atlas Search and its
111+
benefits, see the :atlas:`Atlas Search </atlas-search/>` guide.
112+
113+
Atlas Search enables advanced search functionality for your applications without any additional management or
114+
separate search system alongside your database. Atlas Search queries take the
115+
form of an :ref:`aggregation pipeline <java-aggregation>` stage.
116+
117+
The following operators:
118+
119+
- ``phrase``
120+
121+
- ``regex``
122+
123+
- ``queryString``
124+
125+
- ``equals``
126+
127+
- ``moreLikeThis``
128+
129+
- ``in``
130+
131+
- ``wildcard``
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/references/whats-new.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ and features:
5454

5555
.. replacement:: atlas-query-operators
5656

57+
<<<<<<< HEAD
5758
the :ref:`java-atlas-search` guide
59+
=======
60+
the :ref:`java-atlas-search` page
61+
>>>>>>> e82dac4 (atlas search operators)
5862

5963
.. _java-version-5.3:
6064

0 commit comments

Comments
 (0)