Neo4J Flashcards

1
Q

CREATE (n:Person {name: ‘Alice’})

A

Creates a node with label “Person”, called n during the operation and assigns the property “name” with string value ‘Alice’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

MATCH (a:Person {name: ‘Alice’}), (b:Person {name: ‘Bob’})
CREATE (a)-[:KNOWS]->(b)

A

Finds nodes with the label “Person” where the name is ‘Alice’ and ‘Bob’, and creates a “KNOWS” relationship from ‘Alice’ to ‘Bob’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

MATCH (n:Person)
RETURN n

A

Matches all nodes with the label “Person” and returns them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

MATCH (a:Person)-[:KNOWS]->(b:Person)
RETURN a, b

A

Finds all pairs of “Person” nodes where one knows the other and returns both persons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

MATCH (n:Person {name: ‘Alice’})
SET n.age = 31

A

Matches the “Person” node where the name is ‘Alice’ and sets the property “age” to 31.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

MATCH (n:Person {name: ‘Alice’})
DELETE n

A

Deletes the “Person” node where the name is ‘Alice’. Note: This will fail if the node has any relationships.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

MATCH (n:Person {name: ‘Alice’})-[r]-()
DELETE r, n

A

Deletes the “Person” node where the name is ‘Alice’ along with all its relationships.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

MATCH (a:Person {name: ‘Alice’})-[:KNOWS*]->(b)
RETURN b

A

Finds all nodes that ‘Alice’ knows directly or indirectly and returns those nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

MATCH (a:Person {name: ‘Alice’}), (b:Person {name: ‘Bob’})
MATCH p = shortestPath((a)-[*]-(b))
RETURN p

A

Finds the shortest path between ‘Alice’ and ‘Bob’ and returns the path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

CALL algo.pageRank(‘Person’, ‘KNOWS’)
YIELD node, score
RETURN node, score
ORDER BY score DESC

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CALL algo.louvain(‘Person’, ‘KNOWS’)
YIELD nodeId, community
RETURN algo.asNode(nodeId).name AS name, community
ORDER BY community

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

CREATE FULLTEXT INDEX person_name_fulltext FOR (n:Person) ON EACH [n.name];

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

CALL db.index.fulltext.queryNodes(“PersonIndex”, “Alice”)
YIELD node
RETURN node

A

Performs a full-text search query for “Alice” on the “PersonIndex” and returns the matching nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

MATCH (n:Person)
RETURN count(n) AS totalPeople, avg(n.age) AS averageAge

A

Counts the total number of “Person” nodes and calculates the average age of the “Person” nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

LOAD CSV WITH HEADERS FROM ‘file:///persons.csv’ AS row
CREATE (p:Person {name: row.name, age: toInteger(row.age)})

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

CALL apoc.export.csv.query(“MATCH (n:Person) RETURN n”, “persons_export.csv”, {})

A

Exports the result of the query matching all “Person” nodes to a CSV file named ‘persons_export.csv’.

17
Q

CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE

A

Creates a constraint on the “Person” nodes to ensure that the “name” property is unique.

18
Q

CREATE INDEX city_name_index FOR (c:City) ON (c.name);

A

Creates an index on the “name” property of the “Person” nodes to improve query performance.

19
Q

CALL apoc.help(“text”)

A

Calls the APOC (A Package Of Components) help procedure to get information about available APOC functions related to “text”.

20
Q

@Procedure(value = “example.customProcedure”)
public Stream<Node> customProcedure() {
// Implementation
}</Node>

A

Defines a user-defined procedure in Java named “example.customProcedure” that can be deployed and called within Neo4j.

21
Q

What is the Louvain community detection algorithm?

A

The Louvain community detection algorithm is a method used to identify communities in large networks by maximizing modularity, which measures the strength of division of a network into clusters. It is an iterative algorithm that groups nodes into communities and then aggregates these communities into a new network, repeating the process until the modularity cannot be increased further.

22
Q

What is the PageRank algorithm?

A

The PageRank algorithm is a ranking algorithm used to determine the importance of nodes in a graph based on the structure of incoming links. It was originally developed by Google to rank web pages in search engine results. The algorithm assigns a probability distribution to each node, representing the likelihood of visiting that node, and iteratively updates these probabilities based on the links from other nodes. Nodes with higher incoming links from important nodes are ranked higher.

23
Q

Create a node with the label Person and set the properties name to ‘Alice’ and age to 30.

A

CREATE (n:Person {name: ‘Alice’, age: 30}) RETURN n

24
Q

Create a relationship APPEARS_IN between two existing nodes with labels Actor and Movie, where the actor’s name is ‘Tom Hanks’ and the movie’s title is ‘Forrest Gump’.

A

MATCH (a:Actor {name: ‘Tom Hanks’}), (m:Movie {title: ‘Forrest Gump’})
CREATE (a)-[:APPEARS_IN]->(m)

25
Q

Find all nodes with the label Person and return their names and ages.

A

MATCH (n:Person)
RETURN n.name, n.age

26
Q

Update the property age to 35 for a node with the label Person and the name ‘Bob’.

A

MATCH (n:Person {name: ‘Bob’})
SET n.age = 35

27
Q

Delete a relationship FRIENDS_WITH between two nodes with the label Person, where one person’s name is ‘Alice’ and the other’s name is ‘Bob’.

A

MATCH (a:Person {name: ‘Alice’})-[r:FRIENDS_WITH]->(b:Person {name: ‘Bob’})
DELETE r

28
Q

Return the shortest path between two nodes with the label Person, where one person’s name is ‘Alice’ and the other’s name is ‘Bob’.

A

MATCH (a:Person {name: ‘Alice’}), (b:Person {name: ‘Bob’})
MATCH p = shortestPath((a)-[*]-(b))
RETURN p

29
Q

Create a unique constraint on the email property for nodes with the label User.

A

CREATE CONSTRAINT FOR (u:User) REQUIRE u.email IS UNIQUE

30
Q

Load data from a CSV file named ‘movies.csv’ and create nodes with the label Movie using the data in the CSV file.

A

LOAD CSV WITH HEADERS FROM ‘file:///movies.csv’ AS row
CREATE (m:Movie {title: row.title, releaseYear: toInteger(row.releaseYear)})

31
Q

Remove the age property from nodes with the label Person and the name ‘Alice’.

A

MATCH (n:Person {name: ‘Alice’})
REMOVE n.age

32
Q

Create an index on the name property for nodes with the label City.

A

CREATE INDEX city_name_index FOR (c:City) ON (c.name);