RAG With Knowledge Graph (Neo4j) - Guide On Nosql Database
RAG With Knowledge Graph (Neo4j) - Guide On Nosql Database
data model. Here are the primary types along with examples for
each:
Document Stores:
Example: MongoDB
{
"_id": "12345",
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
},
"hobbies": ["reading", "hiking", "coding"]
}
Key-Value Stores:
Description: Store data as key-value pairs, where the key is a unique identifier and
the value is the data associated with the key. These are highly performant for
simple read and write operations.
Example: Redis
Column-Family Stores:
Description: Store data in columns and rows, similar to relational databases but
optimized for reading and writing large volumes of data. Each column family can
have a different schema.
Example Comparison
Relational Database (MySQL)
Schema Definition:
Query Example:
Query Example:
Description: Store data in graph structures, with nodes representing entities and
edges representing relationships between entities. These databases are designed
to handle complex and interconnected data.
Example: Neo4j
Creating Nodes
Creating Nodes
CREATE (alice:User {name: 'Alice', age: 30}),
(bob:User {name: 'Bob', age: 25}),
(carol:User {name: 'Carol', age: 27}),
(post1:Post {content: 'Graph databases are
cool!', timestamp: '2024-05-20'}),
(post2:Post {content: 'Learning Cypher is
fun!', timestamp: '2024-05-19'})
Creating Relationships
// Creating friendships
CREATE (alice)-[:FRIEND]->(bob),
(bob)-[:FRIEND]->(carol),
(carol)-[:FRIEND]->(alice)
// Creating likes
CREATE (bob)-[:LIKES]->(post1),
(carol)-[:LIKES]->(post1),
(alice)-[:LIKES]->(post2)
Creating Relationships
// Creating friendships
CREATE (alice)-[:FRIEND]->(bob),
(bob)-[:FRIEND]->(carol),
(carol)-[:FRIEND]->(alice)
// Creating likes
CREATE (bob)-[:LIKES]->(post1),
(carol)-[:LIKES]->(post1),
(alice)-[:LIKES]->(post2)
Explanation
● Nodes and Properties: Nodes User and Post have properties such as name,
age, content, and timestamp.
● Queries: Cypher queries are used to traverse the graph and retrieve data
based on the relationships and properties defined.
This example demonstrates how Neo4j can be used to model and query a social
network, leveraging the power of graph databases to efficiently manage and
explore relationships within the data.