以图搜图-自动生成图模式匹配Cypher

2022-07-04 14:42:22 浏览数 (1)

@TOC[1] Here's the table of contents:

•一、函数使用方式

•二、autoCypher返回的格式

•三、过滤器

3.1 属性过滤器

3.2 Elasticsearch过滤器

•四、使用场景

•五、依赖

•六、根据auto-cypher分析的案例schema生成图模式扩展的样例结构

•七、使用案例

•7.1 apoc.cypher.run过程

•7.2 olab.convert.json函数

•7.3 olab.schema.auto.cypher执行

•7.4 从graph data解析图模式

•7.5 olab.schema.auto.cypher函数其它使用案例

•八、参考链接

以图搜图-自动生成图模式匹配Cypher

这里要实现的搜图效果,不是搜索图片,而是搜索图数据。olab.schema.auto.cypher函数可以实现对已有图结构的翻译,实现以图搜图的效果非path匹配。通过JSON定义的图格式数据,抽取图模式并拼接为CYPHER语句。根据CYPHER执行代价自动优化CYPHER查询。

该函数支持两种过滤器,属性过滤器和ES过滤器。属性过滤器可以实现对图数据本身的属性数据进行过滤。ES过滤器支持从Elasticsearch过滤复杂的指标数据。

一、函数使用方式

代码语言:javascript复制
// {limit小于1表示不限制匹配子图个数}
// 函数支持四个入参
RETURN olab.schema.auto.cypher({json},{skip},{limit},{isReTraverseNode})
代码语言:javascript复制
 olab.schema.auto.cypher入参:
     * @param json:入参  #############
     *                 属性过滤器属性之间过滤,连接方式只支持AND
     *                 #############
     *                 属性过滤器(properties_filter):
     *                 包含【STRING】:‘ name CONTAINS '北京' '
     *                 等于【STRING、INT、LONG】:‘ name='北京' ’  ‘ count=0 ’
     *                 大于【INT、LONG】:‘ count>0 ’
     *                 小于【INT、LONG】:‘ count<0 ’
     *                 大于等于【INT、LONG】:‘ count>=0 ’
     *                 小于等于【INT、LONG】:‘ count>=0 ’
     *                 范围-左闭右闭【INT、LONG】:‘ count>=0 AND count<=10 ’
     *                 范围-左闭右开【INT、LONG】:‘ count>=0 AND count<10 ’
     *                 范围-左开右闭【INT、LONG】:‘ count>0 AND count<=10 ’
     *                 范围-左开右开【INT、LONG】:‘ count>0 AND count<10 ’
     *                 #############
     *                 ES-QUERY
     *                 #############
     *                 ES过滤器(es_filter):
     *                 ES-QUERY-DSL【去掉JSON引号的查询语句】eg.{size:1,query:{term:{product_code:"PF0020020104"}}}
     * @param skip:忽略参数
     * @param limit:限制参数【表示匹配多个子图】
     * @param isReTraverseNode:是否允许重复遍历节点【默认不允许】【指graph中每条path中是否允许节点重复】【path之间是允许节点重复的,因为用户有可能这样定义查询图】

二、autoCypher返回的格式

函数返回的数据格式支持节点和图两种格式。节点格式表示匹配模式中只包含节点,图格式表示匹配模式包含节点和关系,并且匹配图模式不支持非联通图。

•节点图模式只包含节点

代码语言:javascript复制
RETURN n

•图对象path集合【图模式包含节点与关系并且不包含孤立节点】

代码语言:javascript复制
RETURN {graph:[path1,path2]} AS graph LIMIT 1

三、过滤器

涉及到对指标数据的过滤,可以根据数据模型选择不同的过滤器。如果选择了ES过滤器,则在模型设计阶段需要设计集成ES的图数据指标存储架构。

3.1 属性过滤器

抽象定义对节点和关系的一级属性暂不支持二级属性的过滤进行过滤;{var}表示对当前过滤对象的抽象定义,是变量的占位符;过滤条件之间支持使用AND连接。

