# Aggregation order by prefix

**URL:** <https://discuss.elastic.co/t/aggregation-order-by-prefix/157988>\
**Category:** Elasticsearch\
**Created:** [November 23, 2018, 10:33am UTC](https://discuss.elastic.co/t/aggregation-order-by-prefix/157988 "2018-11-23T10:33:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![3ms1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/3ms1/32/54472_2.png) [@3ms1](https://discuss.elastic.co/u/3ms1)\
**Post date:** [November 23, 2018, 10:33am UTC](https://discuss.elastic.co/t/aggregation-order-by-prefix/157988/1 "2018-11-23T10:33:23Z")

</div>

Hi,

How I can get sorted bucket by prefix?

For example, score it is higher for "Buffalo" and but I want results with "Fal..." in the beginning to be on top.

If I set **include** in terms then Buffalo won't appear 😕

Should I achieve this using some sort of script?

Thanks

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [November 30, 2018, 9:18pm UTC](https://discuss.elastic.co/t/aggregation-order-by-prefix/157988/2 "2018-11-30T21:18:50Z")

</div>

Can you share the query you are using? Are you trying to improve the ranking of search hits, or the sorting of aggregation results?

If you're trying to order the bucket results for display purposes, the `bucket_sort` pipeline agg may do what you need

---

<div class="post-metadata">

**Author:** ![3ms1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/3ms1/32/54472_2.png) [@3ms1](https://discuss.elastic.co/u/3ms1)\
**Post date:** [December 1, 2018, 12:20am UTC](https://discuss.elastic.co/t/aggregation-order-by-prefix/157988/3 "2018-12-01T00:20:19Z")

</div>

Sure, below you may find the agg I use.

Because there are 4 buckets I can not do _sorting_ and _pagination_.

```json
  "aggs": {
    "prefix": {
      "filter": {
        "prefix": {
          "name": {
            "value": "in"
          }
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "include": "in.*",
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    },
    "second": {
      "filter": {
        "wildcard": {
          "name": "* in*"
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    },
    "middle": {
      "filter": {
        "wildcard": {
          "name": "*in*"
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    },
    "suffix": {
      "filter": {
        "wildcard": {
          "name": "*in"
        }
      },
      "aggs": {
        "platforms": {
          "terms": {
            "field": "name",
            "size": 100,
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "platform": {
              "top_hits": {
                "_source": true,
                "size": 1
              }
            }
          }
        }
      }
    }
  }

```

---

<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:** [December 29, 2018, 12:20am UTC](https://discuss.elastic.co/t/aggregation-order-by-prefix/157988/4 "2018-12-29T00:20:20Z")

</div>

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