# Sort aggregation by other query

**URL:** <https://discuss.elastic.co/t/sort-aggregation-by-other-query/136433>\
**Category:** Elasticsearch\
**Created:** [June 19, 2018, 7:14am UTC](https://discuss.elastic.co/t/sort-aggregation-by-other-query/136433 "2018-06-19T07:14:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihalikv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihalikv/32/26793_2.png) [@mihalikv](https://discuss.elastic.co/u/mihalikv)\
**Post date:** [June 19, 2018, 7:14am UTC](https://discuss.elastic.co/t/sort-aggregation-by-other-query/136433/1 "2018-06-19T07:14:56Z")

</div>

Hi,

i want to ask you if it is possible to do something like this:  
**Mapping**

```
{
    "mappings": {
        "test": {
            "properties": {
                "title": {
                    "type": "text"
                },
                "categories": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword",
                            "index": "true"
                        }
                    }
                }
            }
        }
    }
}

```

**Data**

```
POST test_index/test/
{
    "title" : "title 0",
    "categories" : ["category 0", "category 1"]
}

POST test_index/test/
{
    "title" : "title 1",
    "categories" : ["category 0", "category 2"]
}

```

**Query**

```
{
  "size": 5,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "fields": [
              "name",
              "categories"
            ],
            "query": "category 1"
          }
        }
      ]
    }
  },
  "aggs": {
    "services": {
      "terms": {
        "field": "categories.raw",
        "size": 10
      }
    }
  }
}

```

**Result**

```
"hits": {
    "total": 2,
    "max_score": 0.68324494,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "AWQTm9qnWteqYLKtTuAR",
        "_score": 0.68324494,
        "_source": {
          "title": "title 0",
          "categories": [
            "category 0",
            "category 1"
          ]
        }
      },
      {
        "_index": "test_index",
        "_type": "test",
        "_id": "AWQTm-DZWteqYLKtTuAS",
        "_score": 0.39556286,
        "_source": {
          "title": "title 1",
          "categories": [
            "category 0",
            "category 2"
          ]
        }
      }
    ]
  },
  "aggregations": {
    "categories": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "category 0",
          "doc_count": 2
        },
        {
          "key": "category 1",
          "doc_count": 1
        },
        {
          "key": "category 2",
          "doc_count": 1
        }
      ]
    }
  }

```

I know that aggs are default sorted by _doc\_count_, but i want to know if it's possible somehow sort aggregeation by some inner score. Use case is mixed search where i have listed articles and also categories. When user type _category 1_, he maybe expect search just in **categories** and _category 1_ should be first.

Is it some option for this or maybe solution is combining multiple queryes ?

---

<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:** [July 17, 2018, 7:15am UTC](https://discuss.elastic.co/t/sort-aggregation-by-other-query/136433/2 "2018-07-17T07:15:01Z")

</div>

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