如何在Neo4j中执行嵌套查询 [英] How to do nested queries in neo4j
问题描述
在neo4j中无法弄清楚如何很好地做到这一点,所以请帮助我.
Can't figure out how to do this in a good way in neo4j, so please help me.
我有三种类型的对象:人,对象,意见. 我想获取最新意见的列表,持有意见的人,对象.我可以做到的,这并不困难. 当我想让对同一对象在同一查询中有意见的人时,我的问题就开始了.
I have Three types of objects: Person, Object, Opinion. I want to get a list of the latest opinions, the person who had the opinion, the object. This I can do, it's not difficult. My problem starts when I want to get the people that has opinions about the same objects in the same query.
这是我想要的结果:
- 人员:Berit,对象:cerials,意见:良好
- 人员:Arne,对象:cerials,意见:好
- 人:阿尔伯特,对象:cerials,意见:不好
- Person: Berit, Object: cerials, Opinion: good
- Person: Arne, Object: cerials, Opinion: good
- Person: Albert, Object: cerials, Opinion: bad
- 人员:Arne,对象:食谱,意见:不必要
- 人:Tove,对象:食谱,意见:差
- 人:Berit,对象:食谱,意见:差
...等等
推荐答案
您没有描述您的关系类型,所以我将它们进行组合.这是您可能会开始的方法:
You don't describe your relationship types, so I'll just make them up. Here is how you might start:
MATCH (target:Person)-[:HAS_OPINION]->(target_opinion:Opinion)-[:ON]->(object:Object)<-[:ON]-(other_opinion:Opinion)<-[:HAS_OPINION]-(other_person:Person)
WHERE target.id = {target_id}
目标可以基于ID/名称/任何...
The target could be based on id/name/whatever...
您可以通过两种方式返回数据.可能是非规范化的结果:
You could return the data in a couple of ways. It could be a denormalized result:
MATCH (target:Person)-[:HAS_OPINION]->(target_opinion:Opinion)-[:ON]->(object:Object)<-[:ON]-(other_opinion:Opinion)<-[:HAS_OPINION]-(other_person:Person)
WHERE target.id = {target_id}
RETURN target, target_opinion, object, other_opinion, other_person
或者您可以每行返回一个目标,然后将其他所有目标收集到一组对象中:
Or you could return one target per row and collect all of the others up into an array of objects:
MATCH (target:Person)-[:HAS_OPINION]->(target_opinion:Opinion)-[:ON]->(object:Object)<-[:ON]-(other_opinion:Opinion)<-[:HAS_OPINION]-(other_person:Person)
WHERE target.id = {target_id}
RETURN
target,
target_opinion,
object,
collect({opinion: other_opinion, person: other_person}) AS others
这篇关于如何在Neo4j中执行嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!