0

I have data stored with a nested location object and can't figure out how to get elastic4s to return the location as part of the result of a search. I have data that when queried (from the REST endpoint) looks like this:

{
    "_index": "us_large_cities",
    "_type": "city",
    "_id": "AU7ke-xU_N_KRYZ5Iii_",
    "_score": 1,
    "_source": {
        "city": "Oakland",
        "state": "CA",
        "location": {
            "lat": "37.8043722",
            "lon": "-122.2708026"
        }
    }
}

When I try querying it using elastic4s like so:

search in "us_large_cities"->"city" fields("location", "city", ) query {
filteredQuery filter {
  geoPolygon("location") point(37.9, -122.31) point(37.8, -122.31) point(37.8, -122.25) point(37.9, -122.25)
}

I get back results like this:

{
  "_index" : "us_large_cities",
  "_type" : "city",
  "_id" : "AU7keH9l_N_KRYZ5Iig0",
  "_score" : 1.0,
  "fields" : {
    "city" : [ "Berkeley" ]
  }
}

Where I would expect to see "location" but don't. Does anyone know how I specify the fields so that I can actually get the location?

1 Answers1

1

You should try using source filtering instead, as shown below. Note the use of sourceInclude instead of fields.

search in "us_large_cities"->"city" sourceInclude("location", "city") query {
filteredQuery filter {
  geoPolygon("location") point(37.9, -122.31) point(37.8, -122.31) point(37.8, -122.25) point(37.9, -122.25)
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • This works! I just have to then call .getSource on the hits instead of calling .getFields etc. – Danny Hatcher Aug 03 '15 at 19:45
  • @DannyHatcher FYI source and fields are different, fields show you what it looks like after tokenization and source is the original document in json. – sksamuel Aug 22 '15 at 17:52