所以我有两个集合,一个是城市,邮政编码是一个属性,一个是邮政编码,他们的纬度&经度。
我想要返回离坐标最近的城市。使用地理索引这很容易,但我遇到的问题是同一个城市被多次返回,有时可能是第一和第三最接近,因为我搜索的邮政编码与另一个相邻城市。
城市示例数据:
[
{
"_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
答案 0 :(得分:2)
查询中的第一个FOR
将使用地理索引,并可能返回少量文档(只是指定位置周围的邮政编码)。
第二个FOR
将查找每个找到的邮政编码的城市。这可能是一个问题,具体取决于cities.postalCodes
和cities.countryCode
上是否存在索引。如果没有,则第二个FOR
必须在每次涉及cities
集合时对其进行全面扫描。这将是低效的。因此,可以在这两个属性上创建一个索引,如下所示:
db.cities.ensureIndex({ type: "hash", fields: ["countryCode", "postalCodes[*]"] });
FOR
可以COLLECT
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
:
db._explain(queryString)
这会缩短查询字符串,但我认为它可能无效,因为第三个n
将使用主索引来查找城市文档,即O(1)。
通常,如果对使用索引的查询有疑问,可以使用n
来显示查询将使用哪些索引。