Neo4J Flashcards
CREATE (n:Person {name: ‘Alice’})
Creates a node with label “Person”, called n during the operation and assigns the property “name” with string value ‘Alice’.
MATCH (a:Person {name: ‘Alice’}), (b:Person {name: ‘Bob’})
CREATE (a)-[:KNOWS]->(b)
Finds nodes with the label “Person” where the name is ‘Alice’ and ‘Bob’, and creates a “KNOWS” relationship from ‘Alice’ to ‘Bob’.
MATCH (n:Person)
RETURN n
Matches all nodes with the label “Person” and returns them.
MATCH (a:Person)-[:KNOWS]->(b:Person)
RETURN a, b
Finds all pairs of “Person” nodes where one knows the other and returns both persons.
MATCH (n:Person {name: ‘Alice’})
SET n.age = 31
Matches the “Person” node where the name is ‘Alice’ and sets the property “age” to 31.
MATCH (n:Person {name: ‘Alice’})
DELETE n
Deletes the “Person” node where the name is ‘Alice’. Note: This will fail if the node has any relationships.
MATCH (n:Person {name: ‘Alice’})-[r]-()
DELETE r, n
Deletes the “Person” node where the name is ‘Alice’ along with all its relationships.
MATCH (a:Person {name: ‘Alice’})-[:KNOWS*]->(b)
RETURN b
Finds all nodes that ‘Alice’ knows directly or indirectly and returns those nodes.
MATCH (a:Person {name: ‘Alice’}), (b:Person {name: ‘Bob’})
MATCH p = shortestPath((a)-[*]-(b))
RETURN p
Finds the shortest path between ‘Alice’ and ‘Bob’ and returns the path.
CALL algo.pageRank(‘Person’, ‘KNOWS’)
YIELD node, score
RETURN node, score
ORDER BY score DESC
Runs the PageRank algorithm on the “Person” nodes connected by “KNOWS” relationships, yielding each node with its PageRank score, and returns them ordered by score in descending order.
CALL algo.louvain(‘Person’, ‘KNOWS’)
YIELD nodeId, community
RETURN algo.asNode(nodeId).name AS name, community
ORDER BY community
Runs the Louvain community detection algorithm on the “Person” nodes connected by “KNOWS” relationships, yielding the node IDs and their respective communities, and returns the names and communities ordered by community.
CREATE FULLTEXT INDEX person_name_fulltext FOR (n:Person) ON EACH [n.name];
This command creates a full-text index named person_name_fulltext on the name property of nodes labeled Person, allowing efficient text-based searches in Neo4j
CALL db.index.fulltext.queryNodes(“PersonIndex”, “Alice”)
YIELD node
RETURN node
Performs a full-text search query for “Alice” on the “PersonIndex” and returns the matching nodes.
MATCH (n:Person)
RETURN count(n) AS totalPeople, avg(n.age) AS averageAge
Counts the total number of “Person” nodes and calculates the average age of the “Person” nodes.
LOAD CSV WITH HEADERS FROM ‘file:///persons.csv’ AS row
CREATE (p:Person {name: row.name, age: toInteger(row.age)})
Loads data from a CSV file named ‘persons.csv’, and for each row, creates a “Person” node with the name and age properties from the CSV.