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