[英]How to include specific relationship counts in Cypher query
Given this schema… 给定这种模式...
…and this sample data set… …以及此样本数据集…
…how can I write a Cypher query that will return this data, including the counts… …我该如何编写一个Cypher查询来返回此数据,包括计数……
More details 更多细节
Query Requirements 查询要求
The first statement creates the nodes, the second the relationships between them. 第一条语句创建节点,第二条语句创建它们之间的关系。
CREATE
(Root:Category {name: 'Letters'}),
(KeywordA:KeyWord {name: 'A'}),
(KeywordAA:KeyWord {name: 'A A'}),
(KeywordAB:KeyWord {name: 'A B'}),
(KeywordAC:KeyWord {name: 'A C'}),
(KeywordC:KeyWord {name: 'C'}),
(KeywordCA:KeyWord {name: 'C A'}),
(KeywordCB:KeyWord {name: 'C B'}),
(KeywordD:KeyWord {name: 'D'}),
(KeywordDA:KeyWord {name: 'D A'}),
(KeywordB:KeyWord {name: 'B'}),
(KeywordBA:KeyWord {name: 'B A'}),
(KeywordBAA:KeyWord {name: 'B A A'}),
(KeywordBAB:KeyWord {name: 'B A B'}),
(KeywordBAC:KeyWord {name: 'B A C'}),
(KeywordBB:KeyWord {name: 'B B'}),
(KeywordBBA1:KeyWord {name: 'B B A'}),
(KeywordBBA2:KeyWord {name: 'B B A'}),
(KeywordBBA3:KeyWord {name: 'B B A'}),
(KeywordBBA4:KeyWord {name: 'B B A'}),
(KeywordBBA5:KeyWord {name: 'B B A'}),
(Content18:Content {name: '18'}),
(Content19A:Content {name: '19'}),
(Content19B:Content {name: '19'}),
(Content20:Content {name: '20'}),
(Content1:Content {name: '1'}),
(Content2:Content {name: '2'}),
(Content3A:Content {name: '3'}),
(Content3B:Content {name: '3'}),
(Content4:Content {name: '4'}),
(Content5:Content {name: '5'})
CREATE
(Root)-[:CONTAINS]->(KeywordA),
(KeywordA)-[:CONTAINS]->(KeywordAA),
(KeywordA)-[:CONTAINS]->(KeywordAB),
(KeywordA)-[:CONTAINS]->(KeywordAC),
(Root)-[:CONTAINS]->(KeywordC),
(KeywordC)-[:CONTAINS]->(KeywordCA),
(KeywordC)-[:CONTAINS]->(KeywordCB),
(Root)-[:CONTAINS]->(KeywordD),
(KeywordD)-[:CONTAINS]->(KeywordDA),
(Root)-[:CONTAINS]->(KeywordB),
(KeywordB)-[:CONTAINS]->(KeywordBA),
(KeywordBA)-[:CONTAINS]->(KeywordBAA),
(KeywordBA)-[:CONTAINS]->(KeywordBAB),
(KeywordBA)-[:CONTAINS]->(KeywordBAC),
(KeywordB)-[:CONTAINS]->(KeywordBB),
(KeywordBB)-[:CONTAINS]->(KeywordBBA1),
(KeywordBB)-[:CONTAINS]->(KeywordBBA2),
(KeywordBB)-[:CONTAINS]->(KeywordBBA3),
(KeywordBBA2)-[:CONTAINS]->(KeywordBBA4),
(KeywordBBA2)-[:CONTAINS]->(KeywordBBA5),
(KeywordCB)-[:DESCRIBES]->(Content18),
(KeywordDA)-[:DESCRIBES]->(Content19B),
(KeywordBAA)-[:DESCRIBES]->(Content19A),
(KeywordBAA)-[:DESCRIBES]->(Content1),
(KeywordBAB)-[:DESCRIBES]->(Content20),
(KeywordBB)-[:DESCRIBES]->(Content1),
(KeywordBB)-[:DESCRIBES]->(Content2),
(KeywordBB)-[:DESCRIBES]->(Content3A),
(KeywordBBA1)-[:DESCRIBES]->(Content4),
(KeywordBBA1)-[:DESCRIBES]->(Content5),
(KeywordBBA4)-[:DESCRIBES]->(Content5),
(KeywordBBA5)-[:DESCRIBES]->(Content3B);
Statement: 声明:
// |----------- (1) -----------| |--- (2) --| |--------- (3) ---------| |--------- (4) --------|
MATCH keywordPath = (:Category {name: 'Letters'})-[:CONTAINS*]->(selectedKeyword:KeyWord)-[:CONTAINS*]->(:KeyWord)
// |-------- (5) ---------|
WHERE id(selectedKeyword) = 15
UNWIND
// |------------ (6) -----------|
nodes(keywordPath) AS keywordNode
MATCH contentPath = (keywordNode:KeyWord)-[contentRelationship:DESCRIBES]->(contentNode:Content)
RETURN keywordPath,contentPath;
As far as I understood, you are interested in the relationships towards the content nodes to display according thumbnails. 据我了解,您对与要根据缩略图显示的内容节点的关系感兴趣。 You can retrieve them by the following Cypher statement: 您可以通过以下Cypher语句检索它们:
MATCH keywordPath = (:Category {name: 'Letters'})-[:CONTAINS*]->(selectedKeyword:KeyWord)-[:CONTAINS*]->(:KeyWord)
WHERE id(selectedKeyword) = 15
UNWIND
nodes(keywordPath) AS keywordNode
MATCH contentPath = (keywordNode:KeyWord)-[contentRelationship:DESCRIBES]->(contentNode:Content)
RETURN contentPath;
Statement: 声明:
MATCH keywordPath = (:Category {name: 'Letters'})-[:CONTAINS*]->(selectedKeyword:KeyWord)-[:CONTAINS*]->(:KeyWord)
WHERE id(selectedKeyword) = 15
UNWIND
nodes(keywordPath) AS keywordNode
WITH DISTINCT keywordNode
MATCH contentPath = (keywordNode:KeyWord)-[*]->(contentNode:Content)
RETURN keywordNode, count(DISTINCT contentNode);
Result 结果
╒════════════════╤═════════════════════════════╕
│"keywordNode" │"count(DISTINCT contentNode)"│
╞════════════════╪═════════════════════════════╡
│{"name":"B B A"}│2 │
├────────────────┼─────────────────────────────┤
│{"name":"B B A"}│1 │
├────────────────┼─────────────────────────────┤
│{"name":"B B A"}│2 │
├────────────────┼─────────────────────────────┤
│{"name":"B B A"}│1 │
├────────────────┼─────────────────────────────┤
│{"name":"B"} │8 │
├────────────────┼─────────────────────────────┤
│{"name":"B B"} │6 │
└────────────────┴─────────────────────────────┘
ON HOLD If I understand your question right. 待命如果我理解你的问题是对的。
This query can help you: 该查询可以帮助您:
MATCH (n:Keyword {name:"B B"})-[*1..6]->(c:Content)
RETURN COUNT(DISTINCT c)
You can change 1..6
, but set relation to [*]
is great time consuming base on db schema, It's better to set a range for traversing relations. 您可以更改1..6
,但是基于数据库模式将[*]
设置为关系非常耗时,最好设置一个遍历关系的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.