代码语言:javascript复制
"properties_filter": [
          {
            "hcreatetime": "{var}.hcreatetime='20201116032333'"
          },
          {
            "count": "{var}.count>=0 AND {var}.count<=10"
          }
]

3.2 Elasticsearch过滤器

主要实现对节点和关系的二级属性过滤查询;被过滤对象和过滤条件之间使用entity_unique_code:{var}对接,entity_unique_code字段是默认的ONgDB和Elasticsearch数据对接的字段;依赖custom.es.result.bool函数执行ES过滤。

代码语言:javascript复制
"es_filter": [
          {
            "es_url": "10.20.13.130:9200",
            "index_name": "index_name_1",
            "query": "{size:1,query:{term:{entity_unique_code:{var}}}}}"
          }
]

四、使用场景

对于复杂图数据的分析业务人员不需要懂CYPHER语句就可以实现对数据的获取。自动化系统化这是图数据平台建设的必经之路【未来会分享更多关于本体管理系统的设计】。使用场景-无代码化分析获取图数据:对节点和关系的二级属性执行过滤;对节点和关系的指标执行过滤;挖掘满足多重指标限制的图模式;时序子图的过滤。

五、依赖

使用该函数ongdb集群需要安装auto-cypher-parainstalles-function.cql所示的函数

代码语言:javascript复制
-- 函数安装方式
-- 使用场景:对节点和关系的指标执行过滤;挖掘满足多重指标限制的图模式;时序子图的过滤
CALL apoc.custom.asFunction(
    'es.result.bool',
    'CALL apoc.es.query($esuUrl,$indexName,'',null,$queryDsl) YIELD value WITH value.hits.total.value AS count CALL apoc.case([count>0,'RETURN TRUE AS countBool'],'RETURN FALSE AS countBool') YIELD value RETURN value.countBool AS bool',
    'BOOLEAN',
    [['esuUrl','STRING'],['indexName','STRING'],['queryDsl','MAP']],
    false,
    '通过判断ES查询结果返回FALSE或者TRUE【结果集大于0返回TRUE】'
);
-- 访问方式:RETURN custom.es.result.bool('10.20.13.130:9200','dl_default_indicator_def',{size:1,query:{term:{product_code:"PF0020020104"}}}) AS boolean

六、根据auto-cypher分析的案例schema生成图模式扩展的样例结构

•无环路

代码语言:javascript复制
CREATE (org1:公司) SET org1.name='公司' WITH org1
CREATE (org2:公司) SET org2.name='公司' WITH org1,org2
CREATE (org3:公司) SET org3.name='公司' WITH org1,org2,org3
CREATE (loc:位置) SET loc.name='位置' WITH org1,org2,org3,loc
CREATE (pro:产品) SET pro.name='产品' WITH org1,org2,org3,loc,pro
CREATE (idu1:行业) SET idu1.name='行业' WITH org1,org2,org3,loc,pro,idu1
CREATE (idu2:行业) SET idu2.name='行业' WITH org1,org2,org3,loc,pro,idu1,idu2
CREATE p1=(org1)-[:持股]->(org2) WITH org1,org2,org3,loc,pro,idu1,idu2,p1
CREATE p2=(org1)-[:担保]->(org3) WITH org1,loc,pro,idu1,idu2,p1,p2
CREATE p3=(org1)-[:属于]->(loc) WITH org1,pro,idu1,idu2,p1,p2,p3
CREATE p4=(org1)-[:生产]->(pro) WITH org1,idu1,idu2,p1,p2,p3,p4
CREATE p5=(org1)-[:属于]->(idu1) WITH org1,idu1,idu2,p1,p2,p3,p4,p5
CREATE p6=(idu1)-[:上下游]->(idu2) WITH p1,p2,p3,p4,p5,p6
RETURN {graph:[p1,p2,p3,p4,p5,p6]} AS graph

•多环路

代码语言:javascript复制
CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
RETURN {graph:[p1,p2,p3,p4,p5,p6]} AS graph

•多个环路

代码语言:javascript复制
  CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7
CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8
RETURN {graph:[p1,p2,p3,p4,p5,p6,p7,p8]} AS graph

