You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Neo4j Browser中将查询到的路径保存为新图?

How to Save Matched Paths as a New Subgraph in Neo4j

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

  1. Create a new database in your Neo4j instance:
    CALL db.createDatabase('water_subgraph_db')
    
  2. Switch to the new database, then run the import command:
    CALL apoc.import.graphml("water_subgraph.graphml", {})
    
    Or if you exported a Cypher script, just run the contents of water_subgraph.cypher in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:20:37