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

如何用ApacheAGE在单条查询中返回不同图的顶点?

如何在Apache AGE中单条查询获取多个图的所有顶点?

已创建family_tree和taxonomy_biology两个图,想要一次性获取这两个图的所有顶点,执行了以下查询:

SELECT * FROM cypher ('family_tree' AND 'taxonomy_biology', $$
MATCH (v)
RETURN v
$$) as (vertex agtype);

终端返回错误:

ERROR:  invalid input syntax for type boolean: "family_tree"
LINE 1: SELECT * FROM cypher('family_tree' AND 'taxonomy_biology', $...

单独查询每个图时可以正常返回顶点:
查询taxonomy_biology:

SELECT * FROM cypher('taxonomy_biology', $$
MATCH (v)
RETURN v
$$) as (VERTEX agtype);

结果:

vertex
-----------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Kingdom", "properties": {"name": "Animalia"}}::vertex
 {"id": 1125899906842625, "label": "Phylum", "properties": {"name": "Cnidaria"}}::vertex
(2 rows)

查询family_tree:

SELECT * FROM cypher('family_tree', $$                                                                                                                     
MATCH (v)
RETURN v
$$) as (VERTEX agtype);

结果:

vertex

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Person", "properties": {"name": "Louis", "titles": ["Emperor of the Carolingian Empire", "King of the Franks", "King of Aquitaine"], "year_born": 778, "year_died": 840}}::vertex
 {"id": 844424930131970, "label": "Person", "properties": {"name": "Hildegard", "titles": ["Frankish queen consort"], "year_born": 754, "year_died": 783}}::vertex
 {"id": 844424930131971, "label": "Person", "properties": {"name": "Charlemagne", "titles": ["Holy Roman Emperor", "King of the Franks", "King of the Lombards"], "year_born": 747, "year_died": 814}}::vertex
(3 rows)

解决方案

Apache AGE的cypher()函数第一个参数仅支持传入单个图名,无法通过AND直接指定多个图。要在单条查询中获取多个图的所有顶点,需要使用UNION ALL将两个单独的查询结果合并:

SELECT * FROM cypher('taxonomy_biology', $$
MATCH (v)
RETURN v
$$) as (vertex agtype)
UNION ALL
SELECT * FROM cypher('family_tree', $$
MATCH (v)
RETURN v
$$) as (vertex agtype);
  • 使用UNION ALL会保留所有结果(包括可能存在的ID重复的顶点);如果需要去重(比如两个图中存在完全相同的顶点),可以替换为UNION,但UNION会对结果集去重,性能略低。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 09:50:16