@@ -166,7 +166,6 @@ export const searchAfterAndBulkCreate = async ({
166166 searchResult . hits . hits [ searchResult . hits . hits . length - 1 ] ?. _source [ '@timestamp' ]
167167 )
168168 : null ;
169- searchResultSize += searchResult . hits . hits . length ;
170169
171170 // filter out the search results that match with the values found in the list.
172171 // the resulting set are valid signals that are not on the allowlist.
@@ -180,12 +179,24 @@ export const searchAfterAndBulkCreate = async ({
180179 buildRuleMessage,
181180 } )
182181 : searchResult ;
182+ // searchResultSize += filteredEvents.hits.hits.length;
183183 if ( filteredEvents . hits . total === 0 || filteredEvents . hits . hits . length === 0 ) {
184184 // everything in the events were allowed, so no need to generate signals
185185 toReturn . success = true ;
186186 break ;
187187 }
188188
189+ // make sure we are not going to create more signals than maxSignals allows
190+ if (
191+ searchResultSize != null &&
192+ searchResultSize + filteredEvents . hits . hits . length > tuple . maxSignals
193+ ) {
194+ filteredEvents . hits . hits = filteredEvents . hits . hits . slice (
195+ 0 ,
196+ tuple . maxSignals - searchResultSize
197+ ) ;
198+ }
199+
189200 const {
190201 bulkCreateDuration : bulkDuration ,
191202 createdItemsCount : createdCount ,
@@ -207,9 +218,11 @@ export const searchAfterAndBulkCreate = async ({
207218 refresh,
208219 tags,
209220 throttle,
221+ searchResultSize,
210222 } ) ;
211223 logger . debug ( buildRuleMessage ( `created ${ createdCount } signals` ) ) ;
212224 toReturn . createdSignalsCount += createdCount ;
225+ searchResultSize += createdCount ;
213226 if ( bulkDuration ) {
214227 toReturn . bulkCreateTimes . push ( bulkDuration ) ;
215228 }
@@ -230,6 +243,11 @@ export const searchAfterAndBulkCreate = async ({
230243 ? filteredEvents . hits . hits [ 0 ] . sort [ 0 ]
231244 : undefined ;
232245 }
246+ logger . debug (
247+ `is searchResultSize (${ searchResultSize } ) > maxSignals (${ tuple . maxSignals } )?: ${
248+ searchResultSize > tuple . maxSignals
249+ } `
250+ ) ;
233251 } catch ( exc ) {
234252 logger . error ( buildRuleMessage ( `[-] search_after and bulk threw an error ${ exc } ` ) ) ;
235253 toReturn . success = false ;
0 commit comments