首页 文章

在AQL中返回不同且已排序的查询

提问于
浏览
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"
  }
]

postalCodes示例数据:

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

1 回答

  • 2

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

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

    FOR 可以在 c._id 但不是 c 时完全删除:

    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) 来显示查询将使用哪些索引 .

相关问题