简体   繁体   English

Neo4j-ogm查询路径

[英]Neo4j-ogm query path

In my Java code I have a query to match the shortest path from root to a leaf in my tree. 在我的Java代码中,我有一个查询来匹配从根到树中叶子的最短路径。

Strinq query = "Match path = (p:Root)-[*1..100]-(m:Leaf) "
    + "WITH p,m,path ORDER BY length(path) LIMIT 1 RETURN path";

However, when I try to query this as follows 但是,当我尝试如下查询时

SessionFactory sessionFactory = new SessionFactory("incyan.Data.Neo4j.Models");
Session session = sessionFactory.openSession("http://localhost:7474");
Object o = session(query, new HashMap<String,Object>());

o contains an ArrayList of LinkedHashMap s instead of mapped objects. o包含LinkedHashMapArrayList而不是映射的对象。

I cannot even determine the labels of the path elements and the start and end nodes of the relations. 我什至无法确定路径元素的标签以及关系的起点和终点。

What am I doing wrong? 我究竟做错了什么?

The current neo4j-ogm release does not map query results to domain entities. 当前的neo4j-ogm版本未将查询结果映射到域实体。 Returning a path will only give you the properties of nodes and relationships in that path (in order, so you can infer the relationship start/end). 返回路径只会为您提供该路径中节点和关系的属性(按顺序,因此您可以推断关系的开始/结束)。 ID's aren't returned by the Neo4j REST api currently used by the OGM for this particular operation and that's why they are missing. OGM当前用于此特定操作的Neo4j REST api不会返回ID,这就是它们丢失的原因。 You may instead have to extract the ID's and return them as part of your query. 您可能必须提取ID并将其作为查询的一部分返回。

Mapping individual query result columns to entities will be available in a Neo4j-OGM 2.0 release. Neo4j-OGM 2.0版本将提供将单个查询结果列映射到实体的功能。

I'm not sure about the Java bit, but if you use the shortestPath function (keyword?) your query should be more efficient: 我不确定Java的含义,但是如果您使用shortestPath函数(关键字?),则查询应该更有效:

MATCH path=shortestPath((p:Root)-[*1..100]-(m:Leaf))
RETURN path

Also, I don't know what your data model is like, but I would expect the labels on the nodes of your tree (I'm assuming it's a tree) to all be the same. 另外,我不知道您的数据模型是什么样的,但是我希望树的节点(我假设它是一棵树)上的标签都相同。 You can tell if a node is a root or a leaf using Cypher: 您可以使用Cypher判断节点是根还是叶:

MATCH path=shortestPath((root:Element)-[*1..100]-(leaf:Element))
WHERE NOT((root)-[:HAS_PARENT]->()) AND NOT(()-[:HAS_PARENT]->(leaf))
RETURN path

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

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