# Aggs order with a query scope, got docs not in the query scope, why?

**URL:** <https://discuss.elastic.co/t/aggs-order-with-a-query-scope-got-docs-not-in-the-query-scope-why/278230>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 9, 2021, 3:04am UTC](https://discuss.elastic.co/t/aggs-order-with-a-query-scope-got-docs-not-in-the-query-scope-why/278230 "2021-07-09T03:04:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel\_wang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_wang/32/91373_2.png) [@daniel\_wang](https://discuss.elastic.co/u/daniel_wang)\
**Post date:** [July 9, 2021, 3:04am UTC](https://discuss.elastic.co/t/aggs-order-with-a-query-scope-got-docs-not-in-the-query-scope-why/278230/1 "2021-07-09T03:04:17Z")

</div>

My es version info:

```auto
{
    "version": {
        "number": "7.7.1",
        "build_flavor": "default",
        "build_type": "tar",
        "build_hash": "ad56dce891c901a492bb1ee393f12dfff473a423",
        "build_date": "2020-05-28T16:30:01.040088Z",
        "build_snapshot": false,
        "lucene_version": "8.5.1",
        "minimum_wire_compatibility_version": "6.8.0",
        "minimum_index_compatibility_version": "6.0.0-beta1"
    }
}

```

My index info:

```auto
{
    "sh_xxx_xxx_sku_prd_20210329": {
        "aliases": {
            "sh_xxx_xxx_sku": {}
        },
        "mappings": {
            "dynamic": "false",
            "properties": {
                "id": {
                    "type": "keyword"
                },
                "price": {
                    "type": "double"
                },
                "shopId": {
                    "type": "keyword"
                },
                "shopName": {
                    "type": "text"
                },
                "supplierAttrIds": {
                    "type": "keyword"
                },
                "supplierId": {
                    "type": "integer"
                },
                "supplierProductCommissionRate": {
                    "type": "double"
                },
                "supplierProductId": {
                    "type": "integer"
                },
                "supplierProductPerformanceRate": {
                    "type": "double"
                },
                "supplierStyleAttrId": {
                    "type": "long"
                }
            }
        },
        "settings": {
            "index": {
                "refresh_interval": "30s",
                "number_of_shards": "5",
                "translog": {
                    "flush_threshold_size": "512mb",
                    "sync_interval": "60s",
                    "durability": "async"
                },
                "provided_name": "sh_xxx_xxx_sku_prd_20210329",
                "creation_date": "1617012644280",
                "number_of_replicas": "1",
                "uuid": "fIyF13TBRNmhuIchQX8M5Q",
                "version": {
                    "created": "7070199"
                }
            }
        }
    }
}

```

my search body:

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "shopName": {
                            "query": "飞帆运动"
                        }
                    }
                },
                {
                    "exists": {
                        "field": "supplierStyleAttrId"
                    }
                },
                {
                    "range": {
                        "supplierStyleAttrId": {
                            "from": 0,
                            "include_lower": false,
                            "include_upper": true,
                            "to": null
                        }
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "supplierStyleAttrId": {
            "aggregations": {
                "price": {
                    "min": {
                        "field": "price"
                    }
                },
                "r_bucket_sort": {
                    "bucket_sort": {
                        "size": 5000
                    }
                },
                "supplierProductCommissionRate": {
                    "avg": {
                        "field": "supplierProductCommissionRate"
                    }
                },
                "supplierProductPerformanceRate": {
                    "avg": {
                        "field": "supplierProductPerformanceRate"
                    }
                }
            },
            "terms": {
                "field": "supplierStyleAttrId",
                "order": [
                    {
                        "supplierProductCommissionRate": "desc"
                    }
                ],
                "size": 5000
            }
        }
    }
}

```

with the request body above, i got docs with shopName is not "飞帆运动", why can this happen?

by the with the request body below, i got the right result

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "shopName": {
                            "query": "飞帆运动"
                        }
                    }
                },
                {
                    "exists": {
                        "field": "supplierStyleAttrId"
                    }
                },
                {
                    "range": {
                        "supplierStyleAttrId": {
                            "from": 0,
                            "include_lower": false,
                            "include_upper": true,
                            "to": null
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "supplierStyleAttrId": {
            "aggregations": {
                "price": {
                    "min": {
                        "field": "price"
                    }
                },
                "r_bucket_sort": {
                    "bucket_sort": {
                        "size": 5000
                    }
                },
                "scoreMax": {
                    "max": {
                        "script": {
                            "lang": "painless",
                            "source": "_score"
                        }
                    }
                },
                "supplierProductCommissionRate": {
                    "avg": {
                        "field": "supplierProductCommissionRate"
                    }
                },
                "supplierProductPerformanceRate": {
                    "avg": {
                        "field": "supplierProductPerformanceRate"
                    }
                }
            },
            "terms": {
                "field": "supplierStyleAttrId",
                "order": [
                    {
                        "scoreMax": "desc"
                    }
                ],
                "size": 5000
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [July 9, 2021, 6:33am UTC](https://discuss.elastic.co/t/aggs-order-with-a-query-scope-got-docs-not-in-the-query-scope-why/278230/2 "2021-07-09T06:33:33Z")

</div>

Can you show an example of a shop id that was returned incorrectly?

---

<div class="post-metadata">

**Author:** ![daniel\_wang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_wang/32/91373_2.png) [@daniel\_wang](https://discuss.elastic.co/u/daniel_wang)\
**Post date:** [July 9, 2021, 7:05am UTC](https://discuss.elastic.co/t/aggs-order-with-a-query-scope-got-docs-not-in-the-query-scope-why/278230/3 "2021-07-09T07:05:15Z")

</div>

when request I got:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/e/ee0ec602a74b8fd2aa050ba5c7d62ba2fda0ef11.png)

and i search by supplierStyleAttrId got:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/e/0e2adc7cc7e779bbfa1e8ec61a5577a2a7589b48.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 6, 2021, 7:05am UTC](https://discuss.elastic.co/t/aggs-order-with-a-query-scope-got-docs-not-in-the-query-scope-why/278230/4 "2021-08-06T07:05:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
