Function_score with nested geo_location


(JeffH) #1

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. 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!


#2

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 provides enough examples to make the approach clear:

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, 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:

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?


(system) #3