简体   繁体   English

Neo4J 将结果组合在一起,而不是基于匹配/关系的多个结果

[英]Neo4J Group results together rather than multiple results based on matches/relationships

I have a pretty 'simple' Graph which I interact with via Spring Data (and the Neo4J browser).我有一个非常“简单”的图表,我通过 Spring 数据(和 Neo4J 浏览器)与之交互。

Currently the Graph has nodes of Books, Publishers and Authors (with relations AUTHOR_OF and PUBLISHES respectivly).目前,Graph 具有 Books、Publishers 和 Authors 节点(分别具有 AUTHOR_OF 和 PUBLISHES 关系)。 A Book can have a single Publisher and multiple Authors.一本书可以有一个出版商和多个作者。

I'm trying to write a query to find all potential books, their authors and their publisher (if they have them) based on various matches: WHERE unique Id match OR does it match the ISBN OR if the book has an author does it match on book title AND author.我正在尝试编写查询以根据各种匹配项查找所有潜在的书籍、作者和出版商(如果有的话):WHERE unique Id match OR does it match the ISBN OR if the book has an author it match关于书名和作者。

However, the result set returned seems to vary based on the number of relationships as well as the number of criteria it's matched.但是,返回的结果集似乎会根据关系的数量以及匹配的条件数量而有所不同。

My sample data is as follows:我的样本数据如下:

Neo4J 样本数据

MATCH (bo:Book)
OPTIONAL MATCH (au:Author)-[r:AUTHOR_OF]->(bo:Book)<-[s:PUBLISHES]-(pu:Publisher)
RETURN bo, r, au, s, pu

Returns three records:返回三个记录:

  1. Book 1 with Publisher 'Establishment' and Author 'Doe'出版商“Establishment”和作者“Doe”的第 1 册
  2. Book 1 with Publisher 'Establishment' and Author 'Bloggs第 1 册,出版商为“Establishment”,作者为“Bloggs”
  3. Book 2书 2
MATCH (bo:Book)
WHERE bo.bookId={book 1 ID}
OPTIONAL MATCH (au:Author)-[r:AUTHOR_OF]->(bo:Book)
OPTIONAL MATCH (bo:Book)<-[s:PUBLISHES]-(pu:Publisher)
RETURN bo, r, au, s, pu

Returns two records.返回两条记录。

  1. Book 1 with Publisher 'Establishment' and Author 'Doe'出版商“Establishment”和作者“Doe”的第 1 册
  2. Book 1 with Publisher 'Establishment' and Author 'Bloggs'出版商“Establishment”和作者“Bloggs”的第 1 册
MATCH (bo:Book)
WHERE bo.bookId={book 2 ID}
OPTIONAL MATCH (au:Author)-[r:AUTHOR_OF]->(bo:Book)
OPTIONAL MATCH (bo:Book)<-[s:PUBLISHES]-(pu:Publisher)
RETURN bo, r, au, s, pu

Returns Book 2返回书 2

MATCH (bo:Book)
WHERE bo.bookId={book 2 ID}
OR bo.isbn={book 1 ISBN}
OPTIONAL MATCH (au:Author)-[r:AUTHOR_OF]->(bo:Book)
OPTIONAL MATCH (bo:Book)<-[s:PUBLISHES]-(pu:Publisher)
RETURN bo, r, au, s, pu

Returns three records返回三个记录

  1. Book 1 with Publisher 'Establishment' and Author 'Doe'出版商“Establishment”和作者“Doe”的第 1 册
  2. Book 1 with Publisher 'Establishment' and Author 'Bloggs第 1 册,出版商为“Establishment”,作者为“Bloggs”
  3. Book 2书 2

So I appear to be on the right track... However there are couple of things I'm now stumped on.所以我似乎走在了正确的轨道上......但是我现在遇到了几件事。

  1. How to group the results.如何对结果进行分组。 I want to receive Book 1 with publisher 'Establishment' and Authors 'Doe' and 'Bloggs'我想收到出版商“Establishment”和作者“Doe”和“Bloggs”的书 1
  2. **Add a final or, which matches if theres an author, mathches on author name and book title. **添加最后一个或,如果存在作者,则匹配作者姓名和书名。 This should also return all authors of the book, not just the one it matched on.这也应该返回这本书的所有作者,而不仅仅是它匹配的作者。

I expect the answer lies within a combination of WITH , COLLECT() and DISTINCT but I haven't been successful .我希望答案在于WITHCOLLECT()DISTINCT的组合,但我没有成功

Try something like this, you will need to COLLECT , to collect all authors in a list:尝试这样的事情,你需要COLLECT来收集列表中的所有作者:

MATCH (bo:Book)
WHERE bo.bookId={bookID} OR bo.isbn={bookISBN} OR (bo.title={bookTitle} AND (bo)<-[:AUTHOR_OF]-(:Author{name:{authorName}}))
OPTIONAL MATCH (au:Author)-[r:AUTHOR_OF]->(bo:Book)
OPTIONAL MATCH (bo:Book)<-[s:PUBLISHES]-(pu:Publisher)
RETURN bo, pu, collect(au) AS authors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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