0

I am currently using SpringDataNeo4j–4.1.0.M1 and have a use case where I need to get a sub-graph (actually a tree) and return it in an hierarchical structure similar to this (these sub-graphs are usually around 500 nodes in size and have an unspecified depth):

<Pathway dbId="1" displayName="Name1">
  <Pathway dbId="2" displayName="Name2">
    <Pathway dbId="3" displayName="Name3">
      <Reaction dbId="4" displayName="Name4" />
      <Reaction dbId="5" displayName="Name5" />
      <Pathway dbId="6" displayName="Name6">
          <Reaction dbId="7" displayName="Name7" />
      </Pathway>
    </Pathway>
    ...

The data model:

@NodeEntity
public abstract class Event {

    @GraphId
    private Long id;

    private Long dbId;
    private String displayName;
    ... other relationships and properties
}

@NodeEntity
public class Pathway extends Event {

    @Relationship(type="hasEvent", direction = Relationship.OUTGOING)
    private List<Event> hasEvent;
    ... other relationships and properties
}

@NodeEntity
public class Reaction extends Event {

    ... other relationships and properties
}

To solve this I have created the following query:

String query = "Match (n:Pathway{dbId:123456})-[r:hasEvent*]->(m:Event) Return n,r,m";
Result result =  neo4jTemplate.query(query,Collections.<~>emptyMap());
return result.iterator().next().get("n");

This is working and returns the result as expected. The only problem is the speed. While testing the query using the Neo4j-Browser I get a result within 50-100ms. Using SDN it takes over 2 seconds for Spring to map the result to objects.

The question now is: Is there any way I can speed up this query in Spring or is there a better solution where I can return this hierarchical structure in Cypher using something like nested Collections(since I only need the names and ids and not the objects themselves)?

fkorn
  • 50
  • 6
  • Seems very slow. Is there are reason you're using a hand-rolled query, rather than one of the template methods? I would have thought you could use `neo4jTemplate.load(Pathway.class, pathway.getId(), -1)` where -1 indicates the fetch depth (infinite in this case). Have you checked how many results you get back, and are you able to profile the code to find out where it is performing badly? – Vince Mar 22 '16 at 14:23
  • If I just use the depth parameter I can not specify the specific relation (hasEvent) to traverse the graph (otherwise probably the entire graph will be loaded). In this specific case 527 objects will be returned. I think one problem could be that even if I omit all other relationships the objects are still rich in information (each object has about 5-10 filled properties). I have not yet profiled the code, I just recorded the time of the the execution of `Result result = neo4jTemplate.query(query,Collections.emptyMap());` – fkorn Mar 22 '16 at 14:58

0 Answers0