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.

More Decks by Juan Guillermo Gómez Torres

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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)
  7. 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
  8. 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
  9. ❖ 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