简体   繁体   English

阐明neo4j和neo4j驱动程序的不同行为

[英]Clarify different behaviour of neo4j and neo4j-driver

I have a rudimentary database that has just a couple of nodes and relationships. 我有一个只有几个节点和关系的基本数据库。 When I run a match (n) return n command on the local web client provided with neo4j it returns all the nodes and relationships that's in the database, as seen in the picture below. 当我在neo4j随附的本地Web客户端上运行match (n) return n命令时,它将返回数据库中的所有节点和关系,如下图所示。

neo4j Web客户端

However when I run exactly the same command in a node.js project using the neo4j-driver module, it only returns the three nodes and none of the two relationships are included. 但是,当我使用neo4j-driver模块在node.js项目中运行完全相同的命令时,它仅返回三个节点,并且不包含两个关系。

After a little bit of fiddling with it, I noticed that to retrieve the relationships too, I must issue something like match (n)-[r]-(m) return * . 经过一番摆弄之后,我注意到要检索这些关系,还必须发出match (n)-[r]-(m) return * My first question is why is there such a difference? 我的第一个问题是为什么会有这样的区别? Is the local web client just trying to do a bit more to help the user? 本地Web客户端是否只是在尝试做更多事情来帮助用户?

Furthermore I find the returned records object a little bit confusing. 此外,我发现返回的记录对象有些混乱。 Running this match (n)-[r]-(m) return * command returns 4 items in the result.records object out of which 2-2 are almost identical pair-wise. 运行此match (n)-[r]-(m) return *命令将在result.records对象中返回4个项目,其中2-2成对几乎相同。 In a simplified view this is what it returns: 在简化视图中,这是返回的内容:

item 0: [Jack node, Jill node, Jack -> Jill relationship]
item 1: [Jill node, Jack node, Jack -> Jill relationship]
item 2: [George node, Jill node, George -> Jill relationship]
item 3: [Jill node, George node, George -> Jill relationship]

So items 0 and 1 of the result.records object differ only by the order of their elements. 因此,result.records对象的项目0和1仅在其元素顺序上有所不同。 Same for items 2 and 3. 项目2和3相同。

Question two is what am I supposed to do with this if I want to display the graph on a web page? 问题二,如果我想在网页上显示图形,该怎么办? Look for unique IDs of the nodes and relationships in all the different combinations returned? 在返回的所有不同组合中查找节点和关系的唯一ID?

Question three: maybe there's a better way to achieve what I'm trying to do? 问题三:也许有更好的方法可以实现我的目标?

The Neo4j web browser is indeed just trying to be helpful and the visualization will connect nodes if they have relationships between them (there's an option to turn this behaviour off btw). Neo4j Web浏览器确实只是在尝试提供帮助,并且可视化对象将连接节点(如果它们之间具有关系)(可以选择关闭此行为)。 However, the resultset will not contain those if you didn't ask for them (as it shouldn't). 但是,如果您不要求结果集,那么结果集将不包含那些(因为它不应该)。 Look on the other reponse tabs (table, text, code) in the browser to see the actual resultset. 在浏览器中查看其他响应选项卡(表格,文本,代码)以查看实际结果集。

This query may help you : 该查询可以帮助您:

match p=(n)-[r]-(m) return p

But yes, you are correct, you will have to unpack the result in your application in order to be able to do your own interpretation. 但是,是的,您是对的,您将必须在应用程序中解压缩结果才能进行自己的解释。 It is a case of the you get what you asked for problem that quite a few Neo4j users face. 这就是您遇到许多Neo4j用户所面临的问题的情况。 It is due to the fact that Cypher can return quite a few different things (tabular results, nodes, nodes and relationships, paths, subgraphs, ...) and the driver has to provide for all of them. 这是由于Cypher可以返回许多不同的东西(表格结果,节点,节点和关系,路径,子图等),而驱动程序必须提供所有这些东西。

Have a look at the code tab in the browser to get a feel of what your application will have to work with (what you actually get depends on your application language of choice). 在浏览器中查看“ 代码”选项卡,以了解应用程序必须使用的功能(实际获得的内容取决于选择的应用程序语言)。 It's not very difficult but it does require a bit of getting used to. 这不是很困难,但是确实需要一些习惯。

Hope this helps. 希望这可以帮助。

Regards, Tom 问候,汤姆

PS Doubles in the results are to be expected with such generic queries. 使用此类通用查询,结果有望达到PS Doubles。 Neo4j does pattern matching and your pattern does not have a direction on the relationship nor does it have labels or relationshiptypes. Neo4j进行模式匹配,并且您的模式没有关系的方向,也没有标签或关系类型。 That's going to return quite a few matches where for example (jill)-[:nominated]-(jack) but obviously it also matches (jack)-[:nominated]-(jill). 这将返回很多匹配项,例如(jill)-[:nominated]-(jack),但显然也匹配(jack)-[:nominated]-(jill)。 Both match the pattern. 两者都匹配模式。 Using DISTINCT may help a bit but you really should be more explicit in the pattern. 使用DISTINCT可能会有所帮助,但是您实际上应该在模式中更加明确。

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

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