七、使用案例

案例中使用的DEMO入参数据集下载[2]

7.1 apoc.cypher.run过程

•执行读查询

代码语言:javascript复制
  CALL apoc.cypher.run({CYPHER},{PARAS})

7.2 olab.convert.json函数

•生成JSON->从CYPHER直接生成JSON【支持节点转换/属性转换/路径转换】

代码语言:javascript复制
  match (n) return olab.convert.json(n) limit 10
match p=(n)-heng's[]-() return olab.convert.json(p) limit 1
match (n) return olab.convert.json(properties(n)) limit 10
RETURN apoc.convert.fromJsohengsnList(olab.convert.json(['21','123',123]))

7.3 olab.schema.auto.cypher执行

下面的几个例子展示从已有的图数据生成搜素图模式的CYPHER语句

•无环路

代码语言:javascript复制
CREATE (org1:公司) SET org1.name='公司' WITH org1
CREATE (org2:公司) SET org2.name='公司' WITH org1,org2
CREATE (org3:公司) SET org3.name='公司' WITH org1,org2,org3
CREATE (loc:位置) SET loc.name='位置' WITH org1,org2,org3,loc
CREATE (pro:产品) SET pro.name='产品' WITH org1,org2,org3,loc,pro
CREATE (idu1:行业) SET idu1.name='行业' WITH org1,org2,org3,loc,pro,idu1
CREATE (idu2:行业) SET idu2.name='行业' WITH org1,org2,org3,loc,pro,idu1,idu2
CREATE p1=(org1)-[:持股]->(org2) WITH org1,org2,org3,loc,pro,idu1,idu2,p1
CREATE p2=(org1)-[:担保]->(org3) WITH org1,loc,pro,idu1,idu2,p1,p2
CREATE p3=(org1)-[:属于]->(loc) WITH org1,pro,idu1,idu2,p1,p2,p3
CREATE p4=(org1)-[:生产]->(pro) WITH org1,idu1,idu2,p1,p2,p3,p4
CREATE p5=(org1)-[:属于]->(idu1) WITH org1,idu1,idu2,p1,p2,p3,p4,p5
CREATE p6=(idu1)-[:上下游]->(idu2) WITH p1,p2,p3,p4,p5,p6
WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json
RETURN olab.schema.auto.cypher(json,100) AS cypher
代码语言:javascript复制
CREATE (org1:公司) SET org1.name='公司' WITH org1
CREATE (org2:公司) SET org2.name='公司' WITH org1,org2
CREATE (org3:公司) SET org3.name='公司' WITH org1,org2,org3
CREATE (loc:位置) SET loc.name='位置' WITH org1,org2,org3,loc
CREATE (pro:产品) SET pro.name='产品' WITH org1,org2,org3,loc,pro
CREATE (idu1:行业) SET idu1.name='行业' WITH org1,org2,org3,loc,pro,idu1
CREATE (idu2:行业) SET idu2.name='行业' WITH org1,org2,org3,loc,pro,idu1,idu2
CREATE p1=(org1)-[:持股]->(org2) WITH org1,org2,org3,loc,pro,idu1,idu2,p1
CREATE p2=(org1)-[:担保]->(org3) WITH org1,loc,pro,idu1,idu2,p1,p2
CREATE p3=(org1)-[:属于]->(loc) WITH org1,pro,idu1,idu2,p1,p2,p3
CREATE p4=(org1)-[:生产]->(pro) WITH org1,idu1,idu2,p1,p2,p3,p4
CREATE p5=(org1)-[:属于]->(idu1) WITH org1,idu1,idu2,p1,p2,p3,p4,p5
CREATE p6=(idu1)-[:上下游]->(idu2) WITH p1,p2,p3,p4,p5,p6
WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json
WITH olab.schema.auto.cypher(json,0,10,true) AS cypher
CALL apoc.cypher.run(cypher,{}) YIELD value RETURN value.graph AS graph

•多环路

