Skip to content

NEO4J

Multimodal Graph Database with embeadded RAG

The BFS-generated nodes and their relationships were stored in Neo4j, which was then used to retrieve relevant nodes efficiently using indexed searches. This setup allows quick access to node information and connections for further processing or recommendation tasks.

Code Snippet

driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
driver.execute_query("MATCH (n) DETACH DELETE n")
for idx, row in df.iterrows():
    driver.execute_query(
        """
        MERGE (p:Product {productId: $pid, name: $name})
        MERGE (c:Category {name: $category})
        MERGE (pr:Price {value: $price})
        MERGE (img:Image {path: $imagePath})

        MERGE (p)-[:BELONGS_TO]->(c)
        MERGE (p)-[:HAS_PRICE]->(pr)
        MERGE (p)-[:HAS_IMAGE]->(img)
        """,
        pid=int(row['product_id']),
        name=row['product_name'],
        category=row['category'],
        price=float(row['price']),
        imagePath=row['image_path']
    )



def fetch_prod(product_ids):
    results = []
    for pid in product_ids:
        record = driver.execute_query(
            """
            MATCH (p:Product {productId: $pid})
            OPTIONAL MATCH (p)-[:BELONGS_TO]->(c:Category)
            OPTIONAL MATCH (p)-[:HAS_PRICE]->(pr:Price)
            OPTIONAL MATCH (p)-[:HAS_IMAGE]->(img:Image)
            RETURN p.name AS name, p.productId AS id,
                   c.name AS category, pr.value AS price,
                   img.path AS imagePath
            """,
            pid=int(pid)
        )
        if record and record.records:
            results.append(record.records[0].data())
    return results

#for bfs traversal
def getNodes(product_info):
    results = []
    for pid in product_info:
        record = driver.execute_query(
            """
            MATCH (startNode:Product {name: $nm})-[*1..3]-(endNode)   
            RETURN DISTINCT endNode
            """,
            nm=pid['name']
        )
        if record and record.records:
            results.append(record.records[0].data())
    return results