# Bucket Sort on non-numeric (keyword) field

**URL:** <https://discuss.elastic.co/t/bucket-sort-on-non-numeric-keyword-field/204041>\
**Category:** Elasticsearch\
**Created:** [October 17, 2019, 11:44am UTC](https://discuss.elastic.co/t/bucket-sort-on-non-numeric-keyword-field/204041 "2019-10-17T11:44:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mateuszjarzyna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mateuszjarzyna/32/56100_2.png) [@mateuszjarzyna](https://discuss.elastic.co/u/mateuszjarzyna)\
**Post date:** [October 17, 2019, 11:44am UTC](https://discuss.elastic.co/t/bucket-sort-on-non-numeric-keyword-field/204041/1 "2019-10-17T11:44:11Z")

</div>

I have index with products, each product has `category` and `category_owner`. I'd like to show on my GUI table with all categories sorted by category\_owner, but table must be pageable.

I know there is a way to sort (and paging) aggregations using `bucket_sort`. Example

```auto
GET products/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "unique_categories": {
      "terms": {
        "field": "category",
        "size": 1000
      },
      "aggs": {
        "inner": {
          "avg": {
            "field": "price"
          }
        },
        "soring_agg": {
          "bucket_sort": {
            "sort": [
              {
                "inner": {"order": "asc"}
              }
            ],
            "from": 1,
            "size": 1
          }
        }
      }
    }
  }
}

```

But is there any way to sort on the non-number field? I can create `term` aggregation with size 1 or `top_hists` search, for example

```auto
      "aggs": {
        "inner": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "@timestamp": {"order": "desc"}
              }
            ],
            "_source": {
              "includes": ["category_owner"]
            }
          }
        },
        "soring_agg": {
          "bucket_sort": {
            "sort": [
              {
                "inner": {"order": "asc"}
              }
            ]
          }
        }
      }

```

Bu I've got the error `buckets_path must reference either a number value or a single value numeric metric aggregation, got: [InternalTopHits] at aggregation [inner]`

`terms` aggregation also does not works

```auto
        "inner": {
          "terms": {
            "field": "category_owner",
            "size": 1
          }
        },

```

`buckets_path must reference either a number value or a single value numeric metric aggregation, got: [StringTerms] at aggregation [inner]`

Is there a way to create pagination on unique categories sorted by owner?

EDIT:  
I can create all uniqe categories for each owner but I have to sort and paging it manually in my app ☹

```auto
  "aggs": {
    "owners": {
      "terms": {
        "field": "category_owner",
        "size": 100000
      },
      "aggs": {
        "categories": {
          "terms": {
            "field": "category",
            "size": 1000
          }
        }
      }
    }
  }

```

---

<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:** [November 14, 2019, 11:44am UTC](https://discuss.elastic.co/t/bucket-sort-on-non-numeric-keyword-field/204041/2 "2019-11-14T11:44:24Z")

</div>

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