代码语言:javascript复制
CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json
RETURN olab.schema.auto.cypher(json,100) AS cypher
代码语言:javascript复制
CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json
WITH olab.schema.auto.cypher(json,-1,100,true) AS cypher
CALL apoc.cypher.run(cypher,{}) YIELD value RETURN value.graph AS graph

•多环路

代码语言:javascript复制
  CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7
CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8
WITH olab.convert.json([p1,p2,p3,p4,p5,p6,p7,p8]) AS json
RETURN olab.schema.auto.cypher(json,100) AS cypher
代码语言:javascript复制
CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7
CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8
WITH olab.convert.json([p1,p2,p3,p4,p5,p6,p7,p8]) AS json
WITH olab.schema.auto.cypher(json,-1,100) AS cypher
CALL apoc.cypher.run(cypher,{}) YIELD value RETURN value.graph AS graph

7.4 从graph data解析图模式

定义一个包含nodesrelationshipsgraph data,将该数据转换为CYPHER语句。

代码语言:javascript复制
WITH '{"graph": {"nodes": [{"id": "256","labels": ["行业"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "gh_ind_rel_company_guarantee_company","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "250","labels": ["公司"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "251","labels": ["公司"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "252","labels": ["公司"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "253","labels": ["位置"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "254","labels": ["产品"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "255","labels": ["行业"],"properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]}],"relationships": [{"id": "314","type": "持股","startNode": "250","endNode": "251","properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "315","type": "担保","startNode": "250","endNode": "252","properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "316","type": "属于","startNode": "250","endNode": "253","properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "317","type": "生产","startNode": "250","endNode": "254","properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "318","type": "属于","startNode": "250","endNode": "255","properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]},{"id": "319","type": "上下游","startNode": "255","endNode": "256","properties_filter": [{"hcreatetime": "{var}.hcreatetime='20201116032333'"},{"count": "{var}.count>=0 AND {var}.count<=10"}],"es_filter": [{"es_url": "10.20.13.130:9200","index_name": "dl_default_indicator_def","query": "{size:1,query:{term:{entity_unique_code:{var}}}}"}]}]}}' AS json
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher

7.5 olab.schema.auto.cypher函数其它使用案例

•使用CYPHER查询到的子图生成子图匹配的CYPHER

