Graph Query

Knowledge graph queries use Cypher language, which is expressive and efficient, playing a role in graph databases equivalent to SQL in relational databases.

Basic syntax:

  • MATCH is used to query nodes, relationships and properties, similar to SELECT in SQL.
  • RETURN outputs search results after MATCH query, therefore MATCH must be used with RETURN.
  • WHERE clause filters query results
  • WHERE can combine functions exists(), string matching starts with/ends with/contains, logical operators in/not/and/or, and regular expressions for precise queries
  • LIMIT clause restricts the number of returned results
Query Command Cypher Syntax Example
Find specified entity type match (n:entity type) return n match(n:River) return n
Find entities with specific properties match (n:entity type{property:'value'}) return n match (n:River{name:'Daduo River'}) return n
Find specific relationships match p = (n)-[r]->(m) return p match p = (n)-[r:Contains]->(m) return p

In graph data, deeper relationship-based queries are often required. Common association queries:

No. Cypher Statement Description
1 MATCH p=(n:River{name:'Daduo River'})-[r]-() RETURN p Query all relations of Daduo River (no direction)
2 MATCH p=(n:River{name:'Daduo River'})-[r:FlowThrough]->() RETURN p Query flow-through relationships of Daduo River (with direction)
3 MATCH p=(n)-[r:FlowThrough]->(m:Region{name:'Yaan'}) RETURN p 或 MATCH p=(n)-[r:FlowThrough]->(m:Region)where m.name='Yaan' return n.name Find all rivers flowing through Yaan region
4 MATCH p=(n)-[r:FlowThrough]->(m)where m.name in ['Luding County','Danba County'] return n.name Find rivers flowing through both Luding and Danba counties
4 MATCH p=(n:PowerStation{name:'Pubugou Station'})-[:LocatedIn]->(m)-[:FlowThrough]->(e) RETURN e Find regions through which the river containing Pubugou Power Station flows

When spatial entities are stored in YuKon database, server-side spatial query is supported.

Common spatial functions:

Function Description
st_Equals Equal
st_Intersects Intersect
st_Contains Contain
st_Crosses Cross
st_Within Within
st_Touches Adjacent
st_Overlaps Overlap
st_Disjoint Disjoint
st_Distance Calculate distance

Query examples:

No. Cypher Statement Description
1 match (n:County), (m:Province{name:'Sichuan'}) where st_Contains(m.geom::geometry, n.geom::geometry) return n."name"; Find county entities within Sichuan Province (geometry type)
2 match (n:County{name:'Shuangliu'}), (m:County{name:'Chaoyang'}) return st_Distance(n.geom, m.geom); Return distance between Shuangliu and Chaoyang (geography type)
Caution:

When performing graph queries in YuKon with AgensGraph, entity property fields must be in lowercase.