Graph Query
The query of Knowledge Graph uses cypher language, which has rich expressive force and high query efficiency. Its role in Graph Database is equivalent to that of SQL language in relational database.
Common basic syntax:
- MATCH is used to query about nodes, relationships, and attributes, similar to SELECT in SQL.
- RETURN returns the Search Result after the MATCH search is complete, so MATCH must be used with RETURN.
- The WHERE clause is used to filter the retrieval
- WHERE can be combined with the function exists (), string matching starts with, ends with, contains, logical matching in, not, and, or, and regular expression matching for more sophisticated queries
- The LIMIT clause is used to limit the number of matches returned
Query command | The cypher syntax | Examples |
Finds the specified Entity Type | match (n:Entity Type) return n | Match (n: river) return n |
Find an entity for a certain attribute | Match (n: Entity Type { attribute name: 'attribute value' }) return n | Match (n: river { name: 'Dadu River' }) return n |
Find a type of relationship | match p = (n)-r->(m) return p | Match p = (n)- R: contains-> (m) return p |
In the graph data, we often need to perform a deeper association query based on the relationship. The following are examples of several common association query statements:
Serial number | The cypher statement | Meaning |
1 | MATCH p = (n: river { name: 'Dadu River' })- R- () RETURN p | Query All relations of Dadu River (no arrow direction) |
2 | MATCH p = (n: river { name: 'Dadu River' })- R: flow through-> () RETURN p | Query the flowing relationship of Dadu River (with arrow direction) |
3 | MATCH p = (n)- R: via-> (m: region { name: 'Ya'an' }) RETURN p or MATCH p = (n)- R: via-> (m: region) where m. Name = 'Ya'an' return n. Name | Inquire about all the rivers that flow through the Hongya area. |
4 | MATCH p = (n)- R: flow through-> (m) where m. Name in 'Luding County', 'Danba County' return n. Name | Inquire about rivers flowing through both Luding and Danba counties |
4 | MATCH p = (n: hydropower station { name: 'Pubugou Hydropower Station' })- : river-> (m)- : flow through-> (e) RETURN e | Find out which areas the river where Pubugou Hydropower Station is located flows through |
In addition, when the spatial entity is stored in the YuKon database, it supports the database-side Spatial Query of the entity.
Common spatial functions:
Space function | Meaning |
st_Equals | Equal |
st_Intersects | Intersect |
st_Contains | Contain |
st_Crosses | Intersection |
st_Within | Inside |
st_Touches | Adjacent |
st_Overlaps | Overlay |
st_Disjoint | Separation |
st_Distance | Calculate the distance |
Example query statement:
Serial number | The 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 the county entity in Sichuan Province (spatial geometry is of geometry type) |
2 | Match (n: country { name: 'Shuangliu' }), (m: country { name: 'Chaoyang' }) return st _ Distance (n. Geom, m. Geom); | Return to the distance between Shuangliu County and Chaoyang District (space geometry is geography type) |
Caution:
Entity Attributes field must be lowercase when doing a Graph Query
in the YuKon with Agens Graph.