当前位置:网站首页>Quickly obtain the attributes of the sub graph root node

Quickly obtain the attributes of the sub graph root node

2022-06-13 03:18:00 Tnoy. Ma


Here’s the table of contents:

Quickly get the attributes of the sub graph root node

     Subgraph matching is a very complicated problem , There are mainly Determine the subgraph matching of the pattern and Subgraph matching of uncertain patterns 【 for example : Search by graph pattern similarity 】. This paper mainly describes a sub graph query method for determining patterns , For the data model with many subgraphs, you can also use the way that community members ask questions in the screenshot of this article to model the data, which can achieve the purpose of saving resources and space ; However, specific modeling scenarios need to be combined with business scenarios to be feasible . Known subgraphs can be used to find problems APOC To achieve ,apoc.path Related input / output query ; After specifying the node, get the child graph to which the node belongs , Then extract from the subgraph ROOT Properties of a node .

One 、 The problem background

Two 、 Build sample multi subgraph data

     structure a、b、c、d、e、f Six nodes , And use Follow Relationships tie nodes together , Form a self defined subgraph . It specifies a The node is ROOT The node is the root node of the subgraph .

MERGE (a:Child {name:'a'}) SET a.subname='root'
MERGE (b:Child {name:'b'}) 
MERGE (c:Child {name:'c'}) 
MERGE (d:Child {name:'d'}) 
MERGE (e:Child {name:'e'}) 
MERGE (f:Child {name:'f'}) 
MERGE (a)-[:Follow]->(b)
MERGE (a)-[:Follow]->(c)
MERGE (b)-[:Follow]->(d)
MERGE (b)-[:Follow]->(e)
MERGE (c)-[:Follow]->(f)

 Insert picture description here

3、 ... and 、 Implement the attribute lookup of the root node

     stay Two Sample graph data is constructed in , The following implementation starts from any node in the sample graph ROOT node .

MATCH (n:Child) WHERE n.name='e'
CALL apoc.path.expand(n,'Follow',NULL,0,6) YIELD path WITH COLLECT(path) AS subGraph
UNWIND subGraph AS p
WITH nodes(p) AS nodes
UNWIND nodes AS node
WITH node
WHERE EXISTS(node.subname)
RETURN node

     The query results are as follows , We successfully found our pre-designed a node , That is, what we need to find ROOT node .

 Insert picture description here

Four 、 Search the subgraph GQL Encapsulate as a function

     For a complex query , It is usually necessary to hide its implementation details to facilitate business invocation . Pass below apoc.custom.asFunction This process , The realization of will 3、 ... and The purpose of further encapsulating complex queries in . You only need to use custom.subGraphRootName($para) This function can .

CALL apoc.custom.asFunction(
  	'subGraphRootName',
  	'MATCH (n:Child) WHERE n.name=$nodeName CALL apoc.path.expand(n,\'Follow\',NULL,0,6) YIELD path WITH COLLECT(path) AS subGraph UNWIND subGraph AS p WITH nodes(p) AS nodes UNWIND nodes AS node WITH node WHERE EXISTS(node.subname) RETURN node',
  	'STRING',
  	[['nodeName','STRING']],
  	FALSE,
  	' Get the root node to which the specified node belongs , And return the root node's subname attribute '
);
RETURN custom.subGraphRootName('e') AS rootSubName;

 Insert picture description here

5、 ... and 、 summary

     This article uses a very simple scenario , A method of subgraph analysis is introduced . The problems faced in actual production may be more complicated than this , Data writing performance needs to be comprehensively considered 、 Data query performance 、 Data service usability and other factors . Data optimization in this article is just The tip of the iceberg , A drop in the bucket , It is necessary to constantly polish and practice in actual combat .

原网站

版权声明
本文为[Tnoy. Ma]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280531435854.html