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
+
17
+ import static com .mongodb .client .model .search .SearchPath .fieldPath ;
18
+
18
19
public class AggregateSearchBuilderExample {
19
20
20
- private static final String CONNECTION_URI = "<connection URI >" ;
21
+ private static final String CONNECTION_URI = "<connection uri >" ;
21
22
22
23
// Match aggregation
23
24
private static void runMatch (MongoCollection <Document > collection ) {
24
25
Bson matchStage = Aggregates .match (Filters .eq ("title" , "Future" ));
25
26
Bson projection = Aggregates .project (Projections .fields (Projections .include ("title" , "released" )));
26
27
27
- List <Bson > aggregateStages = Arrays . asList (matchStage , projection );
28
+ List <Bson > aggregateStages = List . of (matchStage , projection );
28
29
System .out .println ("aggregateStages: " + aggregateStages );
29
30
collection .aggregate (
30
31
aggregateStages
31
- ).forEach (result -> System .out .println (result ));
32
+ ).forEach (result -> System .out .println (result ));
32
33
}
33
34
34
- /*
35
+ /*
35
36
* Atlas text search aggregation
36
37
* Requires Atlas cluster and full text search index
37
38
* 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) {
40
41
// begin atlasTextSearch
41
42
Bson textSearch = Aggregates .search (
42
43
SearchOperator .text (
43
- SearchPath . fieldPath ("title" ), "Future" ));
44
+ fieldPath ("title" ), "Future" ));
44
45
// end atlasTextSearch
45
46
46
47
// To condense result data, add this projection into the pipeline
47
48
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
48
49
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 );
50
75
System .out .println ("aggregateStages: " + aggregateStages );
51
76
52
77
System .out .println ("explain:\n " + collection .aggregate (aggregateStages ).explain ());
@@ -55,19 +80,19 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
55
80
56
81
private static void runAtlasTextSearchMeta (MongoCollection <Document > collection ) {
57
82
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" )));
61
86
// end atlasSearchMeta
62
87
63
- List <Bson > aggregateStages = Arrays . asList (textSearchMeta );
88
+ List <Bson > aggregateStages = List . of (textSearchMeta );
64
89
System .out .println ("aggregateStages: " + aggregateStages );
65
90
66
91
collection .aggregate (aggregateStages ).forEach (result -> System .out .println (result ));
67
92
}
68
93
69
94
public static void main (String [] args ) {
70
- String uri = CONNECTION_URI ;
95
+ String uri = CONNECTION_URI ;
71
96
72
97
try (MongoClient mongoClient = MongoClients .create (uri )) {
73
98
MongoDatabase database = mongoClient .getDatabase ("sample_mflix" );
@@ -77,7 +102,8 @@ public static void main(String[] args) {
77
102
// Uncomment the methods that correspond to what you're testing
78
103
// runMatch(collection);
79
104
// runAtlasTextSearch(collection);
80
- runAtlasTextSearchMeta (collection );
105
+ runAtlasSearch (collection );
106
+ // runAtlasTextSearchMeta(collection);
81
107
}
82
108
}
83
109
}
0 commit comments