# Elastic decay function not working on nested field

**URL:** <https://discuss.elastic.co/t/elastic-decay-function-not-working-on-nested-field/324517>\
**Category:** Elasticsearch\
**Created:** [February 2, 2023, 9:05am UTC](https://discuss.elastic.co/t/elastic-decay-function-not-working-on-nested-field/324517 "2023-02-02T09:05:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AthanatiusC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/athanatiusc/32/116678_2.png) [@AthanatiusC](https://discuss.elastic.co/u/AthanatiusC)\
**Post date:** [February 2, 2023, 9:05am UTC](https://discuss.elastic.co/t/elastic-decay-function-not-working-on-nested-field/324517/1 "2023-02-02T09:05:36Z")

</div>

I have the following configuration:

# mapping

```auto
put geo_test
{
  "mappings":{ 
    "properties":{
      "name":{
        "type":"text"
      },
      "location":{
        "type":"nested",
        "properties":{
          "name":{
            "type":"text"
          },
          "coordinates":{
            "type":"geo_point"
          }
        }
      }
    }
  }
}

```

# docs

```auto
put geo_test/_doc/1
{
  "name":"A",
  "location":{
    "name":"A",
    "coordinates":[106.759,-6.16833]
  }
}

put geo_test/_doc/2
{
  "name":"B",
  "location":{
    "name":"B",
    "coordinates":[106.774,-6.12143]
  }
}

```

And when i tried to use gaussian decay function to sort the score based on nearest to origin its not resulting as i wanted it to be:

```auto

get geo_test/_search
{
  "query":{
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "gauss": {
            "location.coordinates": {
              "origin": {
                "lat":106.774,
                "lon":-6.12143
              },
              "scale":"5km",
              "offset": "1km", 
              "decay": 0.5
            }
          },
          "weight": 10
        }
      ],
      "score_mode": "sum", 
      "boost_mode": "replace"
    }
  }
}

```

In the query above i set the origin exactly as doc 2 geopoints, but the result is giving A as the first index and B as the second index. Why did this happend?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 2, 2023, 12:52pm UTC](https://discuss.elastic.co/t/elastic-decay-function-not-working-on-nested-field/324517/2 "2023-02-02T12:52:48Z")

</div>

Hi @AthanatiusC

You must use [Nested Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html). So, put the function inside nested query:

```auto
{
  "query": {
    "nested": {
      "path": "location",
      "query": {
        "function_score": {
          "query": {
            "match_all": {}
          },
          "functions": [
            {
              "gauss": {
                "location.coordinates": {
                  "origin": {
                    "lat": -6.12143,
                    "lon": 106.774
                  },
                  "scale": "5km",
                  "offset": "1km",
                  "decay": 0.1
                }
              },
              "weight": 10
            }
          ],
          "score_mode": "sum",
          "boost_mode": "max"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![AthanatiusC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/athanatiusc/32/116678_2.png) [@AthanatiusC](https://discuss.elastic.co/u/AthanatiusC)\
**Post date:** [February 16, 2023, 6:58am UTC](https://discuss.elastic.co/t/elastic-decay-function-not-working-on-nested-field/324517/3 "2023-02-16T06:58:19Z")

</div>

thank you very much!

---

<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:** [March 16, 2023, 6:58am UTC](https://discuss.elastic.co/t/elastic-decay-function-not-working-on-nested-field/324517/4 "2023-03-16T06:58:42Z")

</div>

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