Aggregation in elasticsearch with specific parameter
<p>I have bulk documents in elasticsearch and as an example I have taken the elasticsearch documentation example as banks<br/></p>

<pre><code>   {
"_index": "bank",
"_type": "account",
"_id": "25",
"_score": 1,
"_source": {
"account_number": 25,
"balance": 40540,
"firstname": "Virginia",
"lastname": "Ayala",
"age": 39,
"gender": "F",
"address": "171 Putnam Avenue",
"employer": "Filodyne",
"email": "virginiaayala@filodyne.com",
"city": "Nicholson",
"state": "PA"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "44",
"_score": 1,
"_source": {
"account_number": 44,
"balance": 34487,
"firstname": "Aurelia",
"lastname": "Harding",
"age": 37,
"gender": "M",
"address": "502 Baycliff Terrace",
"employer": "Orbalix",
"email": "aureliaharding@orbalix.com",
"city": "Yardville",
"state": "DE"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "99",
"_score": 1,
"_source": {
"account_number": 99,
"balance": 47159,
"firstname": "Ratliff",
"lastname": "Heath",
"age": 39,
"gender": "F",
"address": "806 Rockwell Place",
"employer": "Zappix",
"email": "ratliffheath@zappix.com",
"city": "Shaft",
"state": "ND"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "119",
"_score": 1,
"_source": {
"account_number": 119,
"balance": 49222,
"firstname": "Laverne",
"lastname": "Johnson",
"age": 28,
"gender": "F",
"address": "302 Howard Place",
"employer": "Senmei",
"email": "lavernejohnson@senmei.com",
"city": "Herlong",
"state": "DC"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "126",
"_score": 1,
"_source": {
"account_number": 126,
"balance": 3607,
"firstname": "Effie",
"lastname": "Gates",
"age": 39,
"gender": "F",
"address": "620 National Drive",
"employer": "Digitalus",
"email": "effiegates@digitalus.com",
"city": "Blodgett",
"state": "MD"
}
}
</code></pre>

<p><br/>
Now there is a field called state and price in each document.<br/> How can I write a query for which it returns only the results that contain distinct state with sort order as balance in asc order.<br/></p>

<p>I was trying with terms aggregation but of no use.<br/>
UPDATE <br/></p>

<pre><code>POST _search
{
   "size": 0,
   "aggs": {
      "states": {
          "terms": {
              "field": "state"
          },
          "aggs": {
              "balances": {
                  "top_hits": {
                      "from" : 0,
                      "size": 1,
                      "sort": {"balance": "asc"}
                  }
              }
          }
      }
   }
}
</code></pre>

<p><br/> now for this query i'll be returned with all top-hits with price sorted in that key "state". But what i want is a sorted results w.r.t balance and with unique state fields.<br/>
For the above query, i am getting response as follows<br/></p>

<pre><code>"buckets": [
            {
               "key": "tx",
               "doc_count": 30,
               "balances": {
                  "hits": {
                     "total": 30,
                     "max_score": null,
                     "hits": [
                        {
                           "_index": "bank",
                           "_type": "account",
                           "_id": "161",
                           "_score": null,
                           "_source": {
                              "account_number": 161,
                              "balance": 4659,
                              "firstname": "Doreen",
                              "lastname": "Randall",
                              "age": 37,
                              "gender": "F",
                              "address": "178 Court Street",
                              "employer": "Calcula",
                              "email": "doreenrandall@calcula.com",
                              "city": "Belmont",
                              "state": "TX"
                           },
                           "sort": [
                              4659
                           ]
                        }
                     ]
                  }
               }
            },
            {
               "key": "md",
               "doc_count": 28,
               "balances": {
                  "hits": {
                     "total": 28,
                     "max_score": null,
                     "hits": [
                        {
                           "_index": "bank",
                           "_type": "account",
                           "_id": "527",
                           "_score": null,
                           "_source": {
                              "account_number": 527,
                              "balance": 2028,
                              "firstname": "Carver",
                              "lastname": "Peters",
                              "age": 35,
                              "gender": "M",
                              "address": "816 Victor Road",
                              "employer": "Housedown",
                              "email": "carverpeters@housedown.com",
                              "city": "Nadine",
                              "state": "MD"
                           },
                           "sort": [
                              2028
                           ]
                        }
                     ]
                  }
               }
            },
            {
               "key": "id",
               "doc_count": 27,
               "balances": {
                  "hits": {
                     "total": 27,
                     "max_score": null,
                     "hits": [
                        {
                           "_index": "bank",
                           "_type": "account",
                           "_id": "402",
                           "_score": null,
                           "_source": {
                              "account_number": 402,
                              "balance": 1282,
                              "firstname": "Pacheco",
                              "lastname": "Rosales",
                              "age": 32,
                              "gender": "M",
                              "address": "538 Pershing Loop",
                              "employer": "Circum",
                              "email": "pachecorosales@circum.com",
                              "city": "Elbert",
                              "state": "ID"
                           },
                           "sort": [
                              1282
                           ]
                        }
                     ]
                  }
               }
            },
</code></pre>

<p>which is not in price sorted.</p>