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+
17+ import static com .mongodb .client .model .search .SearchPath .fieldPath ;
18+
1819public 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}
0 commit comments