简体   繁体   English

如何在AQL中使用NEIGHBORS?

[英]How to use NEIGHBORS in AQL?

I'm new to ArangoDB and a growing fan already. 我是ArangoDB的新手,也是一位不断增长的粉丝。 Among many things we need to translate many-to-many relations into graphs, and query efficiently in there. 在许多事情中,我们需要将多对多关系转换为图形,并在那里进行有效查询。 However I can't seem to reproduce the behaviour in NEIGHBORS as described in the cookbook under "Using Edge Collections". 但是,我似乎无法重现NEIGHBORS中的行为,如“使用边缘集合”中的食谱所述

After I insert data and run: 插入数据并运行后:

FOR b IN books RETURN { book: b, authors: NEIGHBORS(books, written, b._id, 'inbound') }
[
  {
    "book" : {
      "_id" : "books/10519631898915",
      "_key" : "10519631898915",
      "_rev" : "10519631898915",
      "title" : "The beauty of JOINS"
    },
    "authors" : [ ]
  }
]

Empty authors list! 空作者名单! I tried this instead: 我尝试了这个:

FOR b IN books RETURN { book: b, authors: NEIGHBORS(authors, written, b._id, 'inbound') }
[
  {
    "book" : {
      "_id" : "books/10519631898915",
      "_key" : "10519631898915",
      "_rev" : "10519631898915",
      "title" : "The beauty of JOINS"
    },
    "authors" : [
      "authors/10519474612515",
      "authors/10519475792163"
    ]
  }
]

Which returns the _id list. 返回_id列表。 None of those return what I need as in the cookbook, which is the expected edge/vertex structure. 这些都没有返回我需要的食谱,这是预期的边缘/顶点结构。 (All has been tested in 2.6.9) (所有已在2.6.9中测试过)

How is the use of NEIGHBORS intended and how do I get to my goal in pure AQL? 如何使用NEIGHBORS,如何实现我在纯AQL中的目标? Is there a standard documentation of NEIGHBORS (and other graph AQL features) somewhere with description and type of each argument as well as return value? 是否有一个标准的NEIGHBORS(以及其他图形AQL特性)文档,其中包含每个参数的描述和类型以及返回值?

Right, I found one solution: 是的,我找到了一个解决方案:

FOR p IN PATHS(books, written, 'inbound') 
RETURN p.destination

Result: 结果:

Warnings:

[1577], 'collection 'books' used as expression operand'

Result:

[
  {
    "_id": "books/10519631898915",
    "_rev": "10519631898915",
    "_key": "10519631898915",
    "title": "The beauty of JOINS"
  },
  {
    "_id": "authors/10519474612515",
    "_rev": "10519474612515",
    "_key": "10519474612515",
    "name": {
      "first": "John",
      "last": "Doe"
    }
  },
  {
    "_id": "authors/10519475792163",
    "_rev": "10519475792163",
    "_key": "10519475792163",
    "name": {
      "first": "Maxima",
      "last": "Musterfrau"
    }
  }
]

It gets the destination vertices at least, but it doesn't seem right since I get a warning and the source vertex is included as a destination. 它至少得到目标顶点,但它似乎不对,因为我收到警告并且源顶点被包含为目标。 Further elaboration and suggestions are very welcome. 非常欢迎进一步的阐述和建议。

Have you tried the includeData option for NEIGHBORS? 您是否尝试过NEIGHBORS的includeData选项?

FOR b IN books RETURN { book: b, authors: NEIGHBORS(authors, written, b._id, 'inbound', [], {includeData: true}) }

That worked in my test. 这在我的测试中起作用。 It will be way more performant then PATHS on large datasets (PATHS computes much more irrelevant information) 在大型数据集上,PATHS会更加高效(PATHS计算更多不相关的信息)

Note: The empty array [] is used to define edges that should be followed only. 注意:空数组[]用于定义仅应遵循的边。 With an empty array we follow all edges, but you could also follow special edges fe {label: "written"} instead of [] . 对于一个空数组,我们遵循所有边,但您也可以遵循特殊边缘fe {label: "written"}而不是[]

UPDATE (2017): NEIGHBORS is no longer supported in AQL 3.x 更新(2017年):AQL 3.x不再支持NEIGHBORS

Instead of 代替

NEIGHBORS(books, written, b._id, 'inbound')

you could write a sub-query: 你可以写一个子查询:

(FOR v IN 1..1 INBOUND b written RETURN v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM