1
votes

In my Java code I have a query to match the shortest path from root to a leaf in my tree.

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 LinkedHashMaps instead of mapped objects.

I cannot even determine the labels of the path elements and the start and end nodes of the relations.

What am I doing wrong?

2

2 Answers

1
votes

The current neo4j-ogm release does not map query results to domain entities. 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. You may instead have to extract the ID's and return them as part of your query.

Mapping individual query result columns to entities will be available in a Neo4j-OGM 2.0 release.

0
votes

I'm not sure about the Java bit, but if you use the shortestPath function (keyword?) your query should be more efficient:

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:

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