2
votes

So I'm trying to send a query that will Merge a number of nodes and I want the query to return the nodes that it created but I can't put the return statement inside the FOREACH so is there a way to collect the nodes and then return that collection at the end?

FOREACH (tagName in {tags} | 
MERGE (n:items {classid:tagName.pClassid}) 
ON CREATE 
COLLECT(n) as allCreatedNodes) 
RETURN allCreatedNodes;

"params" : {
        "tags": [{"pClassid" : 1}, {"pClassid" : 2}, {"pClassid" : 3}]
         }
2

2 Answers

2
votes

Right now that's unfortunately not possible.

The only thing that you can do (if you really need it is to look the nodes up after the fact. And unfortunately using IN with a MATCH is not optimized yet.

FOREACH (tagName in {tags} |  MERGE (n:items {classid:tagName.pClassid}))
WITH [t IN {tags} | t.pClassid ] as classIds
MATCH (allCreatedNodes:items)
WHERE allCreatedNodes.classid IN classIds
RETURN allCreatedNodes;
0
votes

One thing you could try is creating a temporary graph structure to quickly look up the nodes after foreach. It probably won't be worth it if the tag collection is small, but it's sometimes a useful strategy and until label indices handle IN collection style lookups it may be worth a shot. Basically it means maintaining some small graph structure as a transient (query-local or query-type-local) index. In this case,

  1. begin the query by creating (query-local) or merging (query-type-local) an index node
  2. relate all the merged nodes to it
  3. and when its time to return match from index node to the created nodes
  4. delete the relationships (and index node if query-local) and return.

I can't test this at the moment, but you could try something like this

CREATE (index)
FOREACH (tagName in {tags} | 
    MERGE (n:items {classid:tagName.pClassid} )
    CREATE index-[:TRANSIENT]->n
) 
WITH index
MATCH index-[t:TRANSIENT]->n
DELETE t, index
RETURN n

(This may be overkill for your type of query. If you try it, do try profiling the query in comparison with a re-fetch from label index and post back.)