From 84db03ebae4859de5d171849625c31933004b53b Mon Sep 17 00:00:00 2001 From: Annie Lee Date: Tue, 22 Mar 2022 14:16:40 -0700 Subject: [PATCH] Add search document function --- .../TransportGetFindingsSearchAction.kt | 56 +++++++++++++++++-- .../transport/TransportGetMonitorAction.kt | 2 + 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsSearchAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsSearchAction.kt index c76d2c32e..722e02d5e 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsSearchAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsSearchAction.kt @@ -135,8 +135,6 @@ class TransportGetFindingsSearchAction @Inject constructor( val searchRequest = SearchRequest() .source(searchSourceBuilder) .indices(".opensearch-alerting-findings") - // Debug request - log.info("Request: $searchRequest") client.search( searchRequest, object : ActionListener { @@ -156,12 +154,16 @@ class TransportGetFindingsSearchAction @Inject constructor( log.info("created parser") XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.nextToken(), xcp) // TODO: Remove debug log - log.info("Adding new finding with id: $id") + log.info("Parsing new finding with id: $id") val finding = Finding.parse(xcp, id) // TODO: Search the document with doc_id - val doc_id = finding.relatedDocId - log.info("doc_id: $doc_id") - findings.add(Finding.parse(xcp, id)) + val doc_ids = finding.relatedDocId + val sourceIndex = finding.index + log.info("doc_ids: $doc_ids") + findings.add(finding) + for (doc_id in doc_ids) { + // Search document in index + } } actionListener.onResponse(GetFindingsSearchResponse(RestStatus.OK, totalFindingCount, findings)) } @@ -172,4 +174,46 @@ class TransportGetFindingsSearchAction @Inject constructor( } ) } + + fun searchDocument( + documentId: String, + sourceIndex: String, + searchSourceBuilder: SearchSourceBuilder, + actionListener: ActionListener + ) { + log.info("Entering searchDocument.kt.") + val getRequest = GetRequest(sourceIndex, documentId) + // Debug request + log.info("Request: $getRequest") + client.threadPool().threadContext.stashContext().use { + client.get( + getRequest, + object : ActionListener { + override fun onResponse(response: GetResponse) { + if (!response.isExists) { + actionListener.onFailure( + AlertingException.wrap( + OpenSearchStatusException( + "Document $documentId not found from source index $sourceIndex.", + RestStatus.NOT_FOUND + ) + ) + ) + return + } + log.info("response: $response") + var monitor: Monitor? = null + if (!response.isSourceEmpty) { + log.info("response not empty") + } + // actionListener.onResponse(GetFindingsSearchResponse(RestStatus.OK, totalFindingCount, findings)) + } + + override fun onFailure(t: Exception) { + actionListener.onFailure(AlertingException.wrap(t)) + } + } + ) + } + } } diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetMonitorAction.kt index 628f8a5c5..2ae205c82 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetMonitorAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetMonitorAction.kt @@ -58,6 +58,8 @@ class TransportGetMonitorAction @Inject constructor( .version(getMonitorRequest.version) .fetchSourceContext(getMonitorRequest.srcContext) // Debug use + log.info("srcContext: $getMonitorRequest.srcContext") + log.info("version: $getMonitorRequest.version") log.info("getRequest: $getRequest") if (!validateUserBackendRoles(user, actionListener)) {