当前位置:网站首页>Use cypher to get the tree of the specified structure
Use cypher to get the tree of the specified structure
2022-06-13 03:19:00 【Tnoy. Ma】
Use Cypher Get the tree of the specified structure
Here’s the table of contents:
Use Cypher Get the tree of the specified structure
One 、 Links to questions from the community
Neo4j Figure database Chinese community : How to get the tree of the specified structure ?


But at the same level node I want to remove duplicates as an array , For example, below 
But the parent node and relationship will be lost after simple processing , I want each node to be converted to a map object , This object contains the original node , And of the parent node id, The name of the relationship is easy to serialize directly on the client
But I tried many ways , This can only be done for a single column , Multiple columns become Cartesian products 
Two 、 Write query to realize data encapsulation
2.1 Create sample data

2.2 Cypher Realization
Encapsulate data hierarchically 【 Get the tree of the specified structure 】, Each node of each layer in the returned result contains the relationship associated with the node ID、 node ID; If you need to include nodes in the returned results 、 Relationship Attribute and type information , Can be in the corresponding Cypher The query location is encapsulated and returned together .
// Match the path to be processed
MATCH p=(c:Class {name:' The movie '})-[r1: example ]->(m:Movie)<-[r2:ACTED_IN]-(:Person)
// Collect two layers of relationships
WITH COLLECT(DISTINCT r1) AS hc_r1,COLLECT(DISTINCT r2) AS hc_r2
// Parse the first layer NODE, The second floor NODE, The third level NODE
// NODE Object structure :node- Current layer node ,f_node Represents the parent node ,rel Represents the relationship of the current node
WITH REDUCE(l=[],r IN hc_r1 | l+apoc.map.setPairs({},[["node",ID(STARTNODE(r))],["rel",ID(r)]])) AS hc_n1,
REDUCE(l=[],r IN hc_r1 | l+apoc.map.setPairs({},[["f_node",ID(STARTNODE(r))],["node",ID(ENDNODE(r))],["rel",ID(r)]])) AS hc_n2,
REDUCE(l=[],r IN hc_r2 | l+apoc.map.setPairs({},[["f_node",ID(ENDNODE(r))],["node",ID(STARTNODE(r))],["rel",ID(r)]])) AS hc_n3
// After the last treatment , Nodes of each layer 、 The associations and parent nodes are ready , The next step is to `node` Weight removal , And then `f_node` and `rel` Collected in an array
// There may be multiple parent nodes and associations of the current node , So you need to add two after weight removal map Field `f_nodes` and `rels`
WITH hc_n1,hc_n2,hc_n3
WITH apoc.map.groupByMulti(hc_n1,'node') AS hc_n1_gp,EXTRACT(map IN hc_n1 | map.node) AS hc_n_l1,
apoc.map.groupByMulti(hc_n2,'node') AS hc_n2_gp,EXTRACT(map IN hc_n2 | map.node) AS hc_n_l2,
apoc.map.groupByMulti(hc_n3,'node') AS hc_n3_gp,EXTRACT(map IN hc_n3 | map.node) AS hc_n_l3
// Encapsulate the data format of each layer finally returned [{cur_node:node,f_nodes:[2,123],rels:[213,23]}]
WITH REDUCE(l=[],nd IN hc_n_l1 | l+apoc.map.setPairs({},[["cur_node",nd],["f_nodes",apoc.coll.toSet(FILTER(e IN EXTRACT(mp IN apoc.map.get(hc_n1_gp,TOSTRING(nd),NULL,FALSE) | mp.f_node) WHERE e IS NOT NULL))],["rels",apoc.coll.toSet(FILTER(e IN EXTRACT(mp IN apoc.map.get(hc_n1_gp,TOSTRING(nd),NULL,FALSE) | mp.rel) WHERE e IS NOT NULL))]])) AS hc1,
REDUCE(l=[],nd IN hc_n_l2 | l+apoc.map.setPairs({},[["cur_node",nd],["f_nodes",apoc.coll.toSet(FILTER(e IN EXTRACT(mp IN apoc.map.get(hc_n2_gp,TOSTRING(nd),NULL,FALSE) | mp.f_node) WHERE e IS NOT NULL))],["rels",apoc.coll.toSet(FILTER(e IN EXTRACT(mp IN apoc.map.get(hc_n2_gp,TOSTRING(nd),NULL,FALSE) | mp.rel) WHERE e IS NOT NULL))]])) AS hc2,
REDUCE(l=[],nd IN hc_n_l3 | l+apoc.map.setPairs({},[["cur_node",nd],["f_nodes",apoc.coll.toSet(FILTER(e IN EXTRACT(mp IN apoc.map.get(hc_n3_gp,TOSTRING(nd),NULL,FALSE) | mp.f_node) WHERE e IS NOT NULL))],["rels",apoc.coll.toSet(FILTER(e IN EXTRACT(mp IN apoc.map.get(hc_n3_gp,TOSTRING(nd),NULL,FALSE) | mp.rel) WHERE e IS NOT NULL))]])) AS hc3
// Use apoc.coll.toSet Weight removal list
RETURN apoc.coll.toSet(hc1) AS hc1,
apoc.coll.toSet(hc2) AS hc2,
apoc.coll.toSet(hc3) AS hc3

边栏推荐
- How to become a technological bull -- from the bull man
- Time processing class in PHP
- 【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
- Use and arrangement of wechat applet coordinate position interface (I)
- Introduction to Kestrel_ Introduction to kestrel web server
- 2022.05.29
- In my ten years, every bit has become a landscape?
- MySQL and PostgreSQL installation subtotal
- This article takes you to learn DDD, basic introduction
- 2021-08-30 distributed cluster
猜你喜欢

Querywrapper constructor method

MySQL 8.0 installation free configuration method

Summary of virtualization technology development

JS merge multiple string arrays to maintain the original order and remove duplicates

Open source - campus forum and resource sharing applet
![Data Governance Series 1: data governance framework [interpretation and analysis]](/img/d9/1476d0ee2c82f5cdd70b4cffaba423.jpg)
Data Governance Series 1: data governance framework [interpretation and analysis]
![[JVM Series 7] garbage collector](/img/e5/902de6398359b75183aa1fafcaa7d1.jpg)
[JVM Series 7] garbage collector

Summary of rust language practice
![[JVM series 8] overview of JVM knowledge points](/img/e5/902de6398359b75183aa1fafcaa7d1.jpg)
[JVM series 8] overview of JVM knowledge points

Code d'initialisation de l'arbre binaire
随机推荐
Array in PHP array function_ Slice and array_ flip
C simple understanding - generics
Application framework / capability blueprint
C method parameter: params
Differences between XAML and XML
【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
On the career crisis of programmers at the age of 35
Six special GPU products for domestic aircraft passed the appraisal and review
Summary of rust language practice
Spark UDF instance details
[JVM Series 7] garbage collector
MySQL transactions and locks (V)
IOS interview · full bat interview record of an IOS programmer (including the true interview questions of Baidu + Netease + Alibaba)
Understanding of intermediatelayergetter
Exercise 8-3 rotate array right
Operating principle of JS core EventLoop
Large attachment fragment upload and breakpoint continuation
Time processing class in PHP
Use of interceptors webmvcconfigurer
Three ways to start WPF project