简体   繁体   English

Neo4j / Cypher查询基于两个不相关的节点返回一个结果

[英]Neo4j / Cypher query based on two unrelated nodes returning one result

Below, a conceptual example of my context: 下面是我的上下文的概念性示例:

I have an abstract class named: User (containing an id property as String) 我有一个抽象类,名为: User (包含id属性作为String)
I have a User subclass named: FacebookUser 我有一个User指定的子类: FacebookUser
I have a User subclass named: TwitterUser 我有一个User指定的子类: TwitterUser

Using Spring-Data-Neo4j, I make User a @NodeEntity . 使用Spring-Data-Neo4j,我将User@NodeEntity

I create a test class in order to insert a FacebookUser and a TwitterUser in the graph. 我创建一个测试类,以便在图中插入FacebookUserTwitterUser
The whole well occurs => both nodes are inserted and have a _ type _ containing the fully qualified name of the subclass. 整个过程发生=>两个节点都插入并且具有_ 类型 _,其中包含子类的完全限定名称。
For instance: com.myApp.model.user.FacebookUser . 例如: com.myApp.model.user.FacebookUser

Now, I want to make this kind of cypher query: 现在,我要进行这种密码查询:
Retrieve the User whose id = 123 检索ID = 123的用户
Thus, I have to find the more efficient way to check for all user nodes (union of FacebookUser s and TwitterUser ), in order to find the unique corresponding record. 因此,我必须找到一种更有效的方法来检查所有用户节点( FacebookUserTwitterUser ),以便找到唯一的对应记录。

I expected to find a way to tell to cypher something like this: 我希望找到一种方法来告诉密码这样的事情:
Take every index matching FacebookUser , every index matching TwitterUser , and check for the expected node amongst them 取得与FacebookUser匹配的每个索引,与TwitterUser匹配的每个索引,并检查其中的预期节点

but I didn't manage. 但是我没办法

My current query, working, is: 我当前的查询有效:

@Query("start u=node(*) where has(u.__type__) and u.__type__ =~ '.*User' and has(u.id) and u.id = {0} return u")

This one translates to: 这一翻译为:

Check for ALL nodes in the graph (I suppose, not efficient at all), then retain those ending with "User" (including FacebookUser and TwitterUser) and check for the right id 检查图中的所有节点(我想,根本没有效率),然后保留那些以“ User”结尾的节点(包括FacebookUser和TwitterUser)并检查正确的id

Is there a way to only use index nodes in this case, instead of scanning the whole graph? 在这种情况下,是否有办法只使用索引节点,而不是扫描整个图?

I was thinking about a way to rename the _ type _ property to the full qualified name of the parent class. 我正在考虑将_ type _属性重命名为父类的完整限定名称的方法。

Can we do it easily with SDN? 我们可以使用SDN轻松做到吗?

Indeed, since it appears that SDN uses the Java class name associated to the node as the node's index name, it would allow to deal with only one index node for both Facebook and Twitter subtypes: the User index node. 实际上,由于SDN似乎使用与该节点关联的Java类名称作为节点的索引名称,因此对于Facebook和Twitter子类型,它仅允许处理一个索引节点: User索引节点。

If you use @Indexed on the classes, you should get indexes like TwitterUser etc, that you then can use with your queries. 如果在类上使用@Indexed,则应获取TwitterUser等索引,然后可以将其与查询一起使用。 However, there are limitations to the inheritance approach, see Spring Data Neo4j - Indexing and Inheritance for a related discussion. 但是,继承方法存在局限性,有关相关讨论,请参见Spring Data Neo4j-索引和继承

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

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