Search code examples
neo4jrelationships

Creating a relationship with an atribute - Neo4j


I'm learning about neo4j and I have the following question.

I have two groups of nodes, the first one is called Workers who have an ID and the name of the worker.

On the other hand there is another group of nodes, called products, which apart from the id, has the following attributes; price, name.

I want to make a relationship called "manipulate" where I relate a worker to the product that he is going to manipulate.

For this I have a trabajaensector.csv file which relates the workers by id, along with the products they are going to manipulate, also by id.

This is its form:

id1,id2,sector
1,1,fruteria
2,2,fruteria
3,2,fruteria
4,7,panaderia
5,5,fruteria
6,5,fruteria
7,9,bebidas
8,9,bebidas
9,10,bebidas
10,10,bebidas
11,3,pescaderia
12,8,panaderia
13,7,panaderia
14,9,bebidas
15,10,bebidas
16,4,pescaderia
17,2,fruteria
18,4,pescaderia

In summary, id1 (worker) manipulates id2 (product) and its sector is "fruteria/pescaderia/panaderia o bebida"

This is my CQL for creating manipulate relationship:

LOAD CSV WITH HEADERS FROM "file:///trabajaensector.csv" AS csvLine
MATCH(w:Worker),(p:Product) where w.id= toInt(csvLine.id1) and p.id=
toInt(csvLine.id2) create (w)-[sect:trabajasec]->(p) return  sect

Here is my problem, the relationship is apparently creating well, however I am losing that third "sector" data, which indicates the sector where the worker works by manipulating that product.

For example, the relationship for a worker named Juan who manipulates apples should have in the relation the variable / attribute "fruteria" or for fish "pescaderia".

Any idea of how to properly include that data in the relationship and how to recover it?


Solution

  • You can add a sector property to the trabajasec relationships:

    LOAD CSV WITH HEADERS FROM "file:///trabajaensector.csv" AS csvLine
    MATCH (w:Worker), (p:Product)
    WHERE w.id = TOINT(csvLine.id1) AND p.id = TOINT(csvLine.id2)
    CREATE (w)-[sect:trabajasec {sector: csvLine.sector}]->(p)
    RETURN sect;
    

    To use the above query, you should first delete the trabajasec relationships created by your earlier LOAD CSV query.