問題
在OpenSearch中,我希望在action中把一整條log作為報警內容發送出來,但是根據文檔,配置為ctx.results.0.hits.hits.0._source.log,但是發現根本找不到這個值
經過查詢,我發現在返回的ctx中僅存在如下的值
results=[, hits=, max_score=null}, took=13, timed_out=false}], periodStart=2024-07-08T08:30:07.425Z, periodEnd=2024-07-08T08:31:07.425Z, error=null, trigger={id=GkWmgZABvp-0qNhTa6Rs, name=Test alarm, please ignore, severity=3, actions=[{name=Test alarm, please ignore}]}, alert={acknowledged_time=null, id=sHdKkZABDnp5EchiGRFG, version=-1, end_time=null, error_message=null, execution_id=HEWmgZABvp-0qNhTa6R5_2024-07-08T07:41:09.151397924_6ca4e795-58a9-489d-85fe-74c4bbf2d9ab, workflow_id=, workflow_name=, associated_alert_ids=[], last_notification_time=1720426150054, severity=3, start_time=1720424470852, state=ACTIVE, bucket_keys=null, parent_bucket_path=null, finding_ids=, related_doc_ids=}}
沒有我需要的
查詢語句為
{"size": 0,"query": {"bool": {"filter": [{"range": {"@timestamp": {"from": "{{period_end}}||-1d","to": "{{period_end}}","include_lower": true,"include_upper": true,"format": "epoch_millis","boost": 1}}},{"query_string": {"query": "*BATCHMONITORING*","default_field": "log","fields": [],"type": "best_fields","default_operator": "or","max_determinized_states": 10000,"enable_position_increments": true,"fuzziness": "AUTO","fuzzy_prefix_length": 0,"fuzzy_max_expansions": 50,"phrase_slop": 0,"escape": false,"auto_generate_synonyms_phrase_query": true,"fuzzy_transpositions": true,"boost": 1}}],"adjust_pure_negative": true,"boost": 1}}
}
解決方案
經過查詢,我發現問題存在于size這個參數
size: 這指定了返回的文檔數量。在這里設為 0 表示只返回匹配的聚合結果,而不返回實際的文檔內容。
我這邊把size:0從查詢語句中刪除后,返回的結果終于包含了我需要的參數
其他參數解釋
- query: 這是查詢的主體,使用布爾查詢(bool query),允許將多個過濾條件組合在一起。
- bool: 布爾查詢允許組合多個查詢子句,如
filter
、must
、must_not
等。 - filter: 這是一個過濾器子句,它包含了兩個過濾條件。
- range: 范圍查詢,用于限制
@timestamp
字段的范圍。具體來說:"from": "{{period_end}}||-1d"
:表示從period_end
變量指定的時間開始,向前推一天。{{period_end}}
是一個模板變量,在實際執行時會被具體的時間值替換。"to": "{{period_end}}"
:表示到period_end
變量指定的時間結束。"format": "epoch_millis"
:指定時間格式為毫秒級時間戳。"include_lower": true, "include_upper": true
:包括上下界值。
- query_string: 查詢字符串查詢,用于搜索包含指定關鍵字的文檔。
"query": "*BATCHMONITORING*"
:這里的查詢字符串是通配符形式,表示搜索包含 “BATCHMONITORING” 關鍵字的文檔。"default_field": "log"
:指定默認搜索字段為 “log”。"default_operator": "or"
:默認操作符為 OR 操作符,表示任何一個關鍵字匹配即可。- 其他參數如
fuzziness
、phrase_slop
等是調整查詢行為的參數,如自動模糊匹配、短語搜索等。
- adjust_pure_negative: 設置為
true
,表示查詢結果中不包含全負匹配的文檔。 - boost: 設置為
1
,表示默認的查詢權重。