1
- package fundamentals . builders ;
1
+ package org . example ;
2
2
3
- import java .util .Arrays ;
4
3
import java .util .List ;
5
4
6
5
import org .bson .Document ;
14
13
import com .mongodb .client .model .Filters ;
15
14
import com .mongodb .client .model .Projections ;
16
15
import com .mongodb .client .model .search .SearchOperator ;
17
- import com .mongodb .client .model .search .SearchPath ;
16
+ import static com .mongodb .client .model .search .SearchPath .fieldPath ;
17
+
18
18
public class AggregateSearchBuilderExample {
19
19
20
20
private static final String CONNECTION_URI = "<connection URI>" ;
@@ -24,14 +24,14 @@ private static void runMatch(MongoCollection<Document> collection) {
24
24
Bson matchStage = Aggregates .match (Filters .eq ("title" , "Future" ));
25
25
Bson projection = Aggregates .project (Projections .fields (Projections .include ("title" , "released" )));
26
26
27
- List <Bson > aggregateStages = Arrays . asList (matchStage , projection );
27
+ List <Bson > aggregateStages = List . of (matchStage , projection );
28
28
System .out .println ("aggregateStages: " + aggregateStages );
29
29
collection .aggregate (
30
30
aggregateStages
31
- ).forEach (result -> System .out .println (result ));
31
+ ).forEach (result -> System .out .println (result ));
32
32
}
33
33
34
- /*
34
+ /*
35
35
* Atlas text search aggregation
36
36
* Requires Atlas cluster and full text search index
37
37
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
@@ -40,13 +40,65 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
40
40
// begin atlasTextSearch
41
41
Bson textSearch = Aggregates .search (
42
42
SearchOperator .text (
43
- SearchPath . fieldPath ("title" ), "Future" ));
43
+ fieldPath ("title" ), "Future" ));
44
44
// end atlasTextSearch
45
45
46
46
// To condense result data, add this projection into the pipeline
47
47
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
48
48
49
- List <Bson > aggregateStages = Arrays .asList (textSearch );
49
+ List <Bson > aggregateStages = List .of (textSearch );
50
+ System .out .println ("aggregateStages: " + aggregateStages );
51
+
52
+ System .out .println ("explain:\n " + collection .aggregate (aggregateStages ).explain ());
53
+ collection .aggregate (aggregateStages ).forEach (result -> System .out .println (result ));
54
+ }
55
+
56
+ /*
57
+ * Atlas search aggregation
58
+ * Requires Atlas cluster and full text search index
59
+ * See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
60
+ */
61
+ private static void runAtlasSearchWithSearchHelperMethods (MongoCollection <Document > collection ) {
62
+ // begin atlasHelperMethods
63
+ List <Bson > pipeline = new ArrayList <>();
64
+
65
+ pipeline .add (Aggregates .search (
66
+ SearchOperator .compound ()
67
+ .filter (
68
+ List .of (
69
+ SearchOperator .in (fieldPath ("genres" ), "Comedy" ),
70
+ SearchOperator .phrase (fieldPath ("fullplot" ), "new york" ),
71
+ SearchOperator .numberRange (fieldPath ("year" )).gtLt (1950 , 2000 ),
72
+ SearchOperator .wildcard (fieldPath ("title" ), "Love *" )
73
+ ))));
74
+
75
+ pipeline .add (Aggregates .project (
76
+ Projections .include ("title" , "year" , "genres" )
77
+ ));
78
+
79
+ AggregateIterable <Document > results = collection .aggregate (pipeline );
80
+ results .forEach (doc -> System .out .println (doc .toJson ()));
81
+ // end atlasHelperMethods
82
+ }
83
+
84
+ /*
85
+ * Atlas search aggregation
86
+ * Requires Atlas cluster and full text search index
87
+ * See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
88
+ */
89
+ private static void runAtlasSearch (MongoCollection <Document > collection ) {
90
+ // begin atlasSearch
91
+ Bson search_stage = Aggregates .search (
92
+ SearchOperator .compound ()
93
+ .filter (List .of (SearchOperator .text (fieldPath ("genres" ), "Drama" )))
94
+ .must (List .of (SearchOperator .phrase (fieldPath ("cast" ), "keanu reeves" )))
95
+ );
96
+ // end atlasSearch
97
+
98
+ // To condense result data, add this projection into the pipeline
99
+ // Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
100
+
101
+ List <Bson > aggregateStages = List .of (search_stage );
50
102
System .out .println ("aggregateStages: " + aggregateStages );
51
103
52
104
System .out .println ("explain:\n " + collection .aggregate (aggregateStages ).explain ());
@@ -55,12 +107,12 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
55
107
56
108
private static void runAtlasTextSearchMeta (MongoCollection <Document > collection ) {
57
109
Bson textSearchMeta =
58
- // begin atlasSearchMeta
59
- Aggregates .searchMeta (
60
- SearchOperator .near (2010 , 1 , SearchPath . fieldPath ("year" )));
110
+ // begin atlasSearchMeta
111
+ Aggregates .searchMeta (
112
+ SearchOperator .near (2010 , 1 , fieldPath ("year" )));
61
113
// end atlasSearchMeta
62
114
63
- List <Bson > aggregateStages = Arrays . asList (textSearchMeta );
115
+ List <Bson > aggregateStages = List . of (textSearchMeta );
64
116
System .out .println ("aggregateStages: " + aggregateStages );
65
117
66
118
collection .aggregate (aggregateStages ).forEach (result -> System .out .println (result ));
@@ -77,7 +129,9 @@ public static void main(String[] args) {
77
129
// Uncomment the methods that correspond to what you're testing
78
130
// runMatch(collection);
79
131
// runAtlasTextSearch(collection);
80
- runAtlasTextSearchMeta (collection );
132
+ // runAtlasSearch(collection);
133
+ // runAtlasTextSearchMeta(collection);
134
+ // runAtlasSearchWithSearchHelperMethods(collection);
81
135
}
82
136
}
83
137
}
0 commit comments