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

Neo4j - Advanced Queries

Neo4j - Advanced Queries

En esta presentación se presenta:
- Consultas avanzadas.
- Encontrar rutas cortas.
- Cláusula UNWIND, WITH, OPTIONAL MATCH.
- Constraints.
- Indexes.
- Recommendation.

Transcript

  1. Neo4j - ADVANCED QUERIES MODULE 3

  2. Graph Model Reference

  3. Filtering Queries Using Where ❖ MATCH (movie:MOVIE) - [:CONTAINS] ->

    (word:WORD) WHERE movie.name = ‘titanic’ RETURN movie, word ❖ MATCH (movie) - [:CONTAINS] -> (word) WHERE movie:MOVIE AND word:WORD AND movie.name = ‘titanic’ RETURN movie, word ❖ MATCH (movie:MOVIE) - [:CONTAINS] -> (word:WORD) WHERE movie.name = ‘titanic’ AND EXISTS(movie.date) RETURN movie, word
  4. Filtering Queries Using Where ❖ MATCH (movie:MOVIE) - [:CONTAINS] ->

    (word:WORD) WHERE movie.name STARTS WITH ‘tita’ RETURN movie, word ❖ MATCH (movie:MOVIE) - [:CONTAINS] -> (word:WORD) WHERE toLower(movie.name) ENDS WITH ‘nic’ RETURN movie, word ❖ MATCH (movie:MOVIE) - [:CONTAINS] -> (word:WORD) WHERE movie.name = ‘titanic’ AND EXISTS((movie) - [:CONTAINS] -> (word)) RETURN movie, word ❖ MATCH (movie:MOVIE) - [:CONTAINS] -> (word:WORD) WHERE movie.name IN [‘scarface’, ‘titanic’] RETURN movie, word
  5. Length and Shortest Path ❖ MATCH (:MOVIE{name:"titanic"}) - [:CONTAINS*2] ->

    (w:WORD) RETURN w ❖ MATCH (:MOVIE{name:"titanic"}) - [*2] -> (w:WORD) RETURN w ❖ MATCH p = shortestPath((user:USER) - [*] - (sb:SUB_CATEGORY)) WHERE user.name = 'Juan' RETURN p
  6. Advanced Queries ❖ MATCH (movie:MOVIE) OPTIONAL MATCH (movie) - [:CONTAINS]

    -> (word:WORD) RETURN movie, word.text ❖ MATCH (w:WORD) RETURN count(w) ❖ MATCH (m:MOVIE{name:'GOT'}) - [:CONTAINS] -> (w:WORD) RETURN m, count(w.text) as count_word ❖ MATCH (m:MOVIE{name:'GOT'}) - [:CONTAINS] -> (w:WORD) RETURN m, collect(w.text) ❖ MATCH (m:MOVIE) - [:CONTAINS] -> (w:WORD) WITH m, count(w) as count_words WHERE count_words >= 2 RETURN m
  7. Advanced Queries ❖ MATCH (m:MOVIE) RETURN m as movie ORDER

    BY m.name desc ❖ MATCH (m:MOVIE) RETURN m as movie ORDER BY m.name desc LIMIT 2 ❖ WITH [1, 2, 3] AS list UNWIND list AS row RETURN row ❖ WITH ["titanic", "GOT", "Toy Story"] AS movies UNWIND movies AS movie MATCH (m:MOVIE{name:movie}) RETURN m ❖ CREATE CONSTRAINT ON (p:Person) ASSERT (p.name, p.born) IS NODE KEY
  8. Constraints ❖ CREATE CONSTRAINT ON (user:USER) ASSERT user.name IS UNIQUE

    ❖ CREATE CONSTRAINT ON (user:USER) ASSERT exists(user.name) ❖ CREATE CONSTRAINT ON ()-[rel:LIKES]-() ASSERT exists(rel.rated) ❖ CREATE CONSTRAINT ON (user:USER) ASSERT (p.name, p.age) IS NODE KEY ❖ CALL db.constraints() ❖ DROP CONSTRAINT ON (user:USER) ASSERT exists(user.name)
  9. Indexes

  10. Indexes ❖ CREATE INDEX FOR (user:USER) ON (user.name) ❖ CREATE

    INDEX FOR (user:USER) ON (user.name, user.age) ❖ CALL db.indexes() ❖ DROP INDEX INDEX_NAME
  11. RECOMMENDATION ❖ MATCH (juan:USER{name:"Juan"}) - [likes:LIKES] -> (m:MOVIE) <- [likes_rel:LIKES]

    - (amigosamigos:USER) WHERE (juan) - [:FRIENDS] -> (:USER) - [:FRIENDS] -> (amigosamigos) AND toInteger(likes.rated) = 5 RETURN amigosamigos, m
  12. ❖ Obtener cuantas palabras tiene la película “Titanic” ❖ Obtener

    cuantos numeros de trofeos en total tiene un usuario. ❖ Obtener todas las palabras por película pero en una lista. ❖ Cuantas palabras está viendo en total el usuario. ❖ Obtenga los usuarios que tienen solo una medalla Ejercicios
  13. LINKS • https://neo4j.com/docs/cypher-manual/current/functions/ • https://neo4j.com/docs/cypher-manual/current/syntax/

  14. https://www.facebook.com/escueladevhack / Escuela de Hackers www.devhack.co https://github.com/escueladevhac k