5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Neo4j: Cypher チートシート

Posted at

Neo4j による Cypher の公式リファレンス

初心者が Cypher を容易に使うためのチートシート

Node 作成

CREATE ([in_query_name]:[Label] { [anykey: anyvalue],... }), ...
RETURN [in_query_name], ...

// 実例
CREATE (a_san:Person { name: "Aさん", 所属: "開発部" }), (b_san:Person { name: "Bさん", 所属: "経理部" })
RETURN a_san, b_san

Relationship 作成

例1: 2つの Node を明示して連結

MATCH (a_san: Person), (b_san: Person)
WHERE a_san.name = "Aさん" AND b_san.name = "Bさん"
CREATE (a_san)-[r:知っている]->(b_san)
RETURN r

例2: 外部キーを使って連結

MATCH (p:Product),(c:Category)
WHERE p.categoryID = c.categoryID
CREATE (p)-[:PART_OF]->(c)

例3: 連結テーブルを使って連結 ( HasAndBelongsToManyケース )

LOAD CSV WITH HEADERS FROM "http://data.neo4j.com/northwind/order-details.csv" AS row
MATCH (p:Product), (o:Order)
WHERE p.productID = row.productID AND o.orderID = row.orderID
CREATE (o)-[details:ORDERS]->(p)
SET details = row,
    details.quantity = toInteger(row.quantity)

Node の Index

例:Product Labelの categoryID Propertyにインデックスを貼る場合

CREATE INDEX ON :Product(categoryID)

Index を確認する

CALL db.indexes

Fulltext Index(全文検索インデックス)

Fulltext Index を付ける

CALL db.index.fulltext.createNodeIndex("nameAndDescription",["Thing","Person","Product"],["name", "description"])

Fulltext Index を確認する

CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "matrix") YIELD node, score
RETURN node.title, node.description, score

Fulltext Index を削除する

CALL db.index.fulltext.drop('nameAndDescription')

※ 実行しても(no changes, no records) とか出るので一見成功しているかわかりにくい

Node の選択

MATCH (a_san: Person), ...
WHERE a_san.name = "Aさん" [AND ...]
RETURN a_san, ...

// ex
MATCH (a_san: Person), (b_san: Person)
WHERE a_san.name = "Aさん" AND b_san.name = "Bさん"
RETURN a_san, b_san

datetimeの比較

WHERE n.start > datetime("2010-01-01T00:00:00+0900")

全ての Node, Relationsを削除

MATCH (n) DETACH DELETE n

※ 注意:Nodeに Relationshipがある場合はNodeの削除はできないので、 DETACH が必要
※ データが多くて削除が遅い場合は、全てのデータを削除する の記事参照

CSVのImport

LOAD CSV WITH HEADERS FROM "http://data.neo4j.com/northwind/products.csv" AS row
CREATE (n:Product)
SET n = row,
  n.unitPrice = toFloat(row.unitPrice),
  n.unitsInStock = toInteger(row.unitsInStock),
  n.unitsOnOrder = toInteger(row.unitsOnOrder),
  n.reorderLevel = toInteger(row.reorderLevel),
  n.discontinued = (row.discontinued <> "0")

Propertiesの明示方法

Float 型にする

n.unitPrice = toFloat(row.unitPrice),

Integer 型にする

n.unitsInStock = toInteger(row.unitsInStock),

Boolean 型にする

n.discontinued = (row.discontinued <> "0")
or
n.discontinued = toBoolean(row.discontinued)

datetime 型にする

n.start = datetime("2010-01-01T00:00:00Z")

timestamp 型にする

n.startedtime = timestamp(row.startedtime)
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?