在 elasticsearch 中验证聚合查询

Validate aggregate query in elasticsearch

我希望有人能解释我为什么遇到这个问题:我想在启动 ES(7.4 版)查询之前验证它,如果查询没有 aggs,一切正常 子句但是,如果查询有 aggs,ES 说这是一个无效的查询,让我发疯的是查询在 ES 中工作正常但同时 ES告诉我这不是一个有效的查询。

我认为是因为 ES 不使用 aggs 验证查询,但我很想 "be sure",我已经阅读了文档但没有找到任何线索。

非常感谢。

这是我要验证的 ES 查询示例

        "query": {
            "bool": {
                "must": [
                    {
                        "match_phrase": {
                            "probe_id": {
                                "query": "probe id"
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "agg_by_hours": {
                "terms": {
                    "field": "hours",
                    "size": 24,
                    "order": {
                        "_term": "desc"
                    }
                }
            }
        }
    }```

validate query API only supports the query section not the aggs section. Nothing else, not even _source, sort, from and size. You can also see this in the source code.

如果您将 ?explain=true 添加到您的 URL,您将看到如下解释:

{
  "valid" : false,
  "error" : "org.elasticsearch.common.ParsingException: request does not support [aggs]"
}

所以您绝对可以验证您的查询,但只需要 query 部分就可以了。