Opensearch/ElasticSearch-ctx查询内容不全的问题

问题

在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,表示默认的查询权重。
相关推荐
宸津-代码粉碎机18 分钟前
OpenAI 连夜迎战 Grok Bot 和 Muse:AI 智能体从 “会聊天” 到 “能办事”,现在入场还来得及吗
java·大数据·人工智能·分布式·python
Y38153266233 分钟前
SERP API 计费口径:credits_charged、执行失败扣费与退款边界
python·搜索引擎
Leo.yuan2 小时前
2026年本地化Data Agent优质厂商盘点:哪些产品更适合企业生产环境
大数据·数据库·人工智能
云上先途2 小时前
任务智能体可以自动完成哪些类型工作,是不是只能做简单重复操作?
大数据·人工智能
小蒋观天下2 小时前
两轮车检测AI摄像头——2026行业竞争格局、商业模式与核心痛点
大数据·人工智能·安全·计算机视觉·ai大模型
RisunJan2 小时前
【这就是AI】AI每日资讯简报 - 2026-09-28(周一)
大数据·人工智能
YoungStudyAI3 小时前
QA的下一个自动化阶段,可能不是再写更多脚本,而是设计测试Agent
运维·自动化
圆圆讲门店3 小时前
挑选同城获客服务机构时需要考量的核心因素都有哪些?
大数据·网络·人工智能·python
frjc4 小时前
数据库选型:如何从众多数据库中选出最理想的那一个
redis·mysql·clickhouse·elasticsearch
分布式存储与RustFS4 小时前
纠删码到底吃掉了多少容量:EC:4、EC:8 的利用率与容错怎么算
运维·云原生·开源·对象存储·分布式存储·s3·性能基准