1- package fundamentals . builders ;
1+ package org . example ;
22
3- import java .util .Arrays ;
43import java .util .List ;
54
65import org .bson .Document ;
1413import com .mongodb .client .model .Filters ;
1514import com .mongodb .client .model .Projections ;
1615import 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+
1818public class AggregateSearchBuilderExample {
1919
2020 private static final String CONNECTION_URI = "<connection URI>" ;
@@ -24,14 +24,14 @@ private static void runMatch(MongoCollection<Document> collection) {
2424 Bson matchStage = Aggregates .match (Filters .eq ("title" , "Future" ));
2525 Bson projection = Aggregates .project (Projections .fields (Projections .include ("title" , "released" )));
2626
27- List <Bson > aggregateStages = Arrays . asList (matchStage , projection );
27+ List <Bson > aggregateStages = List . of (matchStage , projection );
2828 System .out .println ("aggregateStages: " + aggregateStages );
2929 collection .aggregate (
3030 aggregateStages
31- ).forEach (result -> System .out .println (result ));
31+ ).forEach (result -> System .out .println (result ));
3232 }
3333
34- /*
34+ /*
3535 * Atlas text search aggregation
3636 * Requires Atlas cluster and full text search index
3737 * 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) {
4040 // begin atlasTextSearch
4141 Bson textSearch = Aggregates .search (
4242 SearchOperator .text (
43- SearchPath . fieldPath ("title" ), "Future" ));
43+ fieldPath ("title" ), "Future" ));
4444 // end atlasTextSearch
4545
4646 // To condense result data, add this projection into the pipeline
4747 // Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
4848
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 );
50102 System .out .println ("aggregateStages: " + aggregateStages );
51103
52104 System .out .println ("explain:\n " + collection .aggregate (aggregateStages ).explain ());
@@ -55,12 +107,12 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
55107
56108 private static void runAtlasTextSearchMeta (MongoCollection <Document > collection ) {
57109 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" )));
61113 // end atlasSearchMeta
62114
63- List <Bson > aggregateStages = Arrays . asList (textSearchMeta );
115+ List <Bson > aggregateStages = List . of (textSearchMeta );
64116 System .out .println ("aggregateStages: " + aggregateStages );
65117
66118 collection .aggregate (aggregateStages ).forEach (result -> System .out .println (result ));
@@ -77,7 +129,9 @@ public static void main(String[] args) {
77129 // Uncomment the methods that correspond to what you're testing
78130 // runMatch(collection);
79131 // runAtlasTextSearch(collection);
80- runAtlasTextSearchMeta (collection );
132+ // runAtlasSearch(collection);
133+ // runAtlasTextSearchMeta(collection);
134+ // runAtlasSearchWithSearchHelperMethods(collection);
81135 }
82136 }
83137}
0 commit comments