0

Is there a way to query nodes in Neo4j using Cypher in a way the result is a new graph?

I mean, something like this (does not work):

MATCH (n1:NODE_TYPE)<-[:INTERACTION_NODE]-
      (int:INTERACTION)
      -[:INTERACTION_NODE]->(n2:NODE_TYPE)
WHERE n1 <> n2
RETURN (n1)<-->(n2)

It is more or less like return the path between n1 and n2, but ignoring the int node.

Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
Fernando Ferreira
  • 798
  • 1
  • 11
  • 26

1 Answers1

1

You can't return data from a query that isn't in the graph. What you can do is MATCH and then CREATE the new graph that you want, like this:

MATCH (n1:NODE_TYPE)<-[:INTERACTION_NODE]-
      (int:INTERACTION)
      -[:INTERACTION_NODE]->(n2:NODE_TYPE)
WHERE n1 <> n2
CREATE (n1)-[r:something]->(n2)
RETURN n1, r, n2

Note this has the side-effect that not only was the data returned, it was created and written into your database.

But on the other hand, the data coming back from a RETURN statement is always going to be tabular when it's printed out by the shell. If you want to visualize the results of a RETURN as a graph, then you should use the web interface. If you use it, then the RETURN statement above in my example will actually return a picture of a graph.

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • And do you think is it possible to create this new graph only in memory? Maybe using other language like gremlin. – Fernando Ferreira Aug 12 '14 at 14:06
  • The language you're using doesn't have anything to do with it in this instance. As for only in memory...why? You should revise your question to specify why you want to do this, and what you're hoping to accomplish. It will help us give better answers. As for in-memory neo4j databases, you can do that if you use something like ramfs: http://stackoverflow.com/questions/13325084/running-neo4j-purely-in-memory-without-any-persistence – FrobberOfBits Aug 12 '14 at 14:08
  • 1
    sorry I wasn't clear on my comment. I have a neo4j database that ia not in memory. I would not like to mess with graph model people are using on it. So, instead of writing through the CREATE statement to this DB I was wondering if it is possible to temporarily create this new graph in memory or maybe un totally different instance. I have cited gremlin in attempt to say it does not need to be a cypher only solution. Is it more clear now? – Fernando Ferreira Aug 12 '14 at 14:16