[英]Can't filter nodes on cypher query Neo4j
I've got some data, like the following: 我有一些数据,如下所示:
(:Artist {id: 1, name: "Depeche Mode"})<-[:PLAYED_BY]-(:Song {id: 1, title: "Enjoy The Silence"})-[:PLAYED_BY]->(:Artist {id: 2, name: "Lacuna Coil"})
I will need to query, for example, for all the Song
that are PLAYED_BY
Artist with id
1 and 2. 例如,我将需要查询
id
1和2的所有PLAYED_BY
Artist Song
。
I was thinking about something like: 我在想类似的东西:
MATCH (s:Song)-[:PLAYED_BY]->(a:Artist)
WHERE a.id IN [1, 2]
RETURN s
But this one is not working as I want: because it's like an OR
, and I would like to use it like an AND
. 但这不是我想要的那样工作:因为它就像一个
OR
,我想像AND
使用它。
Any suggestions? 有什么建议么?
EDIT1: The number of Artist
should be arbitrary. 编辑1:
Artist
的数量应该是任意的。
EDIT2: I will also need to run this query using a full text index, like this: EDIT2:我还需要使用全文索引运行此查询,如下所示:
START song=node:node_auto_index("title:*enjo*")
MATCH ...
RETURN ...
That song is also played by other artists, for example Song 1
, played by Artist [1, 2, 3]
. 该歌曲也由其他艺术家播放,例如,由
Artist [1, 2, 3]
播放的Song 1
。 When I query for [1,2]
it should be returned anyway. 当我查询
[1,2]
,无论如何应该返回它。
This should work for your example: 这应该适用于您的示例:
MATCH (a1:Artist)<-[:PLAYED_BY]-(s:Song)-[:PLAYED_BY]->(a2:Artist)
WHERE a1.id = 1 AND a2.id = 2
RETURN s
So you want to find Songs that are played by both Artist 1 and Artist 2? 因此,您想查找歌手1和歌手2播放的歌曲吗?
How about this query: 该查询如何:
MATCH (a1:Artist)<-[:PLAYED_BY]-(s:Song)-[:PLAYED_BY]->(a2:Artist)
WHERE a1.id = 1 AND a2.id = 2
RETURN s
Edit 编辑
For an arbitrary number of Artists: 对于任意数量的艺术家:
WITH [1,2] AS ids
MATCH (a:Artist)<-[:PLAYED_BY]-(s:Song)
WITH collect(a.id) AS artistIds, ids,s
WHERE all(x IN artistIds WHERE x IN ids) AND all(x IN ids WHERE x IN artistIds)
RETURN s
Collect the ids of all artists that have played the song then use the Cypher collection predicate function all
to filter on only the desired Artist ids. 收集播放过歌曲的所有艺术家的ID,然后使用Cypher集合谓词
all
仅过滤所需的艺术家ID。
The array [1,2]
can be parameterized like this: WITH {id_array} AS ids ...
数组
[1,2]
可以像这样被参数化: WITH {id_array} AS ids ...
Edit 编辑
Using a legacy index for fuzzy text search should work with this approach. 使用旧式索引进行模糊文本搜索应适用于这种方法。 Just replace the unbound
(s:Song)
piece of the pattern in the MATCH with the bound song
variable populated by the legacy index: 只需将MATCH中未绑定
(s:Song)
模式的部分替换为由旧索引填充的绑定song
变量:
START song=node:node_auto_index("title:*enjo*")
WITH [1,2] AS ids, song
MATCH (a:Artist)<-[:PLAYED_BY]-(song)
WITH collect(a.id) AS artistIds, ids, song
WHERE all(x IN artistIds WHERE x IN ids) AND all(x IN ids WHERE x IN artistIds)
RETURN s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.