2017-03-15 15 views
0

数据这样在ArangoDB中如何通过多边选择图形过滤器中的一个节点?

a->b,c,d 
b->c,d 
d->a,b 

这样的查询

FOR n in Nodes 
FOR v,e,p IN 1 ANY n GRAPH 'MyGraph' 
// Only need a 
// HOW TO WRITE: FILTER n have an edge to `b` and n have an edge to `d` 

// This will select a,b,c 
FILTER v._key in [ 'b', 'd' ] 
RETURN p 

我想选择与边缘节点到b d,否B d,但如何?

编辑

数据

insert {_key:'a'} in nodes 
insert {_key:'b'} in nodes 
insert {_key:'c'} in nodes 
insert {_key:'d'} in nodes 

insert {_from:'nodes/a',_to:'nodes/b'} into relate 
insert {_from:'nodes/a',_to:'nodes/c'} into relate 
insert {_from:'nodes/a',_to:'nodes/d'} into relate 
insert {_from:'nodes/b',_to:'nodes/c'} into relate 
insert {_from:'nodes/b',_to:'nodes/d'} into relate 
insert {_from:'nodes/c',_to:'nodes/d'} into relate 

一个很虚的解决方案是

for n in nodes 
for v,e,p in 1 OUTBOUND n graph 'MyGraph' 
filter v._key == 'b' 
for v2,e2,p2 in 1 INBOUND v graph 'MyGraph' 
sort v2._key == 'd' 
return v2 

但这个查询只能在两种条件下工作,如果我需要一个条件我必须写再查询一个for

回答

1

我看到了几个可以使用的查询。我添加了一个顶点e以显示他们在有更多条件时的样子。

I。最高效的查询应该是:

FOR v IN 1 INBOUND 'nodes/b' graph 'MyGraph' 
    FILTER length(FOR d IN 1 OUTBOUND v graph 'MyGraph' 
    FILTER d._key == 'd' 
    LIMIT 1 
    RETURN 1) == 1 
    FILTER length(FOR e IN 1 OUTBOUND v graph 'MyGraph' 
    FILTER e._key == 'e' 
    LIMIT 1 
    RETURN 1) == 1 
    RETURN v 

查询搜寻SEARCH_TERM_EXAMPLES的b邻居和过滤器在一个子查询与邻国建立并检查它们是否与de连接。

II。一个更透明但也更慢的查询:

LET b = (FOR v IN 1 INBOUND "nodes/b" graph 'MyGraph' RETURN v) 
LET d = (FOR v IN 1 INBOUND "nodes/d" graph 'MyGraph' RETURN v) 
LET e = (FOR v IN 1 INBOUND "nodes/e" graph 'MyGraph' RETURN v) 
RETURN INTERSECTION(b, d, e) 

该查询为每个搜索到的节点进行子查询并返回其邻居的交集。

III。利用bindVars的一个非常通用的查询,但也是最慢的:

绑定参数:

{ 
    "targets": ["b","d","e"] 
} 

查询:

FOR n IN nodes 
    LET neighbors = (FOR v,e,p IN 1 OUTBOUND n graph 'MyGraph' 
    FILTER v._key IN @targets 
    RETURN v._key) 
FILTER @targets ALL IN neighbors 
RETURN n 

是的,这是最慢的,但你从来没有改变当你想添加更多条件时,再查询自己。你只需要改变绑定参数。

相关问题