@@ -9,13 +9,14 @@ import org.apache.logging.log4j.LogManager
9
9
import org.opensearch.ExceptionsHelper
10
10
import org.opensearch.OpenSearchStatusException
11
11
import org.opensearch.action.DocWriteRequest
12
+ import org.opensearch.action.admin.indices.refresh.RefreshAction
13
+ import org.opensearch.action.admin.indices.refresh.RefreshRequest
12
14
import org.opensearch.action.bulk.BulkRequest
13
15
import org.opensearch.action.bulk.BulkResponse
14
16
import org.opensearch.action.index.IndexRequest
15
17
import org.opensearch.action.search.SearchAction
16
18
import org.opensearch.action.search.SearchRequest
17
19
import org.opensearch.action.search.SearchResponse
18
- import org.opensearch.action.support.WriteRequest
19
20
import org.opensearch.alerting.model.DocumentExecutionContext
20
21
import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
21
22
import org.opensearch.alerting.model.InputRunResults
@@ -395,7 +396,6 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
395
396
396
397
val actionCtx = triggerCtx.copy(
397
398
triggeredDocs = triggerResult.triggeredDocs,
398
- // confirm if this is right or only trigger-able findings should be present in this list
399
399
relatedFindings = findingToDocPairs.map { it.first },
400
400
error = monitorResult.error ? : triggerResult.error
401
401
)
@@ -477,10 +477,6 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
477
477
val findingDocPairs = mutableListOf<Pair <String , String >>()
478
478
val findings = mutableListOf<Finding >()
479
479
val indexRequests = mutableListOf<IndexRequest >()
480
- monitorCtx.findingsIndexBatchSize = FINDINGS_INDEXING_BATCH_SIZE .get(monitorCtx.settings)
481
- monitorCtx.clusterService!! .clusterSettings.addSettingsUpdateConsumer(FINDINGS_INDEXING_BATCH_SIZE ) {
482
- monitorCtx.findingsIndexBatchSize = it
483
- }
484
480
485
481
docsToQueries.forEach {
486
482
val triggeredQueries = it.value.map { queryId -> idQueryMap[queryId]!! }
@@ -507,22 +503,15 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
507
503
.string()
508
504
logger.debug(" Findings: $findingStr " )
509
505
510
- if (indexRequests.size > monitorCtx.findingsIndexBatchSize) {
511
- // make bulk indexing call here and flush the indexRequest object
512
- bulkIndexFindings(monitor, monitorCtx, indexRequests)
513
- indexRequests.clear()
514
- } else {
515
- if (shouldCreateFinding) {
516
- indexRequests + = IndexRequest (monitor.dataSources.findingsIndex)
517
- .source(findingStr, XContentType .JSON )
518
- .id(finding.id)
519
- .routing(finding.id)
520
- .opType(DocWriteRequest .OpType .INDEX )
521
- }
506
+ if (shouldCreateFinding) {
507
+ indexRequests + = IndexRequest (monitor.dataSources.findingsIndex)
508
+ .source(findingStr, XContentType .JSON )
509
+ .id(finding.id)
510
+ .opType(DocWriteRequest .OpType .CREATE )
522
511
}
523
512
}
524
513
525
- if (indexRequests.size <= monitorCtx.findingsIndexBatchSize ) {
514
+ if (indexRequests.isNotEmpty() ) {
526
515
bulkIndexFindings(monitor, monitorCtx, indexRequests)
527
516
}
528
517
@@ -542,9 +531,11 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
542
531
monitorCtx : MonitorRunnerExecutionContext ,
543
532
indexRequests : List <IndexRequest >
544
533
) {
545
- if (indexRequests.isNotEmpty()) {
534
+ monitorCtx.findingsIndexBatchSize = FINDINGS_INDEXING_BATCH_SIZE .get(monitorCtx.settings)
535
+
536
+ indexRequests.chunked(monitorCtx.findingsIndexBatchSize).forEach { batch ->
546
537
val bulkResponse: BulkResponse = monitorCtx.client!! .suspendUntil {
547
- bulk(BulkRequest ().add(indexRequests).setRefreshPolicy( WriteRequest . RefreshPolicy . IMMEDIATE ) , it)
538
+ bulk(BulkRequest ().add(indexRequests), it)
548
539
}
549
540
if (bulkResponse.hasFailures()) {
550
541
bulkResponse.items.forEach { item ->
@@ -556,6 +547,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
556
547
logger.debug(" [${bulkResponse.items.size} ] All findings successfully indexed." )
557
548
}
558
549
}
550
+ monitorCtx.client!! .execute(RefreshAction .INSTANCE , RefreshRequest (monitor.dataSources.findingsIndex))
559
551
}
560
552
561
553
private fun publishFinding (
0 commit comments