11'use strict'
22
33import { mapBulk , removeProps , getDocDescriptor } from '../utils/index'
4+ import { mergeESParamsWithRefresh } from '../utils/params'
5+ import { validateQueryComplexity } from '../utils/security'
46import { ElasticsearchServiceParams , ElasticAdapterInterface } from '../types'
57import { errors } from '@feathersjs/errors'
68
@@ -51,14 +53,16 @@ function createBulkOperations(
5153function prepareBulkUpdateParams (
5254 service : ElasticAdapterInterface ,
5355 operations : Array < Record < string , unknown > > ,
54- index : string
56+ index : string ,
57+ requestParams : ElasticsearchServiceParams
5558) : { params : Record < string , unknown > ; needsRefresh : boolean } {
59+ // PERFORMANCE: Merge esParams with per-operation refresh override
5660 const params = Object . assign (
5761 {
5862 index,
5963 body : operations
6064 } ,
61- service . esParams
65+ mergeESParamsWithRefresh ( service . esParams , requestParams )
6266 )
6367
6468 // Remove refresh from bulk params but return it separately
@@ -169,6 +173,9 @@ export async function patchBulk(
169173 const { filters } = service . filterQuery ( params )
170174 const index = ( filters . $index as string ) || service . index
171175
176+ // PERFORMANCE: Validate query complexity budget
177+ validateQueryComplexity ( params . query || { } , service . security . maxQueryComplexity )
178+
172179 // Step 1: Find documents to patch
173180 const findParams = prepareFindParams ( service , params )
174181 const results = await service . _find ( findParams )
@@ -194,7 +201,12 @@ export async function patchBulk(
194201 const operations = createBulkOperations ( service , found , data , index )
195202
196203 // Step 3: Prepare and execute bulk update
197- const { params : bulkUpdateParams , needsRefresh } = prepareBulkUpdateParams ( service , operations , index )
204+ const { params : bulkUpdateParams , needsRefresh } = prepareBulkUpdateParams (
205+ service ,
206+ operations ,
207+ index ,
208+ params
209+ )
198210
199211 let bulkResult = ( await service . Model . bulk ( bulkUpdateParams as never ) ) as unknown as Record < string , unknown >
200212
@@ -208,6 +220,11 @@ export async function patchBulk(
208220 return mapBulk ( bulkResult . items as Array < Record < string , unknown > > , service . id , service . meta , service . join )
209221 }
210222
223+ // PERFORMANCE: Lean mode - skip fetching full documents if requested
224+ if ( params . lean ) {
225+ return mapBulk ( bulkResult . items as Array < Record < string , unknown > > , service . id , service . meta , service . join )
226+ }
227+
211228 // Step 6: Fetch updated documents with selected fields
212229 const mgetResult = ( await fetchUpdatedDocuments ( service , updatedIds , index , filters ) ) as Record <
213230 string ,
0 commit comments