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

Neo4j技术问询:生成基于Feature节点与parent_of关系的层级查询Cypher

Answer

To retrieve the full hierarchy of features connected to your input feature (including all ancestors, descendants, and their relationships until leaf nodes), you can use this Cypher query. We'll assume your Feature nodes have a property named feature_id (adjust this if your actual property name differs):

// Define the input feature ID (replace with your actual value or use a parameter)
MATCH (input:Feature {feature_id: 'INPUT_FEATURE_ID'})

// Match all ancestor features (parent, grandparent, etc., up to the root)
MATCH ancestor_path = (input)<-[:parent_of*0..]-(ancestor:Feature)

// Match all descendant features (children, grandchildren, etc., down to leaves)
MATCH descendant_path = (input)-[:parent_of*0..]-(descendant:Feature)

// Combine and deduplicate all related features
WITH COLLECT(DISTINCT ancestor) + COLLECT(DISTINCT descendant) AS all_features

// For each feature, get its parent(s) and children
UNWIND all_features AS feature
OPTIONAL MATCH (parent:Feature)-[:parent_of]->(feature)
OPTIONAL MATCH (feature)-[:parent_of]->(child:Feature)

// Return structured results for each feature
RETURN
  feature.feature_id AS feature_id,
  COALESCE(COLLECT(DISTINCT parent.feature_id), []) AS parent_ids,
  COALESCE(COLLECT(child.feature_id), []) AS child_ids,
  // Check if it's a leaf node (no outgoing parent_of relationships)
  NOT EXISTS((feature)-[:parent_of]->()) AS is_leaf,
  // Check if it's a root node (no incoming parent_of relationships)
  NOT EXISTS(()-[:parent_of]->(feature)) AS is_root
ORDER BY feature_id;

Key Explanations:

  • *0.. in path matches: This includes the input feature itself (0 steps) plus all levels of ancestors/descendants (unlimited depth).
  • COLLECT(DISTINCT ...): Ensures no duplicate feature entries even if a feature appears in multiple paths.
  • OPTIONAL MATCH: Handles root nodes (no parent) and leaf nodes (no children) without excluding them from results.
  • is_leaf flag: Marks nodes with no child features (the end of the downward hierarchy).
  • is_root flag: Marks nodes with no parent features (the top of the upward hierarchy).

If You Want a Nested Hierarchy (Using APOC):

If you have the APOC library installed and prefer a nested tree structure (e.g., root → parent → input → children → leaves), use this query:

MATCH (input:Feature {feature_id: 'INPUT_FEATURE_ID'})

// Find the root ancestor of the input feature
MATCH root_path = (root:Feature)-[:parent_of*]->(input)
WHERE NOT EXISTS(()-[:parent_of]->(root))

// Use APOC to build the nested tree from root down to leaves
CALL apoc.path.subgraphTree(root, {relationshipFilter: 'parent_of>', labelFilter: 'Feature'})
YIELD node, path
RETURN apoc.convert.toTree(node) AS feature_hierarchy;

This returns a nested JSON-like structure showing the full tree starting from the root down to all leaf nodes, including your input feature in the hierarchy.

Notes:

  • Replace 'INPUT_FEATURE_ID' with your actual feature number, or use a parameter (e.g., $feature_id) for reusability in applications.
  • If your relationship direction is reversed (e.g., child_of from child to parent), adjust the arrow directions in the MATCH clauses accordingly.

内容的提问来源于stack exchange,提问作者Rajat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:37:51