graph query
Queries in Knowledge Graph use Cypher language, which is expressive and efficient, and its role in graph databases is equivalent to SQL in relational databases.
Common Basic Syntax:
- MATCH is used to query nodes, relationships, and properties, similar to SELECT in SQL.
- RETURN returns the search result after MATCH search is completed, so MATCH must be used together with RETURN.
- The WHERE clause is used to filter retrievals.
- WHERE can be combined with functions like exists(), string matching starts with, ends with, contains, logical matching in, not, and, or, and regular expression matching for more detailed queries.
- The LIMIT clause is used to limit the number of returned matching 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 Attributes | match (n:entity type{attribute name:'attribute value'}) return n | match (n:River{name:'Dadube River'}) return n |
| Find Specific Types of Relationships | match p = (n)-[r]->(m) return p | match p = (n)-[r:Contains]->(m) return p |
In graph data, we often need to perform deeper relationship queries based on relationships. The following examples show several commonly used relationship query statements:
| Serial Number | Cypher Statement | Meaning |
| 1 | MATCH p=(n:River{name:'Dadube River'})-[r]-() RETURN p | Query all relations of Dadube River (without arrow direction). |
| 2 | MATCH p=(n:River{name:'Dadube River'})-[r:Flows Through]->() RETURN p | Query the flows through relationship of Dadube River (with arrow direction). |
| 3 | MATCH p=(n)-[r:Flows Through]->(m:Region{name:'Ya'an'}) RETURN p or MATCH p=(n)-[r:Flows Through]->(m:Region) where m.name='Ya'an' return n.name | Query all rivers that flow through the Ya'an region. |
| 4 | MATCH p=(n)-[r:Flows Through]->(m) where m.name in ['Luding County', 'Danba County'] return n.name | Query rivers that flow through both Luding County and Danba County. |
| 5 | MATCH p=(n:Hydropower Station{name:'Pubugou Hydropower Station'})-[:Located On River]->(m)-[:Flows Through]->(e) RETURN e | Find which regions the river where Pubugou Hydropower Station is located flows through. |
Additionally, when spatial entities are stored in the YuKon database, spatial queries on entities are supported at the database side.
Common Spatial Functions:
| Spatial Function | Meaning |
| st_Equals | Equal |
| st_Intersects | Intersect |
| st_Contains | Contain |
| st_Crosses | Cross |
| st_Within | Within |
| st_Touches | Touch |
| st_Overlaps | Overlap |
| st_Disjoint | Disjoint |
| st_Distance | Calculate Distance |
Query Statement Examples:
| Serial Number | Cypher Statement | Meaning |
| 1 | match (n:country), (m:province{name:'Sichuan Province'}) where st_Contains(m.geom::geometry, n.geom::geometry) return n."name"; | Query county entities within Sichuan Province (spatial geometry is of type geometry). |
| 2 | match (n:country{name:'Shuangliu County'}), (m:country{name:'Chaoyang District'}) return st_Distance(n.geom, m.geom); | Return the distance between Shuangliu County and Chaoyang District (spatial geometry is of type geography). |
Notes:When performing graph queries in YuKon with AgensGraph, entity property fields must be in lowercase.