2

所以我有两个集合,一个包含一系列邮政编码作为属性的城市,另一个包含邮政编码及其纬度和经度。

我想返回最接近坐标的城市。使用地理索引这很容易,但我遇到的问题是多次返回同一个城市,有时它可能是第一个和第三个最接近的,因为我正在搜索与另一个城市接壤的邮政编码。

城市示例数据:

[
  {
    "_key": "30936019",
    "_id": "cities/30936019",
    "_rev": "30936019",
    "countryCode": "US",
    "label": "Colorado Springs, CO",
    "name": "Colorado Springs",
    "postalCodes": [
      "80904",
      "80927"
    ],
    "region": "CO"
  },
  {
    "_key": "30983621",
    "_id": "cities/30983621",
    "_rev": "30983621",
    "countryCode": "US",
    "label": "Manitou Springs, CO",
    "name": "Manitou Springs",
    "postalCodes": [
      "80829"
    ],
    "region": "CO"
  }
]

邮政编码示例数据:

[
  {
    "_key": "32132856",
    "_id": "postalCodes/32132856",
    "_rev": "32132856",
    "countryCode": "US",
    "location": [
      38.9286,
      -104.6583
    ],
    "postalCode": "80927"
  },
  {
    "_key": "32147422",
    "_id": "postalCodes/32147422",
    "_rev": "32147422",
    "countryCode": "US",
    "location": [
      38.8533,
      -104.8595
    ],
    "postalCode": "80904"
  },
  {
    "_key": "32172144",
    "_id": "postalCodes/32172144",
    "_rev": "32172144",
    "countryCode": "US",
    "location": [
      38.855,
      -104.9058
    ],
    "postalCode": "80829"
  }
]

以下查询有效,但作为 ArangoDB 新手,我想知道是否有更有效的方法来执行此操作:

FOR p IN WITHIN(postalCodes, 38.8609, -104.8734, 30000, 'distance')
    FOR c IN cities
        FILTER p.postalCode IN c.postalCodes AND c.countryCode == p.countryCode
        COLLECT close = c._id AGGREGATE distance = MIN(p.distance)
        FOR c2 IN cities
            FILTER c2._id == close
            SORT distance
            RETURN c2
4

1 回答 1

2

查询中的第一个FOR将使用地理索引并可能返回少量文档(仅返回指定位置周围的邮政编码)。第二个FOR将查找每个找到的邮政编码的城市。cities.postalCodes这可能是一个问题,具体取决于和上是否存在索引cities.countryCode。如果没有,那么第二个必须在每次涉及时FOR对集合进行全面扫描。cities这将是低效的。因此,可以像这样在两个属性上创建索引:

db.cities.ensureIndex({ type: "hash", fields: ["countryCode", "postalCodes[*]"] });

当不是ing而是 by时,第三个FOR可以完全删除:COLLECTc._idc

FOR p IN WITHIN(postalCodes, 38.8609, -104.8734, 30000, 'distance')
  FOR c IN cities
    FILTER p.postalCode IN c.postalCodes AND c.countryCode == p.countryCode
    COLLECT city = c AGGREGATE distance = MIN(p.distance)
    SORT distance
    RETURN city

这将缩短查询字符串,但我认为它可能对效率没有太大帮助,因为第三个FOR将使用主索引来查找城市文档,即 O(1)。

通常,当对使用索引的查询有疑问时,您可以使用db._explain(queryString)它来显示查询将使用哪些索引。

于 2016-06-20T09:53:29.470 回答