多个聚合elasticsearch

时间:2017-04-25 08:27:07

标签: json search elasticsearch aggregation

我正在尝试构建一个查询,允许我在单个查询上进行多个聚合(在同一级别,而不是子聚合)。这是我发送的请求:

{
    "index": "index20",
    "type": "arret",
    "body": {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "multi_match": {
                            "query": "anim fore",
                            "analyzer": "query_analyzer",
                            "type": "cross_fields",
                            "fields": [
                                "doc_id"
                            ],
                            "operator": "and"
                        }
                    }
                ]
            }
        },
        "aggs": {
            "anim_fore": {
                "terms": {
                    "field": "suggest_keywords.autocomplete",
                    "order": {
                        "_count": "desc"
                    },
                    "include": {
                        "pattern": "anim.*fore.*"
                    }
                }
            },
            "fore": {
                "terms": {
                    "field": "suggest_keywords.autocomplete",
                    "order": {
                        "_count": "desc"
                    },
                    "include": {
                        "pattern": "fore.*"
                    }
                }
            }
        }
    }
}

但是,执行此查询时出现以下错误:

Error: [parsing_exception] Unknown key for a START_OBJECT in [fore]., with { line=1 & col=1351 }

我一直在尝试以多种形式更改此查询以使其正常工作,但我总是最终遇到此错误。这对我来说似乎很奇怪,因为这个查询似乎与那里指定的格式兼容:ES documentation

可能存在关于术语聚合的具体内容但我无法对其进行排序。

2 个答案:

答案 0 :(得分:2)

错误在include设置中,应该只是字符串

    "aggs": {
        "anim_fore": {
            "terms": {
                "field": "suggest_keywords.autocomplete",
                "order": {
                    "_count": "desc"
                },
                "include": "anim.*fore.*"              <--- here
            }
        },
        "fore": {
            "terms": {
                "field": "suggest_keywords.autocomplete",
                "order": {
                    "_count": "desc"
                },
                "include": "fore.*"                    <--- and here
            }
        }
    }

答案 1 :(得分:-1)

您在doc_id之后有逗号逗号,在关闭must的数组标记后,您的查询应如下所示

"must": [
    {
        "multi_match": {
            "query": "anim fore",
            "analyzer": "query_analyzer",
            "type": "cross_fields",
            "fields": [
                "doc_id" // You have trailing comma here
            ],
            "operator": "and"
        }
    }
] // And here