Skip to content

Cypher query #

Find similar titles
  • 최초 작성자
    Kevin

Structured data

Category
Database

Cypher query #

Cypher query는 그래프 데이터베이스의 질의어이며, 다른 질의어에 비해 단순하지만 매우 강력한 질의어이다. Cypher query는 처음에는 그래프 데이터베이스 Neo4j사가 만들었으나 2015년 10월 오픈 사이퍼 프로젝트를 통해 개방되었다. Cypher query는 프로퍼티 그래프 모델에 기반을 두며, 노드와 엣지의 표준 그래프 요소들에 레이블(label)과 프로퍼티(property)를 추가해서 데이터베이스화 할 수 있다.

Cypher query 문법 #

일반적으로 많이는 쓰이는 문법은 Match와 Where이다. Match는 검색할 패턴의 구조를 기술할때 사용되며, 노드타입에 따라 그리고 엣지타입에 따라 패턴을 기술할 수 있으며, 프로퍼티값을 지정하여 검색이 가능하다.

$ Match (n:Food{n.name:"강황"})-[r]-(n1:Metabolite)-[r1]-(n2:Disease)

Where의 경우에는 match할 패턴에서 조건식을 기술할때 사용된다. 따라서 노드와 엣지에 부등호 기호들을 활용하여 다양한 검색조건들을 기술할 수 있다.

$ Match (n:Food{n.name:"강황"})-[r]-(n1:Metabolite)-[r1]-(n2:Disease)
where n.group = "곡류"
return n, n1, n2

return할때 node 중복성 제외하기 위해 distinct와 node의 제한을 두기위해 limit를 많이 활용한다.

$ Match (n:Food{n.name:"강황"})-[r]-(n1:Metabolite)-[r1]-(n2:Disease)
where n.group = "곡류"
return distinct n, n1, n2 limit 100

그외에 with와 union을 많이 활용하면 멀티패턴의 검색이 가능하다. with는 중복되는 패턴과 중복되지 않는 패턴이 공존할때 주로 많이 활용되며, union은 멀티패턴을 모두 검색하여 볼 수 있다.

$ Match (n:Food{n.name:"강황"})-[r]-(n1:Metabolite)-[r1]-(n2:Disease)
where n.group = "곡류"
with n, n1, n2
Match (n3:Food{n.name:"포도"})-[r]-(n1:Metabolite)-[r1]-(n2:Disease)
return distinct n, n1, n2 limit 100

본문에서 사용한 문법이외에도 다양한 문법이 존재하며, 더 자세한 내용이 궁금하면 Neo4j cypher Refcard(https://neo4j.com/docs/cypher-refcard/current)에서 다양한 쿼리문법을 확인할 수 있다.

Reference #

0.0.1_20231010_1_v71