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

**URL:** <https://discuss.elastic.co/t/how-to-show-distance-from-some-point-and-apply-decay-funtion-to-it/178182>\
**Category:** Elasticsearch\
**Created:** [April 24, 2019, 8:20am UTC](https://discuss.elastic.co/t/how-to-show-distance-from-some-point-and-apply-decay-funtion-to-it/178182 "2019-04-24T08:20:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enp/32/3825_2.png) [@enp](https://discuss.elastic.co/u/enp)\
**Post date:** [April 24, 2019, 8:20am UTC](https://discuss.elastic.co/t/how-to-show-distance-from-some-point-and-apply-decay-funtion-to-it/178182/1 "2019-04-24T08:20:22Z")

</div>

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?

---

<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:** [May 22, 2019, 8:20am UTC](https://discuss.elastic.co/t/how-to-show-distance-from-some-point-and-apply-decay-funtion-to-it/178182/2 "2019-05-22T08:20:29Z")

</div>

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