0

我有这个json:

{
    "name":"david", //node:Person
    "TAKING":[ //link
        {"name":"math"}, //node:Subject
        {"name":"physics"} //node:Subject
    ],
    "FRIEND":[ //link
        {"name":"andres"}, //node:Person
        {"name":"luis"} //node:Person
    ]
}

我有这个查询从neo4j中提取它

start person=node(*) match person-[:FRIEND]->friend, person-[:TAKING]->subject where person.name="Andres" return person, collect(distinct friend), collect(distinct subject);

结果是这样的:

==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
==> | person                                   | collect(distinct friend)                                                         | collect(distinct subject)                      |
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
==> | Node[1]{name:"Andres",title:"Developer"} | [Node[2]{name:"David",title:"Developer"},Node[3]{name:"Luis",title:"Developer"}] | [Node[5]{name:"math"},Node[6]{name:"physics"}] |
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

我认为查询的这一部分可以更好:

person-[:FRIEND]->friend, person-[:TAKING]->subject

目标是避免返回部分中的 distinc 子句:

collect(distinct friend), collect(distinct subject)

我将其重写为:

subject<-[:TAKING]-person-[:FRIEND]->friend

但同样的结果。

有没有更好的方法来进行这个查询?有没有办法用密码构建原始 json?

4

1 回答 1

1

尝试以下查询,如http://console.neo4j.org/?id=mlwmlt中所示,以避免使用 DISTINCT 关键字:

START person=node(*) 
WHERE HAS (person.name) AND person.name='A' 
WITH person 
MATCH (subject)<-[:TAKING]-(person) 
WITH person, COLLECT(subject) AS subjects 
MATCH (person)-[:FRIEND]->(friend) 
RETURN person, subjects, COLLECT(friend)

但一般来说你不应该使用 node(*)。一个好主意是使用名称索引。

于 2013-08-24T12:40:47.540 回答