return 使用 elastic4s 的嵌套字段

return nested fields using elastic4s

我的数据存储在嵌套位置对象中,但无法弄清楚如何将 elastic4s 获取到 return 位置作为搜索结果的一部分。我的数据在查询时(从 REST 端点)看起来像这样:

{
    "_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"
        }
    }
}

当我尝试像这样使用 elastic4s 查询它时:

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)
}

我得到这样的结果:

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

我希望看到 "location" 但没有看到的地方。有谁知道我如何指定字段以便我可以实际获取位置?

您应该尝试使用 source filtering,如下所示。注意使用 sourceInclude 而不是 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)
}