JC R
JC R

Reputation: 333

Cypher - Return nodes with their relation with a count contraint on the relation

This question is related to that one.

I explain my little usecase to better understand my problem :

I have this kind of graph (doc)-[contain]-(sentence)-[with]-(word)

Nodes (word) can have semantic relations between them (word)-[rel]-(word)

I want to return (doc) with a number of [rel] > 10

This query can do that :

MATCH (doc:document)-[contain]-(sentence)-[with]-(ng1:word)-[rel:relations]-(ng2:word)
WITH doc, count(rel) as nbRels, collect(rel) as rels
WHERE nbRels > 10 RETURN doc, nbRels, rels

But I don't know what (word) are concerned by each [rel] found.

Is it possible to do know and to return that?

Thanks,

Upvotes: 1

Views: 240

Answers (1)

JohnMark13
JohnMark13

Reputation: 3739

Untested, but you should be able to unwind your rels collection and pull out the start and end nodes. Something like:

MATCH (doc:document)-[contain]-(sentence)-[with]-(ng1:word)-[rel:relations]-(ng2:word)
WITH doc, count(rel) as nbRels, collect(rel) as rels
WHERE nbRels > 10
WITH doc, UNWIND(rels) as rel,
RETURN doc, STARTNODE(rel), ENDNODE(rel)

But you might just want to collect something different in the first place:

MATCH (doc:document)-[contain]-(sentence)-[with]-(ng1:word)-[rel:relations]-(ng2:word)
WITH doc, ng1, collect(ng2) as rels
WHERE LENGTH(rels) > 10 
RETURN doc, ng1, rels

Upvotes: 1

Related Questions