简体   繁体   English

如何在Cypher查询中包含特定的关系计数

[英]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 更多细节

  • The :Keywords form a directed, acyclic tree with the :Category at the root. :Keywords形成一个有向非循环树,其:Category位于根目录。
  • Each :Keyword has 0 or more :Content items. 每个:Keyword具有0或多个:Content项目。
  • Although every :Keyword has exactly one parent :Keyword (or the :Category), the :Keyword ↔️ :Content relationship is many-to-many. 尽管每个:Keyword都只有一个父:Keyword(或:Category),但:Keyword↔️:Content关系是多对多的。

Query Requirements 查询要求

  • I'm hoping to make this a single query based on knowing only the ID for the selected node, shown in red: 🔴. 我希望基于仅知道所选节点的ID(以红色显示)的方式使它成为单个查询:🔴。
  • Counts show number of distinct content nodes attached to the selected Keyword's sub-tree. 计数显示附加到所选关键字的子树的不同内容节点的数量。 See the 3rd image attachment for example counts. 有关计数示例,请参见第3个图像附件。
  • The query should include the black content 🔹's as shown as the goal is to display thumbnail images of all content directly tagged with the current selection. 查询应包括黑色内容🔹,如图所示,因为目标是显示直接用当前选择标记的所有内容的缩略图。

Creating your graph: 创建图形:

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);

Graphical representation. 图示。

Selecting your sub graph: 选择子图:

  1. defining your starting point, the category 定义起点,类别
  2. any number of keywords until reaching your selected keyword 任意数量的关键字,直到达到您选择的关键字
  3. defining your selected keyword 定义您选择的关键字
  4. any number of keywords until reaching a content node 任意数量的关键字,直到到达内容节点
  5. hand over the parameter (id) for your selected keyword 移交所选关键字的参数(id)
  6. retrieve all identified nodes of step 1 to 5 检索步骤1至5的所有已标识节点

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;

Your desired solution: 您想要的解决方案:

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;

Update: query for keyword related content counts 更新:查询与关键字相关的内容计数

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM