@@ -24,7 +24,15 @@ describe("firestore-pipelines", () => {
2424 or,
2525 xor,
2626 conditional,
27- concat
27+ concat,
28+ variable,
29+ documentMatches,
30+ score,
31+ matches,
32+ snippet,
33+ exists,
34+ not,
35+ currentTimestamp
2836 } = require ( "@google-cloud/firestore/pipelines" ) ;
2937
3038 let app ;
@@ -2118,4 +2126,140 @@ describe("firestore-pipelines", () => {
21182126 // [END vector_length]
21192127 console . log ( result ) ;
21202128 }
2129+
2130+ async function searchExamples ( ) {
2131+ // [START search_example]
2132+ await db . pipeline ( ) . collection ( 'restaurants' )
2133+ . search ( {
2134+ query : documentMatches ( 'waffles' )
2135+ } )
2136+ . execute ( ) ;
2137+ // [END search_example]
2138+
2139+ // [START search_exact_match]
2140+ await db . pipeline ( ) . collection ( 'restaurants' )
2141+ . search ( {
2142+ query : documentMatches ( '"belgian waffles"' )
2143+ } )
2144+ . execute ( ) ;
2145+ // [END search_exact_match]
2146+
2147+ // [START search_two_terms]
2148+ await db . pipeline ( ) . collection ( 'restaurants' )
2149+ . search ( {
2150+ query : documentMatches ( 'waffles eggs' )
2151+ } )
2152+ . execute ( ) ;
2153+ // [END search_two_terms]
2154+
2155+ // [START search_exclude_term]
2156+ await db . pipeline ( ) . collection ( 'restaurants' )
2157+ . search ( {
2158+ query : documentMatches ( '-waffles' )
2159+ } )
2160+ . execute ( ) ;
2161+ // [END search_exclude_term]
2162+
2163+ // [START search_score_field]
2164+ await db . pipeline ( ) . collection ( 'restaurants' )
2165+ . search ( {
2166+ query : field ( 'menu' ) . matches ( 'waffles' ) ,
2167+ addFields : [
2168+ score ( ) . as ( 'score' ) ,
2169+ ]
2170+ } ) . execute ( ) ;
2171+ // [END search_score_field]
2172+ }
2173+
2174+ async function subqueryExamples ( ) {
2175+ // [START define_example]
2176+ const result = await db . pipeline ( ) . collection ( "authors" )
2177+ . define (
2178+ field ( "id" ) . as ( "currentAuthorId" )
2179+ )
2180+ // [END define_example]
2181+ . addFields (
2182+ db . pipeline ( ) . collection ( "books" )
2183+ . where ( field ( "author_id" ) . equal ( variable ( "currentAuthorId" ) ) )
2184+ . aggregate (
2185+ field ( "rating" ) . average ( ) . as ( "avgRating" )
2186+ )
2187+ . toScalarExpression ( )
2188+ . as ( "averageBookRating" )
2189+ )
2190+ . execute ( ) ;
2191+ console . log ( result ) ;
2192+
2193+ // [START to_array_expression]
2194+ await db . pipeline ( ) . collection ( "projects" )
2195+ . define (
2196+ field ( "id" ) . as ( "parentId" )
2197+ )
2198+ . addFields (
2199+ db . pipeline ( ) . collection ( "tasks" )
2200+ . where ( field ( "project_id" ) . equal ( variable ( "parentId" ) ) )
2201+ . select ( field ( "title" ) )
2202+ . toArrayExpression ( )
2203+ . as ( "taskTitles" )
2204+ )
2205+ . execute ( ) ;
2206+ // [END to_array_expression]
2207+
2208+ // [START to_scalar_expression]
2209+ await db . pipeline ( ) . collection ( "authors" )
2210+ . define (
2211+ field ( "id" ) . as ( "currentAuthorId" )
2212+ )
2213+ . addFields (
2214+ db . pipeline ( ) . collection ( "books" )
2215+ . where ( field ( "author_id" ) . equal ( variable ( "currentAuthorId" ) ) )
2216+ . aggregate (
2217+ field ( "rating" ) . average ( ) . as ( "avgRating" )
2218+ )
2219+ . toScalarExpression ( )
2220+ . as ( "averageBookRating" )
2221+ )
2222+ . execute ( ) ;
2223+ // [END to_scalar_expression]
2224+ }
2225+
2226+ async function forceIndexExamples ( ) {
2227+ // [START force_index_id]
2228+ // Force Planner to use Index ID CICAgOi36pgK
2229+ await db . pipeline ( )
2230+ . collectionGroup ( { collectionId : "customers" , forceIndex : "CICAgOi36pgK" } )
2231+ . limit ( 100 )
2232+ . execute ( ) ;
2233+ // [END force_index_id]
2234+
2235+ // [START force_index_primary]
2236+ // Force Planner to only do a collection scan
2237+ await db . pipeline ( )
2238+ . collectionGroup ( { collectionId : "customers" , forceIndex : "primary" } )
2239+ . limit ( 100 )
2240+ . execute ( ) ;
2241+ // [END force_index_primary]
2242+ }
2243+
2244+ async function dmlExamples ( ) {
2245+ // [START update_dml]
2246+ const snapshot = await db . pipeline ( )
2247+ . collectionGroup ( "users" )
2248+ . where ( not ( exists ( field ( "preferences.color" ) ) ) )
2249+ . addFields ( constant ( null ) . as ( "preferences.color" ) )
2250+ . removeFields ( "color" )
2251+ . update ( )
2252+ . execute ( ) ;
2253+ // [END update_dml]
2254+ console . log ( snapshot ) ;
2255+
2256+ // [START delete_dml]
2257+ const pipeline = db . pipeline ( )
2258+ . collectionGroup ( "users" )
2259+ . where ( field ( "address.country" ) . equal ( "USA" ) )
2260+ . where ( field ( "__create_time__" ) . timestampAdd ( "day" , 10 ) . lessThan ( currentTimestamp ( ) ) )
2261+ . delete ( ) ;
2262+ await pipeline . execute ( ) ;
2263+ // [END delete_dml]
2264+ }
21212265} ) ;
0 commit comments