常用CYPHER查询(五)

 

Here’s the table of contents:

  1. Graph RAG 通常引用的格式
  2. 多标签并发查询
  3. 将查询结果转换为树结构
  4. 日期处理

Graph RAG 通常引用的格式

MATCH p=(n:图谱大纲)-[r*..2]-()
WHERE ANY(word IN ['cpi'] WHERE n.topic CONTAINS UPPER(word))
WITH NODES(p) AS nodes, RELATIONSHIPS(p) AS rels
RETURN REDUCE(s = '', i IN RANGE(0, SIZE(nodes) - 2) | s + nodes[i].topic + ', ' + (CASE WHEN STARTNODE(rels[i]) = nodes[i] THEN '-[' + TYPE(rels[i]) + ']->, ' ELSE '<-[' + TYPE(rels[i]) + ']-, ' END)) + LAST(nodes).topic AS path;
"与核心CPI的关系, <-[RELATED_TO]-, 总量逻辑"
"与核心CPI的关系, <-[RELATED_TO]-, 总量逻辑, -[RELATED_TO]->, 美林时钟框架"
https://colab.research.google.com/drive/1tLjOg2ZQuIClfuWrAC2LdiZHCov8oUbs#scrollTo=EniVfVn5U3fC
Entities processed: ['Rocket']
Entities processed: ['rockets', 'rocket']
Graph RAG context:
The following are knowledge sequence in max depth 2 in the form of directed graph like:
`subject -[predicate]->, object, <-[predicate_next_hop]-, object_next_hop ...` extracted based on key entities as subject:
Rocket, -[reunites with]->, Teefs, <-[voices]-, Asim Chaudhry
Rocket, -[reunites with]->, Lylla, -[tells]->, him
Rocket, -[flatlines]->, near-death experience
Rocket, -[is]->, genetically engineered raccoon-based bounty hunter
Rocket, -[is]->, member of the Guardians
Rocket, -[is]->, former Avenger
Rocket, -[is]->, master of weapons and military tactics
Rocket, -[reunites with]->, Floor, <-[voices]-, Mikaela Hoover
Rocket, -[reunites with]->, Floor
Rocket, <-[is]-, Bradley Cooper
Rocket, -[reunites with]->, Lylla
Rocket, -[reunites with]->, Teefs
Rocket, -[is]->, member of the Guardians, <-[is]-, Groot

多标签并发查询

CALL db.labels() YIELD label WITH COLLECT(label) AS lbs
CALL apoc.cypher.parallel(
	'WITH $name AS name,LOWER($word) AS word CALL apoc.cypher.run(\'MATCH (node:\'+name+\') WHERE node.name=$word RETURN DISTINCT LABELS(node)[0] AS label,node.name AS name LIMIT 1\',{word:word}) YIELD value RETURN DISTINCT value',
    {name:lbs,word:'ppi'},
    'name')
YIELD value RETURN value

将查询结果转换为树结构

MATCH p=(:产品)-[:子类*..6]->(:产品)
WITH COLLECT(p) AS data
CALL apoc.convert.toTree(data,false,{nodes: {产品: ['name']}}) YIELD value
RETURN value
// 修改关系名称后输出
MATCH p=(:产品)-[:子类*..6]->(:产品)
WITH RELATIONSHIPS(p) AS rels
UNWIND rels AS rel
WITH startNode(rel) AS start,endNode(rel) AS end
CALL apoc.create.vRelationship(start, 'children', {}, end) YIELD rel
WITH (start)-[rel]->(end) AS paths
UNWIND paths AS path
WITH COLLECT(path) AS p_list
CALL apoc.convert.toTree(p_list) YIELD value
WITH value
MATCH (n:产品) WHERE n.name=value.name AND NOT (()-[:子类]->(n))
RETURN value;

日期处理

//获取10天前的一个日期并格式化为数字
RETURN TOINTEGER(apoc.temporal.format((DATE()-DURATION({days:10})),'YYYYMMdd')) AS date;