# Function\_score with nested geo\_location

**URL:** <https://discuss.elastic.co/t/function-score-with-nested-geo-location/24976>\
**Category:** Elasticsearch\
**Created:** [July 6, 2015, 7:07pm UTC](https://discuss.elastic.co/t/function-score-with-nested-geo-location/24976 "2015-07-06T19:07:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeffh/32/821_2.png) [@JeffH](https://discuss.elastic.co/u/JeffH)\
**Post date:** [July 6, 2015, 7:07pm UTC](https://discuss.elastic.co/t/function-score-with-nested-geo-location/24976/1 "2015-07-06T19:07:11Z")

</div>

I'm trying to implement function\_score query to blend full-text query with geo location--similar to what's described here: [https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html). however my documents' geo\_location is in a locations array (locations.geo\_location) which has a nested mapping. here's some "sense" which demonstrates the problem:

```
POST my_index
{
  "settings": {"number_of_shards": 1},
  "mappings": {
     "person": {
        "properties": {
           "name": {"type": "string"},
           "locations": {
              "type": "nested",
              "properties": {
                 "city": {"type": "string"},
                 "geo_location": {"type": "geo_point"},
                 "state": {"type": "string"}
              }
           }
        }
     }
  }
}

PUT my_index/person/1
{
  "name": "john doe",
  "locations": [
      {"city": "foo", "state": "bar", "geo_location": [-87, 41]}
    ]
}

PUT my_index/person/2
{
  "name": "jane doe",
  "locations": [
      {"city": "foo", "state": "bar", "geo_location": [-88, 42]}
    ]
}

GET my_index/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "doe"
        }
      },
      "functions": [
        {"gauss": {
          "locations.geo_location": {
            "origin": { "lat": 42, "lon": -88 },
            "scale": "100km"
          }
        }}
      ]
    }
  }
}

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.8048013,
        "hits": [
            {
                "_index": "my_index",
                "_type": "person",
                "_id": "1",
                "_score": 0.8048013,
                "_source": {
                    "name": "john doe",
                    "locations": [
                        {
                            "city": "foo",
                            "state": "bar",
                            "geo_location": [
                                -87,
                                41
                            ]
                        }
                    ]
                }
            },
            {
                "_index": "my_index",
                "_type": "person",
                "_id": "2",
                "_score": 0.8048013,
                "_source": {
                    "name": "jane doe",
                    "locations": [
                        {
                            "city": "foo",
                            "state": "bar",
                            "geo_location": [
                                -88,
                                42
                            ]
                        }
                    ]
                }
            }
        ]
    }
}

```

The score is same for both results though I was hoping doc #2 would be ranked higher. So it appears the nested mapping is causing problems with the above query. FWIW after dropping the ("type": "nested") from the locations mapping results are returned as expected. Is it possible to wrap the gauss locations.geo\_location function in a nested wrapper, and what would that look like? Thanks for your time and consideration!

---

<div class="post-metadata">

**Author:** ![rhbecker](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@rhbecker](https://discuss.elastic.co/u/rhbecker)\
**Post date:** [December 2, 2016, 3:28am UTC](https://discuss.elastic.co/t/function-score-with-nested-geo-location/24976/2 "2016-12-02T03:28:53Z")

</div>

I'm looking to do something very similar (but with a date, rather than a location). The original poster laid out a very good example mapping and document set, so I'll ask my questions based on that.

My goal is to return a set of documents based on a **query that references properties at the document root** , then apply a **decay-based function score** to the resulting set, but **using a nested object property**.

If I remove the nesting from the equation by adding a `nonnested_geo_location` property to the document root, exactly like `locations.geo_location`, in terms of mapping and values, the [function score documentation page](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html) provides enough examples to make the approach clear:

```auto
GET my_index/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "doe"
        }
      },
      "functions": [
        {
          "gauss": {
            "nonnested_geo_location": {
              "origin": { "lat": 42, "lon": -88 },
              "scale": "100km"
            }
          }
        }
      ]
    }
  }
}

```

By way of examples provided in [an answer to a different question](https://discuss.elastic.co/t/nested-value-on-function-score/29935/2), I was able to figure out how to apply the decay function scoring based on a nested object property, but without a query first running to potentially narrow the set of documents to which the scoring is applied:

```auto
GET /my_index/_search
{
  "_source": ["name", "locations"],
  "query": {
    "nested": {
      "path": "locations",
      "query": {
        "function_score":{
          "functions": [
            {
              "gauss": {
                "locations.geo_location": {
                  "origin": { "lat": 42, "lon": -88 },
                  "scale": "100km"
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

I've been unable to find any examples that marry these 2 needs. **How do I formulate a query that references properties from the parent object in order to narrow the set of documents to which a decay function score that references a nested object property is then applied?**

---

<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:** [July 5, 2017, 10:05pm UTC](https://discuss.elastic.co/t/function-score-with-nested-geo-location/24976/3 "2017-07-05T22:05:19Z")

</div>


