Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Neo4j - Creating Data

Neo4j - Creating Data

En esta clase aprenderás:
- Crear nodos.
- Crear relaciones.
- Consultas básicas
- Consultas para realizar recomendaciones.

More Decks by Juan Guillermo Gómez Torres

Other Decks in Programming

Transcript

  1. Script Graph Model Reference MERGE (user1:USER{name:'Juan', age:35}) MERGE (user2:USER{name:'Pedro', age:20})

    MERGE (user3:USER{name:'Maria', age:40}) MERGE (movie1:MOVIE{name:"Titanic"}) MERGE (movie2:MOVIE{name:"Forrest Gump"}) MERGE (movie3:MOVIE{name:"Rey Leon"}) MERGE (movie4:MOVIE{name:"Avengers"}) MERGE (movie5:MOVIE{name:"Batman"}) MERGE (word1:WORD{text:"love"}) MERGE (word2:WORD{text:"try"}) MERGE (word3:WORD{text:"boat"}) MERGE (word4:WORD{text:"travel"}) MERGE (word5:WORD{text:"king"}) MERGE (word6:WORD{text:"lion"}) MERGE (word7:WORD{text:"jungle"}) MERGE (word8:WORD{text:"war"}) MERGE (word9:WORD{text:"planet"}) MERGE (word10:WORD{text:"bat"}) MERGE (word11:WORD{text:"gotica"}) MERGE (word12:WORD{text:"run"}) MERGE (word13:WORD{text:"shrimp"}) MERGE (medal1:MEDAL{text:"Medal 1"}) MERGE (medal2:MEDAL{text:"Medal 2"}) MERGE (medal3:MEDAL{text:"Medal 3"}) MERGE (sub1:SUB_CATEGORY{text:"Thropies Total"}) MERGE (sub2:SUB_CATEGORY{text:"Thropies Daily"}) MERGE (sub3:SUB_CATEGORY{text:"Thropies Weekly"}) MERGE (category:CATEGORY{text:"Mastery"}) MERGE (user1) - [:LIKES{rated:5}] -> (movie1) MERGE (user1) - [:LIKES{rated:4}] -> (movie2) MERGE (user1) - [:LIKES] -> (movie3) MERGE (user2) - [:LIKES{rated:4}] -> (movie1) MERGE (user2) - [:LIKES] -> (movie4) MERGE (user3) - [:LIKES{rated:5}] -> (movie1) MERGE (user3) - [:LIKES] -> (movie5) MERGE (movie1) - [:CONTAINS] -> (word1) MERGE (movie1) - [:CONTAINS] -> (word2) MERGE (movie1) - [:CONTAINS] -> (word3) MERGE (movie1) - [:CONTAINS] -> (word4) MERGE (movie2) - [:CONTAINS] -> (word12) MERGE (movie2) - [:CONTAINS] -> (word13) MERGE (movie3) - [:CONTAINS] -> (word5) MERGE (movie3) - [:CONTAINS] -> (word6) MERGE (movie3) - [:CONTAINS] -> (word7) MERGE (movie4) - [:CONTAINS] -> (word8) MERGE (movie4) - [:CONTAINS] -> (word9) MERGE (movie5) - [:CONTAINS] -> (word10) MERGE (movie5) - [:CONTAINS] -> (word11) MERGE (medal1) - [:BELONGS] -> (sub1) MERGE (medal2) - [:BELONGS] -> (sub1) MERGE (medal3) - [:BELONGS] -> (sub2) MERGE (sub1) - [:BELONGS] -> (category) MERGE (sub2) - [:BELONGS] -> (category) MERGE (sub3) - [:BELONGS] -> (category) MERGE (user1) - [:FRIENDS] -> (user2) MERGE (user2) - [:FRIENDS] -> (user3) MERGE (user1) - [:WON{date:'01/01/2020'}] -> (medal1) MERGE (user2) - [:WON{date:'01/01/2020'}] -> (medal2) MERGE (user3) - [:WON{date:'01/01/2020'}] -> (medal3)
  2. Creating Nodes ❖ CREATE (optionalVariable optionalLabels {optionalProperties}) ❖ Creating word

    nodes . ➢ CREATE (:WORD {text:’try’} ), (:WORD {text:’love’} ) ❖ Word is also a lemma ➢ CREATE (:WORD:LEMMA {text:’death’} ) MATCH(w:WORD {text:’death’}) SET w:LEMMA
  3. Nodes Operations ❖ adding properties to nodes. ➢ MATCH (word:WORD{text:’try’})

    SET description = ‘Try description’, ….. N PROPERTIES RETURN word ❖ removing properties to nodes. ➢ MATCH (word:WORD{text:’try’}) SET description = null REMOVE text RETURN word
  4. Creating Relationships CREATE (x)-[:REL_TYPE]->(y) CREATE (x)<-[:REL_TYPE]-(y) ❖ Creating Relationship ➢

    CREATE (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ) CREATE (movie) - [:CONTAINS] -> (word) ❖ Creating Relationship with Match ➢ MATCH (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ) CREATE (movie) - [:CONTAINS] -> (word) ❖ Creating Multiple Relationship ➢ MATCH (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ), (user: USER{name:’juan’}) CREATE (user) - [:LIKES] -> (movie) - [:contains] -> (word) RETURN user, movie, word
  5. Relationships Operations ❖ Creating Relationship Properties ➢ CREATE (word:WORD {text:’try’}

    ), (movie:MOVIE {name:’titanic’} ) CREATE (movie) - [rel:CONTAINS] -> (word) SET rel.date = ‘01/04/2020’ RETURN movie, word ❖ Creating Relationship Properties 2 ➢ MATCH (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ) CREATE (movie) - [:CONTAINS {date:’01/04/2020’} ] -> (word) ❖ Creating Relationship Properties 3 ➢ MATCH (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ) CREATE (movie) - [:CONTAINS [‘val1’, ‘val2’] ] -> (word) ❖ Creating Relationship if it doesn’t exist ➢ MATCH (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ) WHERE NOT EXISTS (movie) - [:CONTAINS ] -> (word) CREATE (movie) - [:contains ] -> (word)
  6. Deleting Relationships and Nodes ❖ Creating Relationship Properties ➢ MATCH

    (movie:MOVIE {name:’titanic’} ) - [rel:contains] -> (word:WORD {text:’try’} ) DELETE rel RETURN movie, word ❖ Creating Relationship Properties 2 ➢ MATCH (movie:MOVIE {name:’titanic’} ) DETACH DELETE movie
  7. Merging Data MERGE (variable:Label{nodeProperties}) RETURN variable MERGE (variable:Label {nodeProperties})-[:REL_TYPE]->(otherNode) RETURN

    variable ❖ Creating node if it not found ➢ MERGE (word:LEMMA {text:’try’} ) RETURN word ❖ Creating Properties ➢ MERGE (movie:MOVIE {name:’titanic’} ) ON CREATE SET movie.date = “01/02/1998’ ON MATCH SET movie.date = “01/02/1998’ RETURN movie ❖ Creating relationship with merge MATCH (word:WORD {text:’try’} ), (movie:MOVIE {name:’titanic’} ) MERGE (movie) - [:contains] -> (word)
  8. ❖ Crear todo el grafo de ejemplo: ➢ Mínimo 3

    películas ➢ 3 nodos de palabras por película ➢ 2 usuarios ➢ Cada usuario le gusta dos películas y solo hay una película que les gusta a los dos. ➢ Cada usuario ha ganado dos medallas ➢ Las medallas las han ganado con 5 y 10 trofeos. ➢ Crear las categorias y subcategorias. Ejercicio
  9. Querying Nodes MATCH (variable) RETURN variable MATCH (variable:Label) RETURN variable

    ❖ All nodes in the graph ➢ MATCH (n) RETURN n ❖ All users in the graph ➢ MATCH (user:USER) RETURN user
  10. Match to Retrieve MATCH (variable {propertyKey: propertyValue}) RETURN variable MATCH

    (variable:Label {propertyKey: propertyValue}) RETURN variable MATCH (variable {propertyKey1: propertyValue1, propertyKey2: propertyValue2}) RETURN variable MATCH (variable:Label {propertyKey: propertyValue, propertyKey2: propertyValue2}) RETURN variable
  11. Match to Retrieve ❖ Movie Titanic ➢ MATCH (movie:MOVIE {name:’titanic’}

    ) RETURN movie ➢ MATCH (movie:MOVIE {name:’titanic’, date:’01/02/1998’} ) RETURN movie ➢ MATCH (movie:MOVIE {date:’01/02/1998’} ) RETURN movie.name, movie.date
  12. Querying using Relationships () // a node ()--() // 2

    nodes have some type of relationship ()-->() // the first node has a relationship to the second node ()<--() // the second node has a relationship to the first node MATCH (node1)-[:REL_TYPE]->(node2) RETURN node1, node2 MATCH (node1)-[:REL_TYPEA | :REL_TYPEB]->(node2) RETURN node1, node2 CALL db.schema
  13. ❖ MATCH (movie:MOVIE {name:’titanic’} ) - [:CONTAINS] -> (word:WORD) RETURN

    movie, word ❖ MATCH (user:USER {name:’juan’} ) - [:LIKES] -> (movie:MOVIE) - [:CONTAINS] -> (word:WORD) RETURN user, movie, word ❖ MATCH (user:USER) - [:LIKES] -> (:MOVIE{name:’titanic’}) RETURN user.name ❖ MATCH (user:USER) --> (:MOVIE{name:’titanic’}) RETURN user.name Querying using Relationships
  14. ❖ MATCH (movie:MOVIE {name:’titanic’} ) - [rel] -> (word:WORD) RETURN

    movie, word, type(rel) ❖ MATCH (user:USER) - [:won {numtrophies:3} ] -> (medals:MEDALS) RETURN user, medals ❖ MATCH (user:USER) - [LIKES] -> (:Movie) - [:CONTAINS] -> (:WORD {text:’love’}) RETURN user ❖ MATCH path = (:USER) - [LIKES] -> (:Movie) - [:CONTAINS] -> (:WORD {text:’love’}) RETURN path Querying using Relationships
  15. ❖ Obtener todas las palabras que contiene la película “TITANIC”

    ❖ Obtener todas las series y las palabras que le gusta al usuario “JUAN” ❖ Obtener las subcategorías de la categoría “MASTERY” ❖ Obtener las medallas ganadas por “JUAN” ❖ Obtener a qué usuarios les gusta la pelicula de “TITANIC” y los trofeos de cada usuario Ejercicios