Home > OS >  the result of ElasticSearch aggregation Filter error
the result of ElasticSearch aggregation Filter error

Time:12-20

my searchRequest like this,I only want to get two skill_group (id = 6806,6805)aggregation result ,so I add filter both in query and aggregation . but I still get other skill_group aggregation result. the es version is 7.1

{
    "size": 0,
    "query": {
        "bool": {
            "filter": [{
                "terms": {
                    "skill_group_id": [6806, 6805],
                    "boost": 1.0
                }
            }],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "aggregations": {
        "test": {
            "filter": {
                "terms": {
                    "skill_group_id": [6806, 6805],
                    "boost": 1.0
                }
            },
            "aggregations": {
                "SKILLGROUP": {
                    "terms": {
                        "field": "skill_group_id",
                        "size": 10000,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [{
                            "_count": "desc"
                        }, {
                            "_key": "asc"
                        }],
                        "collect_mode": "breadth_first"
                    },
                    "aggregations": {
                        "WORKSTATUS": {
                            "terms": {
                                "field": "status",
                                "size": 10000,
                                "min_doc_count": 1,
                                "shard_min_doc_count": 0,
                                "show_term_doc_count_error": false,
                                "order": [{
                                    "_count": "desc"
                                }, {
                                    "_key": "asc"
                                }],
                                "collect_mode": "breadth_first"
                            }
                        }
                    }
                }
            }
        }
    }
}

the result is like this enter image description here

CodePudding user response:

If skill_group_id is an array, then aggregating on skill_group_id will aggregate on all values of that array.

What you can do in addition to the filter, is to also leverage the include setting on the terms aggregation, like this:

            "SKILLGROUP": {
                "terms": {
                    "field": "skill_group_id",
                    "size": 10000,
                    "include": [6806, 6805],            <--- add this
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [{
                        "_count": "desc"
                    }, {
                        "_key": "asc"
                    }],
                    "collect_mode": "breadth_first"
                },
  • Related