使用elastic4s返回嵌套字段

时间:2015-08-03 18:24:11

标签: scala elasticsearch elastic4s

我使用嵌套的位置对象存储数据,并且无法弄清楚如何获取elastic4s作为搜索结果的一部分返回位置。我有数据查询(从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" ]
  }
}

我希望看到“位置”,但不要。有谁知道我如何指定字段,以便我可以实际获得位置?

1 个答案:

答案 0 :(得分:1)

您应该尝试使用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)
}