Search code examples
neo4jcypherneo4j-apoc

Retrieve the nodes from a map returned by apoc.cypher.run


I'm using neo4j 3.5, I need to make a subquery and I'm using "apoc.cypher.run".

CALL apoc.cypher.run("MATCH (a:OBJECT)-[HAS_RELATIONSHIP]->(b:THING)
WHERE toLower(b.name) CONTAINS 'something'
RETURN a    
UNION
MATCH (a:Object)-[HAS_RELATIONSHIP]->(b:THING)--(c: Category)
WHERE toLower(c.name) CONTAINS 'something'
RETURN a", {})
yield value as a
WITH a
MATCH (a:Object)-[HAS_RELATIONSHIP]->(b:Thing)
OPTIONAL MATCH (b)--(c: Category) 
return a, b, c 

It breaks on the line :

MATCH (a:Object)-[HAS_RELATIONSHIP]->(b:Thing)

with an error "a defined with conflicting type Map (expected Node)", what is wrong with this query ?


Solution

  • CALL apoc.cypher.run ()... yield value as a returns a map and in your case maps of one map(nodes) each and this is expected.
    The returned map would be structured as {a:{node Properties}}.
    This has to be broken down further to get the nodes that are mapped inside.

    yield value as a
    WITH a.a as a
    

    To retrieve the node from each map:

    CALL apoc.cypher.run("MATCH (a:OBJECT)-[HAS_RELATIONSHIP]->(b:THING)
    WHERE toLower(b.name) CONTAINS 'something'
    RETURN a    
    UNION
    MATCH (a:Object)-[HAS_RELATIONSHIP]->(b:THING)--(c: Category)
    WHERE toLower(c.name) CONTAINS 'something'
    RETURN a", {})
    yield value as a
    WITH a.a as a
    MATCH (a:Object)-[HAS_RELATIONSHIP]->(b:Thing)
    OPTIONAL MATCH (b)--(c: Category) 
    return a, b, c