Skip to Content
Neo4j:权威指南
book

Neo4j:权威指南

by Luanne Misquitta, Christophe Willemsen
July 2025
Beginner to intermediate
410 pages
5h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Neo4j:权威指南

第 4 章 建模与重构模式 建模和重构模式

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

既然你已经掌握了图形建模的原理,那么就可以探索其他一些建模模式了。你将学会识别何时应用这些模式,并权衡它们的利弊。随着用例和模型的发展,您迟早还需要重构您的图,因此本章后半部分将以实践的方式带您了解重构模式。

Hyperedges:N 向关系

现在有一种新趋势--似乎每个人都对翻唱歌曲感兴趣。ElectricHarmony 也想加入这一行列,推出了一项新功能,帮助听众轻松找到他们喜爱的歌曲的翻唱。这项功能还能帮助听众发现自己喜爱的歌曲中是否有翻唱作品,如果有,原作者是谁。你发现自己又一次和团队一起在白板上为这个用例建模。

最明显的开始方式如图 4-1 所示:在ARTISTTrack 的关系上说明艺术家录制的曲目是否是翻唱。

图 4-1. 量化ARTIST 关系以表示曲目是否为翻唱曲目

良好的建模实践包括重新审视现有的用例,看看重构是否对它们产生了影响(也许是负面的)。您会立即回想起一个关键用例--获取曲目艺术家。到目前为止,ElectricHarmony 倾向于始终显示曲目的原作者,而您不希望改变这一点。然而,由于该信息包含在关系的一个属性中,Neo4j 需要做额外的工作来遍历TRACK 中的所有ARTIST 关系,检查cover 属性值,并丢弃大部分。虽然这不是一个大问题(因为曲目中艺术家数量极多的情况很少见),但您的团队认为这不是一个正确的解决方案。

相反,根据在前一章中学到的关于关系粒度的知识,你们决定引入一种新的关系类型:COVERED_BY (见图 4-2)。

图 4-2. COVERED_BY 是一种新引入的关系类型,用于表示艺术家是否翻唱过某首歌曲。

这样,关键用例就不会受到影响。当 Neo4j 需要获取封面--或者反过来说,查找某个艺术家翻唱过的所有曲目时,它会遍历COVERED_BY 关系。

那么,如何查找演唱过该曲目任何版本的艺术家呢?只需在模式中包含这两种关系类型,Neo4j 就会遍历这两种关系:

MATCH (t:Track {id:"4J4gApJKSC0himDViFotdy"})-[:ARTIST|COVERED_BY]-(a:Artist)
RETURN a

在白板上画一些例子(如图 4-3 所示)来验证这种方法是否有效。

图 4-3. 使用新模型表示一些数据以验证用例

确实有效:很容易就能找到原作者和其他演唱过这些曲目的歌手。但它真的有效吗?你现在是否在问你的团队,是 Simon & Garfunkel 还是 Disturbed 录制了《不朽》这张专辑?发生了什么?

模型现在已经丢失了信息:不再可能将专辑与曲目的特定版本联系起来。我们需要的是一个

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

基于scikit-learn和PyTorch的实践机器学习

基于scikit-learn和PyTorch的实践机器学习

Aurélien Géron
企业级Java开发中的应用人工智能 (Chinese Edition)

企业级Java开发中的应用人工智能 (Chinese Edition)

Alex Soto Bueno, Markus Eisele, Natale Vinto

Publisher Resources

ISBN: 9798341664852