代码语言:javascript复制
MATCH p0=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to1:持股]->(n1:公司) WITH n6,n5,n0,n1,p0 
MATCH p1=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to2:担保]->(n2:公司) WITH n6,n5,n0,n1,p0,n2,p1 
MATCH p2=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to3:属于]->(n3:位置) WITH n6,n5,n0,n1,p0,n2,p1,n3,p2 
MATCH p3=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to4:生产]->(n4:产品) WITH n6,n5,n0,n1,p0,n2,p1,n3,p2,n4,p3 
WITH olab.convert.json([p0,p1,p2,p3]) AS json LIMIT 1
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher
// RESULT:
// MATCH p0=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to1:持股]->(n1:公司) WITH n6,n5,n0,n1,p0 MATCH p1=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to2:担保]->(n2:公司) WITH n6,n5,n0,n1,p0,n2,p1 MATCH p2=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to3:属于]->(n3:位置) WITH n6,n5,n0,n1,p0,n2,p1,n3,p2 MATCH p3=(n6:行业)<-[r5to6:上下游]-(n5:行业)<-[r0to5:属于]-(n0:公司)-[r0to4:生产]->(n4:产品) WITH n6,n5,n0,n1,p0,n2,p1,n3,p2,n4,p3 RETURN {graph:[p0,p1,p2,p3]} AS graph LIMIT 100
代码语言:javascript复制
MATCH p0=(n6:行业)<-[r5to6:上下游]-(n5:行业) WITH p0 
WITH olab.convert.json(p0) AS json LIMIT 1
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher
// RESULT:
// MATCH p0=(n1:行业)<-[r0to1:上下游]-(n0:行业) WITH n1,n0,p0 RETURN {graph:[p0]} AS graph LIMIT 100
代码语言:javascript复制
MATCH p0=(n6:行业)<-[r5to6:上下游]-(n5:行业) WITH p0 
WITH olab.convert.json(p0) AS json LIMIT 1
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher
// RESULT:
// MATCH p0=(n1:行业)<-[r0to1:上下游]-(n0:行业) WITH n1,n0,p0 RETURN {graph:[p0]} AS graph LIMIT 100
代码语言:javascript复制
// 测试非联通的子图
MATCH p0=(n6:行业)<-[r5to6:上下游]-(n5:行业) WITH p0 
MATCH p1=(:Movie)-[]-(:Person) WITH p0,p1
WITH olab.convert.json([p0,p1]) AS json LIMIT 1
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher
// RESULT:
// Caused by: java.lang.IllegalArgumentException: The graph is not a weakly connected graph!
代码语言:javascript复制
// 测试环路子图
MATCH p0=(n:Movie)<-[:WROTE]-(p:Person)-[:ACTED_IN]->(n:Movie) WITH p0 
WITH olab.convert.json([p0]) AS json LIMIT 1
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher
// RESULT:
// MATCH p0=(n0:Movie)<-[r1to0:WROTE]-(n1:Person) WITH n0,n1,p0 
// MATCH p1=(n0:Movie)<-[r1to0:ACTED_IN]-(n1:Person) WITH n0,n1,p0,p1 
// RETURN {graph:[p0,p1]} AS graph LIMIT 100
代码语言:javascript复制
// 测试多环路子图
CREATE (n1:公司) SET n1.name='公司' WITH n1
CREATE (n2:公司) SET n2.name='公司' WITH n1,n2
CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3
CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4
CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1
CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2
CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3
CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4
CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5
CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6
CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7
CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8
WITH olab.convert.json([p1,p2,p3,p4,p5,p6,p7,p8]) AS json
RETURN olab.schema.auto.cypher(json,0,100,true) AS cypher
// RESULT:
// MATCH p0=(n0:公司)-[r0to3:属于]->(n3:行业)<-[r2to3:属于]-(n2:公司)<-[r1to2:担保]-(n1:公司) WITH n0,n3,n2,n1,p0 
// MATCH p1=(n0:公司)-[r0to3:属于]->(n3:行业)<-[r2to3:属于]-(n2:公司)<-[r1to2:持股]-(n1:公司) WITH n0,n3,n2,n1,p0,p1 
// MATCH p2=(n0:公司)-[r0to2:持股]->(n2:公司)<-[r1to2:持股]-(n1:公司) WITH n0,n3,n2,n1,p0,p1,p2 
// MATCH p3=(n0:公司)-[r0to2:持股]->(n2:公司)<-[r1to2:担保]-(n1:公司) WITH n0,n3,n2,n1,p0,p1,p2,p3 
// MATCH p4=(n0:公司)-[r0to2:担保]->(n2:公司)<-[r1to2:持股]-(n1:公司) WITH n0,n3,n2,n1,p0,p1,p2,p3,p4 
// MATCH p5=(n0:公司)-[r0to2:担保]->(n2:公司)<-[r1to2:担保]-(n1:公司) WITH n0,n3,n2,n1,p0,p1,p2,p3,p4,p5 
// MATCH p6=(n0:公司)-[r0to1:担保]->(n1:公司) WITH n0,n3,n2,n1,p0,p1,p2,p3,p4,p5,p6 
// MATCH p7=(n0:公司)-[r0to1:持股]->(n1:公司) WITH n0,n3,n2,n1,p0,p1,p2,p3,p4,p5,p6,p7 
// RETURN {graph:[p0,p1,p2,p3,p4,p5,p6,p7]} AS graph LIMIT 100

八、参考链接

该函数可以非常方便的进行以图搜图,系统端只需要定义好数据模型和传参方式即可。更多案例请查看ongdb-lab-apoc组件[3]

References

[1] TOC: 以图搜图-自动生成图模式匹配Cypher [2] 案例中使用的DEMO入参数据集下载: https://github.com/ongdb-contrib/ongdb-lab-apoc [3] ongdb-lab-apoc组件: https://github.com/ongdb-contrib/ongdb-lab-apoc

0 人点赞