我最近开始使用Neo4j / cypher,并且能够成功构建想到的大多数基本查询,但是解决该问题的方法使我无所适从。
节点具有非常简单的关系模型:书籍分为类别
这些书将是唯一的,并且可以与多个类别相关。
我的基本查询将收集类别,从而产生一组具有相关类别的书籍:
match (c:Category)-[:contains]-(b:Book)
return b as book, collect(distinct c) as categories
然后我可以收集这些书,得到一组相关的书和类别:
match (c:Category)-[:contains]-(b:Book)
with b, collect(distinct c) as categories
return collect(distinct b) as books, categories
这似乎是朝着正确的方向发展,但整个过程中都有许多重复的书籍和类别。这是一个伪示例:
Books Categories
-----------------------------------------------
[Easy Home Updates] [Home and Garden]
-----------------------------------------------
[Gardening Today, [Outdoors,
Gardening for Kids, Hobbies,
Green Thumb Made Easy] Gardening]
-----------------------------------------------
[Conversational Spanish, [Spanish,
Spanish for Travelers, Travel,
Advanced Spanish] Language]
-----------------------------------------------
[Gardening Today, [Gardening,
Gardening for Kids] Kids]
-----------------------------------------------
[Home Improvement, [Home Improvement,
Easy Home Updates, Home and Garden,
Family Home Projects] Family]
-----------------------------------------------
[Gardening Today] [Gardening]
-----------------------------------------------
[Conversational Spanish, [Language,
Advanced Spanish] Spanish]
我似乎无法找到一种方法,可以在初始匹配中使用过滤或reduce和apoc函数聚合重复项。
理想的结果是减少书籍和类别收藏。像这样:
Books Categories
----------------------------------------------
[Gardening Today, [Gardening,
Gardening for Kids, Outdoors,
Green Thumb Made Easy] Hobbies,
Kids,
Family]
----------------------------------------------
[Conversational Spanish, [Spanish,
Spanish for Travelers, Language,
Advanced Spanish] Travel,
Education]
----------------------------------------------
[Home Improvement, [Home and Garden,
Easy Home Updates, Home Improvement,
Family Home Projects] Construction]
或者我的方法已经完全失效,并且有一种更好,更有效的方式对相关节点进行分组。
任何能为您指出正确方向的帮助将不胜感激。如果需要进一步说明,请告诉我。
答案 0 :(得分:0)
为便于进一步解答和解决方案,我注意到了我的图形创建语句:
CREATE
(categoryHome:Category {name: 'Home and Garden'}),
(categoryOutdoor:Category {name: 'Outdoors'}),
(categoryHobby:Category {name: 'Hobbies'}),
(categoryGarden:Category {name: 'Gardening'}),
(categorySpanish:Category {name: 'Spanish'}),
(categoryTravel:Category {name: 'Travel'}),
(categoryLanguage:Category {name: 'Language'}),
(categoryKids:Category {name: 'Kids'}),
(categoryImprovement:Category {name: 'Home Improvement'}),
(categoryFamily:Category {name: 'Family'}),
(book1:Book {name: 'Easy Home Updates'}),
(book2:Book {name: 'Gardening Today'}),
(book3:Book {name: 'Gardening for Kids'}),
(book4:Book {name: 'Green Thumb Made Easy'}),
(book5:Book {name: 'Conversational Spanish'}),
(book6:Book {name: 'Spanish for Travelers'}),
(book7:Book {name: 'Advanced Spanish'}),
(book8:Book {name: 'Home Improvement'}),
(book9:Book {name: 'Easy Home Updates'}),
(book10:Book {name: 'Family Home Projects'}),
(categoryHome)-[:CONTAINS]->(book1),
(categoryHome)-[:CONTAINS]->(book8),
(categoryHome)-[:CONTAINS]->(book9),
(categoryHome)-[:CONTAINS]->(book10),
(categoryOutdoor)-[:CONTAINS]->(book2),
(categoryOutdoor)-[:CONTAINS]->(book3),
(categoryOutdoor)-[:CONTAINS]->(book4),
(categoryHobby)-[:CONTAINS]->(book2),
(categoryHobby)-[:CONTAINS]->(book3),
(categoryHobby)-[:CONTAINS]->(book4),
(categoryGarden)-[:CONTAINS]->(book2),
(categoryGarden)-[:CONTAINS]->(book3),
(categoryGarden)-[:CONTAINS]->(book4),
(categorySpanish)-[:CONTAINS]->(book5),
(categorySpanish)-[:CONTAINS]->(book6),
(categorySpanish)-[:CONTAINS]->(book7),
(categoryTravel)-[:CONTAINS]->(book5),
(categoryTravel)-[:CONTAINS]->(book6),
(categoryTravel)-[:CONTAINS]->(book7),
(categoryLanguage)-[:CONTAINS]->(book5),
(categoryLanguage)-[:CONTAINS]->(book6),
(categoryLanguage)-[:CONTAINS]->(book7),
(categoryKids)-[:CONTAINS]->(book2),
(categoryKids)-[:CONTAINS]->(book3),
(categoryImprovement)-[:CONTAINS]->(book8),
(categoryImprovement)-[:CONTAINS]->(book9),
(categoryImprovement)-[:CONTAINS]->(book10),
(categoryFamily)-[:CONTAINS]->(book8),
(categoryFamily)-[:CONTAINS]->(book9),
(categoryFamily)-[:CONTAINS]->(book10);
在我看来,您的技术实施是正确的,但是从专业角度来看,您的要求不一致。 让我们选择一个例子。您期望获得以下记录:
BOOKS: CATEGORIES:
Gardening Today, Gardening,
Gardening for Kids, Outdoors,
Green Thumb Made Easy Hobbies,
Kids,
Family
通过执行以下Cypher查询,Family
条目不是书籍Gardening Today
的有效类别。
MATCH (book:Book {name: 'Gardening Today'})<-[:CONTAINS]-(category:Category)
RETURN DISTINCT book.name, collect(category.name);
╒═════════════════╤═════════════════════════════════════════╕
│"book.name" │"collect(category.name)" │
╞═════════════════╪═════════════════════════════════════════╡
│"Gardening Today"│["Kids","Gardening","Hobbies","Outdoors"]│
└─────────────────┴─────────────────────────────────────────┘
进行交叉检查,确认类别Family
包含其他书籍。
MATCH (category:Category {name: 'Family'})-[:CONTAINS]->(book:Book)
RETURN DISTINCT category.name, collect(book.name);
╒═══════════════╤═══════════════════════════════════════════════════════════════╕
│"category.name"│"collect(book.name)" │
╞═══════════════╪═══════════════════════════════════════════════════════════════╡
│"Family" │["Family Home Projects","Easy Home Updates","Home Improvement"]│
└───────────────┴───────────────────────────────────────────────────────────────┘
此过程继续传播。这就是为什么您按预期获得不同切片结果集的原因。因此,您已经实施的方法是正确的:
MATCH path = (category:Category)-[:CONTAINS]->(book:Book)
WITH collect(category.name) AS categoryGroup, book.name AS bookName
RETURN categoryGroup, collect(bookName);
╒═════════════════════════════════════════════════════════════════╤═════════════════════════════════════════════════════════════════════╕
│"categoryGroup" │"collect(bookName)" │
╞═════════════════════════════════════════════════════════════════╪═════════════════════════════════════════════════════════════════════╡
│["Spanish","Travel","Language"] │["Spanish for Travelers","Advanced Spanish","Conversational Spanish"]│
├─────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────┤
│["Home Improvement","Family","Home and Garden","Home and Garden"]│["Easy Home Updates"] │
├─────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────┤
│["Hobbies","Gardening","Kids","Outdoors"] │["Gardening Today","Gardening for Kids"] │
├─────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────┤
│["Hobbies","Gardening","Outdoors"] │["Green Thumb Made Easy"] │
├─────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────┤
│["Home Improvement","Family","Home and Garden"] │["Home Improvement","Family Home Projects"] │
└─────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────┘
由于所请求的映射违反了分配规则(集合论),因此我们无法使用通常的模式匹配。取而代之的是,我们可以通过技巧找到目标书中所有连接的节点,然后进行准备。
请确保您已安装Neo4j APOC library。
MATCH (selectedBook:Book)
WHERE selectedBook.name = 'Gardening for Kids'
CALL apoc.path.subgraphNodes(selectedBook, {uniqueness: 'NODE_GLOBAL'}) YIELD node
WITH collect(DISTINCT node) AS subgraphNodes
WITH
filter (node IN subgraphNodes
WHERE node:Category) AS categories,
filter (node IN subgraphNodes
WHERE node:Book) AS books
WITH categories, books
UNWIND categories AS category
UNWIND books AS book
RETURN collect(DISTINCT category.name) AS categoryNames, collect(DISTINCT book.name) AS bookNames;
Category
和Book
对已标识的节点进行排序简易家庭更新:
╒═══════════════════════════════════════════════╤═══════════════════════════════════════════════════════════════╕
│"categoryNames" │"bookNames" │
╞═══════════════════════════════════════════════╪═══════════════════════════════════════════════════════════════╡
│["Home and Garden","Family","Home Improvement"]│["Easy Home Updates","Family Home Projects","Home Improvement"]│
└───────────────────────────────────────────────┴───────────────────────────────────────────────────────────────┘
儿童园艺:
╒════════════════════════════════════════╤════════════════════════════════════════╕
│"categoryNames" │"bookNames" │
╞════════════════════════════════════════╪════════════════════════════════════════╡
│["Kids","Gardening","Hobbies","Outdoors"│["Gardening for Kids","Gardening Today",│
│] │"Green Thumb Made Easy"] │
└────────────────────────────────────────┴────────────────────────────────────────┘