spring-projects / spring-data-neo4j

Provide support to increase developer productivity in Java when using Neo4j. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
http://spring.io/projects/spring-data-neo4j
Apache License 2.0
828 stars 620 forks source link

Given same Query, output is different using Neo4j Browser and SDN Neo4jTemplate #2237

Closed mrksph closed 3 years ago

mrksph commented 3 years ago

Hi,

I'm using Neo4j Enterprise Edition v4.2.1 and Spring Data Neo4j 6.0.3.

I have the following Query:

MATCH path=(b:BaseHierarchy)-[]->(n:Group)-[:PARENT_OF*]->(c:Client) 
WHERE n.code = 'BPCEG' AND c.industry = '1005' 
WITH collect(path) as paths, b 
WITH b, reduce(a=[], node in reduce(b=[],c in [aa in paths | nodes(aa)] | b + c) | case when node in a then a else a + node end) as nodes, reduce(d=[], relationship in reduce(e=[],f in [dd in paths | relationships(dd)] | e + f) | case when relationship in d then d else d + relationship end) as relationships 
RETURN b, relationships, nodes;

If I run this query in the Neo4j Browser I get the following graph:

image

But if I run the same query using

Neo4jTemplate.findOne(query, new HashMap<>(), BaseHierarchyNode.class)

The result only includes the first-level children of the blue node BPCEG.

image

The data model is not the problem, as I can successfully retrieve the (correct) graph

Using Neo4jClient.query(query).fetch().one() I get the following result:

image

The same node/relationships count as the result of executing the query against Neo4j Browser.

meistermeier commented 3 years ago

We made some improvements to the mapping of related of related entities in the last versions of 6.0.x Could you upgrade to 6.0.8 and see if the problem still exist? Also your query should become now more performant because you could use

MATCH path=(b:BaseHierarchy)-[]->(n:Group)-[:PARENT_OF*]->(c:Client) 
WHERE n.code = 'BPCEG' AND c.industry = '1005' 
RETURN b, collect(nodes(p)), collect(relationships(p));
mrksph commented 3 years ago

Hi Gerrit, I'll give it a try, for now I tried to run the query with the changes you mention in the browser but the query couldn't be rendered as nodes and edges, could it be because it's intended to be used only with SDN?

mrksph commented 3 years ago

Hi, I've just run the query with the changes you said but the results come duplicated. I'm using SDN 6.0.8

If there's any info or detail I can provide to make it easier for you guys please just ask, thanks.

image

meistermeier commented 3 years ago

Duplicates are expected with this query. You can still use the other style if you prefer this. It is basically either let the database do the work (reduce style query) or SDN / application side (collect style).

mrksph commented 3 years ago

Ok, I fixed my problem with the duplicates using

apoc.coll.toSet(apoc.coll.flatten(collect(relationships))) as relationships,
apoc.coll.toSet(apoc.coll.flatten(collect(nodes))) as nodes;
meistermeier commented 3 years ago

So we can close this issue now?

mrksph commented 3 years ago

Yes thank you Gerrit, as always