Cypher query to return multiple groups of nodes, count of specific group and then a property from the first node in that each group

Hello,

I did all kinds of reading the docs on cypher, and I still cannot figure this one out.. Any help would be greatly appreciated.

I originally created this query using graphql, but I am moving away from graphql, and now trying to do it in raw cypher.

The database is actually neo4j version 4.4..

(c:classification) <- [:IN_CLASSIFICATION] - (a:article) - [:IN_TOPIC] -> (t:topic)

the classification classifies the articles by news type (top stories, financial, political etc)

and all articles from multiple sources that are talking about the same story are grouped together by topic

so, for example, let's say top news of the day -- there are 23 articles talking about the same story so all articles would be grouped together with a groupnumber -- say 0 -- but within that group number 23 different articles would be linked by to the same groupnumber

So I want to be able to query the database for "top" news, and it returns all the groups that are classified as "top" news, the total number of articles in each group, and then I want the title of the first article in each group..

e.q:

------------------------------------------------------------------------------------------__
group | articles | title
0 23 "this is the first article title in group 0"
1 14 "this is the first article title in group 1"
2 7 "this is the first article title in group 2"

.
.
.
.

I've been able to get it to give me a list of all groups, and then the count for each group's articles, but whenever I try to get the first article in each group's title is where I'm stuck.. :frowning:

thanks in advance for any advice you can give me,

  • Andre

Can you provide your query

If the articles are added to a group in first-come-first-served basis, then:
match (a:Group)-[:ARTICLE]->(b:Articles)
where a.group = 0
with a, b, apoc.coll.sort(collect(elementId(b))) as ids
match (b)
WHERE elementId(b) = ids[0]
return b.article

Here is a working example:
merge (a:Group {name: "A"})
merge (b:Articles {article: "Article 1b"})
merge(a)-[:ARTICLE]->(b)
merge (c:Articles {article: "Article 1c"})
merge(a)-[:ARTICLE]->(c)
merge (d:Articles {article: "Article 1d"})
merge(a)-[:ARTICLE]->(d)
merge (f:Title {title: "Breaking News"})
merge (b)-[:TITLE]->(f)
merge (g:Title {title: "Breaking News2"})
merge (c)-[:TITLE]->(g)
merge (h:Title {title: "Breaking News3"})
merge (d)-[:TITLE]->(h)

Result:

Thank you all for offering to help.. I was able to figure it out eventually.. I misunderstood the documentation on the call subquery.. that was the key, and all I had to do was rewrite my query so the subquery just returned a single record so I could get the title of any one of the 'group'..