@@ -60,33 +60,25 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
60
60
*/
61
61
private static void runAtlasSearchWithSearchHelperMethods (MongoCollection <Document > collection ) {
62
62
// begin atlasHelperMethods
63
- Bson searchStageFilters = Aggregates .search (
63
+ List <Bson > pipeline = new ArrayList <>();
64
+
65
+ pipeline .add (Aggregates .search (
64
66
SearchOperator .compound ()
65
67
.filter (
66
- List . of (
67
- SearchOperator .text (fieldPath ("genres" ), "Drama" ),
68
- SearchOperator .phrase (fieldPath ("cast " ), "sylvester stallone " ),
69
- SearchOperator .numberRange (fieldPath ("year" )).gtLt (1980 , 1989 ),
70
- SearchOperator .wildcard (fieldPath ("title" ),"Rocky *" )
71
- )));
72
-
73
- Bson projection = Aggregates . project ( Projections . fields (
74
- Projections .include ("title" , "year" , "genres" , "cast" )
68
+ Arrays . asList (
69
+ SearchOperator .in (fieldPath ("genres" ), Arrays . asList ( "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" )
75
77
));
76
78
77
- List <Bson > aggregateStages = List .of (searchStageFilters , projection );
78
- collection .aggregate (aggregateStages ).forEach (System .out ::println );
79
-
79
+ AggregateIterable <Document > results = collection .aggregate (pipeline );
80
+ results .forEach (doc -> System .out .println (doc .toJson ()));
80
81
// end atlasHelperMethods
81
-
82
- // To condense result data, add this projection into the pipeline
83
- // Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
84
-
85
- List <Bson > aggregateStages = List .of (searchStageFilters );
86
- System .out .println ("aggregateStages: " + aggregateStages );
87
-
88
- System .out .println ("explain:\n " + collection .aggregate (aggregateStages ).explain ());
89
- collection .aggregate (aggregateStages ).forEach (result -> System .out .println (result ));
90
82
}
91
83
92
84
/*
@@ -137,8 +129,9 @@ public static void main(String[] args) {
137
129
// Uncomment the methods that correspond to what you're testing
138
130
// runMatch(collection);
139
131
// runAtlasTextSearch(collection);
140
- runAtlasSearch (collection );
132
+ // runAtlasSearch(collection);
141
133
// runAtlasTextSearchMeta(collection);
134
+ // runAtlasSearchWithSearchHelperMethods(collection);
142
135
}
143
136
}
144
137
}
0 commit comments