# ElasticSearch 2 bucket level sorting

**URL:** <https://discuss.elastic.co/t/elasticsearch-2-bucket-level-sorting/28330>\
**Category:** Elasticsearch\
**Created:** [August 31, 2015, 7:02am UTC](https://discuss.elastic.co/t/elasticsearch-2-bucket-level-sorting/28330 "2015-08-31T07:02:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepender\_Singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepender_singla/32/4485_2.png) [@Deepender\_Singla](https://discuss.elastic.co/u/Deepender_Singla)\
**Post date:** [August 31, 2015, 7:02am UTC](https://discuss.elastic.co/t/elasticsearch-2-bucket-level-sorting/28330/1 "2015-08-31T07:02:23Z")

</div>

Hello,

I am stuck on this from last two days. The mapping of database is this:

```
    {
   "users": {
      "mappings": {
         "user": {
            "properties": {
              credentials": {
                  "type": "nested",
                  "properties": {
                     "achievement_id": {
                        "type": "string"
                     },
                     "percentage_completion": {
                        "type": "integer"
                     }
                  }
               },
               "current_location": {
                  "type": "geo_point"
               },
             "locations": {
               "type": "geo_point"
         }
            }
         }
      }
   }

```

Now In the mapping, You can see there are two geo-distance fields one is current\_location and other is locations. Now I want to sort user based on credentials.percentage\_completion which is a nested field. This work fine for example this query,  
Example Query:

```
GET /users/user/_search?size=23
{
  "sort": [
    {
      "credentials.percentage_completion": {
        "order": "desc",
        "missing": "_last"
      }
    },
 "_score"
  ],
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "100000000km",
          "user.locations": {
            "lat": 19.77,
            "lon": 73
          }
        }
      }
    }
  }
}

```

I want to change sorting order made into buckets, the desired order is first show all the people who are at 100KM radius of user.current\_location and sort them according to credentials.percentage\_completion and then rest of users sorted again by credentials.percentage\_completion.

I tried putting conditional in sorting and made it multilevel but that will not work because only nested can have filters and that on nested fields child only.

I thought I can use \_score for sorting and give more relevance to people who are under 1000 km but geo-distance is a filter, I don't seem to find any way to give relevance in filter.

Is there anything I am missing here , any help would be great.

Thanks

---

<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 5, 2017, 11:53pm UTC](https://discuss.elastic.co/t/elasticsearch-2-bucket-level-sorting/28330/2 "2017-07-05T23:53:06Z")

</div>


