Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the concatenated string of properties via single Cypher call

Tags:

neo4j

cypher

I have the next graph:

Theme Contains multiple Stories. Theme and Story are graph nodes, connected via Contains relation.

Each node has property name.

I would like to write a Cypher query so it returns the concatenated string from names.

E.g. Theme.name = "theme name", Story1.name = "s1 name", Story2.name = "s2 name". Query result expected: "theme name s1 name s2 name".

How that can be done?

like image 625
vvinton Avatar asked Mar 11 '17 15:03

vvinton


People also ask

What Cypher statement returns the keys used for properties on nodes and relationships in the database?

When you want to return all nodes, relationships and paths found in a query, you can use the * symbol. This returns the two nodes, the relationship and the path used in the query.

What does call clause is used to perform?

The CALL clause is used to call a procedure deployed in the database.

WHAT IS WITH clause in Cypher?

The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next. It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.

What is a clause in Neo4j?

Administration clauses These comprise clauses used to manage databases, schema and security; further details can found in Database management and Access control. Clause. Description. CREATE | DROP | START | STOP DATABASE. Create, drop, start or stop a database.


1 Answers

You have a couple of choices, using either REDUCE or apoc.text.join (from the apoc procedures in 3.x)

something like this -

MATCH (t:Theme)-[:Contains]->(s:Story)
RETURN REDUCE(result=t.name, s in collect(s.name) | result+" "+s)

Or using APOC

MATCH (t:Theme)-[:Contains]->(s:Story)
RETURN apoc.text.join(t.name+collect(s.name)," ")
like image 144
mfkilgore Avatar answered Oct 03 '22 02:10

mfkilgore