# Unable to access Documents Fields (Per Document Monitor Alerts)

**URL:** <https://forum.opensearch.org/t/unable-to-access-documents-fields-per-document-monitor-alerts/20465>\
**Category:** Alerting\
**Created:** [July 23, 2024, 9:23am UTC](https://forum.opensearch.org/t/unable-to-access-documents-fields-per-document-monitor-alerts/20465 "2024-07-23T09:23:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kmrs97](https://avatars.discourse-cdn.com/v4/letter/k/6de8d8/32.png) [@Kmrs97](https://forum.opensearch.org/u/Kmrs97)\
**Post date:** [July 23, 2024, 9:23am UTC](https://forum.opensearch.org/t/unable-to-access-documents-fields-per-document-monitor-alerts/20465/1 "2024-07-23T09:23:58Z")

</div>

Hello, I am using the latest version of OpenSearch and OpenSearch Dashboards. My goal is to check if each document indexed in a specified index pattern matches a specific field, and if it does, send an alert to a custom webhook.

I configured the monitor as follows:

 ![Screenshot_1](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/d/df3503b127968af791d5615965238175470f852e.png)  
 ![Screenshot_2](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/8/8e8a62d226e4d4549fe70d097f484692901079f2.png)  
 ![Screenshot_3](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/8/86d47a747842921a5b5fd35fe8369ffa453e5421.png)  
 ![Screenshot_4](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/f/f5a40e2b451b6eb7b1ebde79b296ce52aa658dc5.png)

My query is:  
{  
“description”: “”,  
“queries”: [  
{  
“id”: “4a4d4a18-8776-4564-a43f-dcc699d29c04”,  
“name”: “Detect\_alert”,  
“fields”: ,  
“query”: “EventType:"alert"”,  
“tags”: ,  
“query\_field\_names”:   
}  
]  
}

My trigger condition is:  
query[name=Detect\_alert]

And as an action I sent an alert to a custom webhook with the message:  
{  
“test” : “test”  
}.

All good so far. I am receiving the POST requests correctly on my custom webhook for every document that meets the conditions. However, I would like to include some data from each document, such as the fields [“Message”, “DeviceId”, etc.], in the message sent to the webhook. Observing the Trigger Info results, I noticed that the results only contain the document’s id along with its index.

“results”: [  
{  
“4a4d4a18-8776-4564-a43f-dcc699d29c04”: [  
“Bte72pAB5TTZtaAgM7Vj|topic-wifi”,  
“Bde72pAB5TTZtaAgM7Vh|topic-wifi”,  
“BNe72pAB5TTZtaAgM7Vf|topic-wifi”,  
“A9e72pAB5TTZtaAgM7Ve|topic-wifi”,  
“Ade62pAB5TTZtaAgvbWI|topic-wifi”,  
“ANe62pAB5TTZtaAgvbWI|topic-wifi”,  
“\_9e62pAB5TTZtaAgvbSI|topic-wifi”,  
“\_te62pAB5TTZtaAgvbSI|topic-wifi”,  
“\_de62pAB5TTZtaAgvbSD|topic-wifi”,  
“-9e62pAB5TTZtaAgRrTf|topic-wifi”,  
“-te62pAB5TTZtaAgRrTe|topic-wifi”,  
“-Ne62pAB5TTZtaAgRrTc|topic-wifi”,  
“-de62pAB5TTZtaAgRrTc|topic-wifi”,…]  
}  
]

Is there a way to include the document information and use their fields to send additional data to my webhook?

For example, send a message like:  
{{#ctx.results}}  
{  
"Alert on ":{{$query\_id.$document\_that\_trieggered\_the\_alert.DeviceId}}  
“Message”:{{$query\_id.$document\_that\_trieggered\_the\_alert.Message}}  
}  
{{/ctx.results}}

I apologize if this issue has already been addressed. If so, could you please direct me to the previous solutions?

Thank you!

---

<div class="post-metadata">

**Author:** ![HarryPottar](https://avatars.discourse-cdn.com/v4/letter/h/bb73d2/32.png) [@HarryPottar](https://forum.opensearch.org/u/HarryPottar)\
**Post date:** [September 6, 2024, 12:35am UTC](https://forum.opensearch.org/t/unable-to-access-documents-fields-per-document-monitor-alerts/20465/2 "2024-09-06T00:35:06Z")

</div>

You are close with your message template.

The document can be extracted using the mustache array syntax.

```auto
{{#ctx.alerts}}
		{{#sample_documents}}
        {{/sample_documents}}
{{/ctx.alerts}}

```

However, there is an issue: mustache indexes don’t work.

```auto
{{^-last}}, {{/-last}}

```

If this worked, it would say only to put a comma if this is not the last iteration.

Because your alert will have multiple documents, you need a comma between them. Which means the results are not valid for JSON. I have my webhook sent as text/html and then run regular expression at the other end to remove any stray commas to make the string valid JSON

```auto
{
	"alerts": {
		"triggerId": "{{ctx.trigger.id}}",
   		"triggerName": "{{ctx.trigger. name}}", 
   		"lastUpdate": "{{ctx.last_update_time}}", 
   		"periodStart": "{{ctx.periodStart}}", 
   		"periodEnd": "{{ctx.periodEnd}}", 
        "documents": [
        {{#ctx.alerts}}
		    {{#sample_documents}}
		        {
			       "index": "{{_index}}",
			       "documentId": "{{_id}}",
			       "timestamp": "{{_source.@timestamp}}",
			       "event": {
				         "nodeId": "{{_source.event.nodeId}}",
				         "filespace": "{{_source.event.filespace}}",
				         "filespaceUuid": "{{_source.event.filespaceUuid}}"
			         }
		         },
		   {{/sample_documents}}
		{{/ctx.alerts}}
	]}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [November 5, 2024, 12:35am UTC](https://forum.opensearch.org/t/unable-to-access-documents-fields-per-document-monitor-alerts/20465/3 "2024-11-05T00:35:57Z")

</div>

Closed on November 5, 2024, 12:35am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
