Elasticsearch, sort aggs according to sibling fields

Elasticsearch v7.5

Hello and good day!

We have 2 indices named socialmedia and influencers

Sample contents:

socialmedia:

{
    '_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

{
    '_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

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:

"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!

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