JavaEar 专注于收集分享传播有价值的技术资料

Unknown key for a START_OBJECT in a multiple aggregations elasticsearch

I'm trying to build a query allowing me to make multiple aggregations (on the same level, not sub aggregations) on a single query. Here's the request I'm sending :

{
    "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.*"
                    }
                }
            }
        }
    }
}

However, I'm getting the following error when executing this query :

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

I've been trying to change this query in many forms to make it works but I always end up with this error. It seems really strange to me as this query seems compatible with the format specified there : ES documentation.

Maybe there is something specific about terms aggregations but I haven't been able to sort it out.

2个回答

    最佳答案
  1. The error is in your include settings, which should simply be strings

        "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
                }
            }
        }
    
  2. 参考答案2
  3. You have trailing commas after doc_id and after closing array tag for must, your query should look like this

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