vendredi 2 février 2018

Cypher - return 1 'random' node from specific relationships

Hello fellow stackers,

Trying to build a Cypher query that returns ONE entry per 'comic'.

Basic structure is as follows:

(Comic)<-[]-(Media)-[]->(MediaType)

I want to grab 3 'random' comics and return only one Media per comic of said type. As a comic may have 100 medias of the media type, it's a little concerning. I could break this down into multiple queries, but I am sure there's a way for Cypher to do this through its own system.

My original code would do what it needed to, but if there were multiple medias of the same type, one comic would had the possibility of being returned multiple times.

The original Code:

MATCH (mt:MediaType{Name: {mediatype} })
<-[:Is_Media_Of]-(m:Media)-[:Is_Media_For]->(w:Webcomic)
WHERE w.ComicID <> toInt({comicid})
AND m.Status = 'Active'
RETURN m.URL as ImgURL, 
w.Name as ComicName, 
rand() as r 
ORDER BY r ASC Limit toInt({count}) ",
["mediatype"=>$AdType,"count"=>$Count,"comicid"=>$ComicID])

This would sometimes return:

'comic1' 'img'

'comic2' 'img'

'comic1' 'img2'

'comic3' 'img2'

'comic4' 'img1'

'comic3' 'img5'

I'm sure it's pretty straight forward and I'm missing something. Any help is greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire