# Ordering aggregations by geo distance and score

**URL:** <https://discuss.elastic.co/t/ordering-aggregations-by-geo-distance-and-score/124459>\
**Category:** Elasticsearch\
**Created:** [March 18, 2018, 8:55pm UTC](https://discuss.elastic.co/t/ordering-aggregations-by-geo-distance-and-score/124459 "2018-03-18T20:55:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![woshitom](https://avatars.discourse-cdn.com/v4/letter/w/a587f6/32.png) [@woshitom](https://discuss.elastic.co/u/woshitom)\
**Post date:** [March 18, 2018, 8:55pm UTC](https://discuss.elastic.co/t/ordering-aggregations-by-geo-distance-and-score/124459/1 "2018-03-18T20:55:15Z")

</div>

My mapping is the following:

```
PUT places
{
  "mappings": {
    "test": {
      "properties": {
        "id_product": { "type": "keyword" },
        "id_product_unique": { "type": "integer" },
        "location": { "type": "geo_point" },
        "suggest": {
          "type": "text"
        },
        "active": {"type": "boolean"}
      }
    }
  }
}

POST places/test
{
   "id_product" : "A",
   "id_product_unique": 1,
   "location": {
      "lat": 1.378446,
      "lon": 103.763427
   },
   "suggest": ["coke","zero"],
   "active": true
}

POST places/test
{
   "id_product" : "A",
   "id_product_unique": 2,
   "location": {
      "lat": 1.878446,
      "lon": 108.763427
   },
   "suggest": ["coke","zero"],
   "active": true
}

POST places/test
{
   "id_product" : "B",
   "id_product_unique": 3,
   "location": {
      "lat": 1.478446,
      "lon": 104.763427
   },
   "suggest": ["coke"],
   "active": true
}

POST places/test
{
   "id_product" : "C",
   "id_product_unique": 4,
   "location": {
      "lat": 1.218446,
      "lon": 102.763427
   },
   "suggest": ["coke","light"],
   "active": true
}

```

In my example there is 2 can of coke zero ("id\_product\_unique" = 1 and 2), 1 can of coke ("id\_product\_unique" = 3) and one can of coke light ("id\_product\_unique" = 4)

All these cans are in different locations.

An "id\_product" is not unique as an exact same "can of coke" can be sold in two different locations (ex "id\_product\_unique" = 1 and 2).

Only "id\_product\_unique" and "location" change from a "can of coke" to an other one (2 same "can of coke" have the same fields "suggest" and "id\_product" but not the same "id\_product\_unique" and "location").

My goal is to search for a product from a given GPS location, and display a unique result by id\_product (the closest one):

```
POST /places/_search?size=0
{
  "aggs" : {
    "group-by-type" : {
      "terms" : { "field" : "id_product"},
      "aggs": {
        "min-distance": {
          "top_hits": {
            "sort": {
              "_script": { 
                "type": "number",
                "script": {
                  "source": "def x = doc['location'].lat; def y = doc['location'].lon; return Math.abs(x-1.178446) + Math.abs(y-101.763427)",
                  "lang": "painless"
                },
                "order": "asc"
              }
            },
            "size" : 1
          }
        }
      }
    }
  }
}

```

From this list of result I'd like now to apply a should query and to re-order my list of result by computed score. I tried the following:

```
POST /places/_search?size=0
{
  "query" : {
    "bool": {
      "filter": {"term" : { "active" : "true" }},
      "should": [
        {"match" : { "suggest" : "coke" }},
        {"match" : { "suggest" : "light" }}
      ]
    }
  },
  "aggs" : {
    "group-by-type" : {
      "terms" : { "field" : "id_product"},
      "aggs": {
        "min-distance": {
          "top_hits": {
            "sort": {
              "_script": { 
                "type": "number",
                "script": {
                  "source": "def x = doc['location'].lat; def y = doc['location'].lon; return Math.abs(x-1.178446) + Math.abs(y-101.763427)",
                  "lang": "painless"
                },
                "order": "asc"
              }
            },
            "size" : 1
          }
        }
      }
    }
  }
}

```

But I cannot figure how to replace the distance sort score by the doc score.

Any help would be great.

---

<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:** [April 15, 2018, 8:55pm UTC](https://discuss.elastic.co/t/ordering-aggregations-by-geo-distance-and-score/124459/2 "2018-04-15T20:55:27Z")

</div>

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