Elastic decay function not working on nested field

I have the following configuration:

mapping

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

docs

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:


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?

Hi @AthanatiusC

You must use Nested Query. So, put the function inside nested query:

{
  "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"
        }
      }
    }
  }
}
1 Like

thank you very much!

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