Ordering aggregations by geo distance and score

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.

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