How to show distance from some point and apply decay funtion to it

Hi,

It is possible to score destinations by distance from some point and even show something like distance itself:

  GET data/destination/_search?_source=city_name,location
  {
    "query": {
      "function_score": {
        "functions": [
          {
            "exp": {
              "location": {
                "origin": {
                  "lat": 51.50853,
                  "lon": -0.12574
                },
                "offset": "1km",
                "scale": "100km"
              }
            }
          }
        ]
      }
    },
    "script_fields": {
      "distance": {
        "script": "doc['location'].arcDistance(-0.12574,51.50853)"
      }
    }
  }

I see something like this as result:

  {
    "_index": "data",
    "_type": "destination",
    "_id": "477",
    "_score": 1,
    "_source": {
      "city_name": "London",
      "location": {
        "lon": "-0.12574",
        "lat": "51.50853"
      }
    },
    "fields": {
      "distance": [
        7492509.394966006
      ]
    }
  },
  {
    "_index": "data",
    "_type": "destination",
    "_id": "767",
    "_score": 0.90021527,
    "_source": {
      "city_name": "Kingston-Upon-Thames",
      "location": {
        "lon": "-0.300689",
        "lat": "51.41233"
      }
    },
    "fields": {
      "distance": [
        7497185.381104352
      ]
    }
  },
  {
    "_index": "data",
    "_type": "destination",
    "_id": "5696",
    "_score": 0.692497,
    "_source": {
      "city_name": "Henley-on-Thames",
      "location": {
        "lon": "-0.90503",
        "lat": "51.53758"
      }
    },
    "fields": {
      "distance": [
        7540166.626127409
      ]
    }
  }

My first problem is fields.distance value - it looks like not real distance in kilometers or even in meters. Maybe I need to convert result of arcDistance or use another function?

Another problem is scoring with decay function by distance instead of location. Is it possible? I tried to use 'distance' or 'fields.distance' instead of 'location' and some integer value for origin, but I got error "unknown field". Maybe another way exists?

I saw "geo_distance" filters support, but this is just filter without support of decay functions, right?

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