# Elasticsearch, sort aggs according to sibling fields

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-aggs-according-to-sibling-fields/215068>\
**Category:** Elasticsearch\
**Created:** [January 15, 2020, 3:11am UTC](https://discuss.elastic.co/t/elasticsearch-sort-aggs-according-to-sibling-fields/215068 "2020-01-15T03:11:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [January 15, 2020, 3:11am UTC](https://discuss.elastic.co/t/elasticsearch-sort-aggs-according-to-sibling-fields/215068/1 "2020-01-15T03:11:32Z")

</div>

Elasticsearch v7.5

Hello and good day!

We have 2 indices named **socialmedia** and **influencers**

Sample contents:

**socialmedia:**

```auto
{
    '_id' : 1001,
    'title' : "Title 1",
    'smp_id' : 1,
},
{
    '_id' : 1002,
    'title' : "Title 2",
    'smp_id' : 2,
},
{
    '_id' : 1003,
    'title' : "Title 3",
    'smp_id' : 3,
}
//omitted other documents 

```

**influencers**

```auto
{
    '_id' : 1,
    'name' : "John",
    'smp_id' : 1,
    'smp_score' : 5
},
{
    '_id' : 2,
    'name' : "Peter",
    'smp_id' : 2,
    'smp_score' : 10
},
{
    '_id' : 3,
    'name' : "Mark",
    'smp_id' : 3,
    'smp_score' : 15
}
//omitted other documents

```

Now I have this simple query that determines which **influencer** has the most document in the **socialmedia** index

```auto
GET socialmedia/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  }, 
  "aggs": {
    "INFLUENCERS": {
      "terms": {
        "field": "smp_id.keyword"
        //smp_id is a **text** based field, that's why we have `.keyword` here
      }
    }
  }
}

```

**SAMPLE OUTPUT:**

```auto
"aggregations" : {
    "INFLUENCERS" : {
      "doc_count_error_upper_bound" : //omitted,
      "sum_other_doc_count" : //omitted,
      "buckets" : [
        {
          "key" : "1",
          "doc_count" : 87258
        },
        {
          "key" : "2",
          "doc_count" : 36518
        },
        {
          "key" : "3",
          "doc_count" : 34838
        },
      ]
  }
}

```

**OBJECTIVE:**

My query is able to sort the **influencers** according to _doc\_count_ of their posts in the **socialmedia** index, now, is there a way for us to sort the **INFLUENCERS** according to their **SMP\_SCORE**?

With that idea, **smp\_id** 3 which is Mark, should be the first one to appear since he has an **smp\_score** of 15

Thank you in advance for your help!

---

<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:** [February 12, 2020, 3:11am UTC](https://discuss.elastic.co/t/elasticsearch-sort-aggs-according-to-sibling-fields/215068/2 "2020-02-12T03:11:44Z")

</div>

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