如何在Neo4j Browser中将查询到的路径保存为新图?
Great question! Unfortunately, Neo4j doesn’t have a built-in MERGE path command that automatically replicates your matched paths into a new subgraph, but there are a few simple, practical ways to get this done—depending on whether you want to keep the subgraph in the same database or export it to a completely new one.
Option 1: Mark the Subgraph in the Same Database
If you just want to flag the nodes and relationships from your matched paths so you can easily reference them later (instead of creating duplicates), add a custom label or attribute to identify them:
MATCH path = (n:A {title:"WATER"})-[r*2]-(m:B) // Add a label to all nodes in the path FOREACH (node IN nodes(path) | SET node:WaterSubgraph) // Add a property to all relationships in the path FOREACH (rel IN relationships(path) | SET rel.part_of_water_subgraph = true)
Now you can quickly query this subgraph anytime with:
MATCH path = (n:WaterSubgraph)-[r:part_of_water_subgraph*]-(m:WaterSubgraph) RETURN path
Option 2: Export to a New Database (As a Standalone Graph)
If you need a separate, independent copy of the subgraph, use Neo4j’s APOC library (you’ll need to have it installed and enabled first) to export the paths, then import them into a new database.
Step 1: Export the Matched Paths
Export the subgraph to a GraphML file (easy to import later):
MATCH path = (n:A {title:"WATER"})-[r*2]-(m:B) CALL apoc.export.graphml.data(nodes(path), relationships(path), "water_subgraph.graphml", {}) YIELD file, nodes, relationships RETURN file, nodes, relationships
You can also export to a Cypher script instead, which lets you run direct create statements:
MATCH path = (n:A {title:"WATER"})-[r*2]-(m:B) CALL apoc.export.cypher.data(nodes(path), relationships(path), "water_subgraph.cypher", {format: "cypher-shell"}) YIELD file, nodes, relationships RETURN file, nodes, relationships
Step 2: Import into a New Database
- Create a new database in your Neo4j instance:
CALL db.createDatabase('water_subgraph_db') - Switch to the new database, then run the import command:
Or if you exported a Cypher script, just run the contents ofCALL apoc.import.graphml("water_subgraph.graphml", {})water_subgraph.cypherin the new database.
Quick Alternative: Duplicate Nodes/Relationships in the Same Database
If you want to create exact duplicates of the nodes and relationships (not just mark them), use this query to copy all properties and labels/relationship types:
MATCH path = (n:A {title:"WATER"})-[r*2]-(m:B) WITH nodes(path) AS originalNodes, relationships(path) AS originalRels // Create duplicate nodes and store their original ID -> new node mapping UNWIND originalNodes AS origNode CREATE (newNode) SET newNode = origNode, newNode:DuplicateSubgraph WITH collect({origId: id(origNode), newNode: newNode}) AS nodeMap, originalRels AS origRels // Create duplicate relationships using the node mapping UNWIND origRels AS origRel MATCH (startNew) WHERE startNew = [nm IN nodeMap WHERE nm.origId = id(startNode(origRel))][0].newNode MATCH (endNew) WHERE endNew = [nm IN nodeMap WHERE nm.origId = id(endNode(origRel))][0].newNode CREATE (startNew)-[newRel:type(origRel)]->(endNew) SET newRel = origRel RETURN count(newRel) AS duplicated_relationships
This will create a separate copy of your subgraph with the :DuplicateSubgraph label for easy identification.
内容的提问来源于stack exchange,提问作者Asya

