NEO4J
Aplicaciones:
- Whiteboard friendly
- Nice for tree like structures
Arrancar la instancia en local:
> neo4j start
Acceso web al administrador:
Ver en localhost Usuario: neo4j, Password: neo4j
Ficheros en:
cd /usr/local/Cellar/neo4j/3.0.7/libexec
Clientes
Búsquedas:
Obtener 10 nodos ordenados por su id
MATCH (n:NodeName) RETURN n ORDER BY id(n) LIMIT 10
Calcular la base imponible de una factura
START invoice=Node(4) MATCH (invoice)-[item:HAS_ITEM]->(p) RETURN invoice.number, sum(item.amount*item.price) AS total
Cambiar la dirección de una RELACIÓN
MATCH (n)-[r:OLD_REL]->(d) CREATE (n)<-[r2:NEW_REL]-(d) SET r2=r DELETE r
Renombrar un Nodo
MATCH (n:OldNodeName) REMOVE n:OldNodeName SET n:NewNodeName
Buscar nodos que no tiene etiquetas (labels)
MATCH (n) WHERE size(labels(n)) = 0 RETURN n
Buscar una relación según su ID (ejemp. ID = 14) y Borrarla
START r=rel(14) DELETE r
Buscar un nodo según su ID y devolverlo
START n=node(15) RETURN n
Devolver todos los Nodos
START n=node(*) RETURN labels(n)[0] AS Etiq, n.name AS Nodo, n.direccion AS Dirección
Eliminar todos los Nodos
MATCH (n) DETACH DELETE n
Cargar de un CSV y crear o actualizar
USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "http://localhost/person_node.csv" as row MERGE (p:Person {personId: row.person_id}) ON CREATE SET p.name = row.name;
Creación:
Crear una propiedad (p.e: ID) "única"
CREATE CONSTRAINT ON (n:Node) ASSERT n.ID IS UNIQUE
Meta-datos:
Obetener todos las Etiquetas de Nodos de forma separada
MATCH (n) WITH DISTINCT labels(n) AS labels UNWIND labels AS label RETURN DISTINCT label ORDER BY label
basic cypher queries
Find the unique labels that appear in the database:
MATCH (n) RETURN DISTINCT labels(n)
Find the unique relationships that appear in the database:
MATCH (n)-[r]-() RETURN DISTINCT type(r)
Combine the previous two queries to RETURN the unique combinations relationships and labels in the database:
MATCH (n)-[r]-() RETURN DISTINCT labels(n), type(r)
Find nodes that don't have any relationships:
START n=node(*) MATCH (n)-[r?]-() WHERE r IS NULL RETURN n
Find all nodes that have a specific property:
MATCH (n) WHERE EXISTS(n.name) RETURN n
Find all nodes that have a specific relationship (regardless of the direction of the relationship):
START n=node(*) MATCH (n)-[:SOME_RELATIONSHIP]-() RETURN DISTINCT n
Show the nodes and a count of the number of relationships that they have:
START n=node(*) MATCH n-[r]-() RETURN n, count(r) AS rel_count ORDER BY rel_count DESC
Get a count of all nodes in your graph:
START n=node(*) MATCH n RETURN count(n)
To DELETE all nodes in a database (first you have to DELETE all relationships)
START n=node(*) MATCH n-[r]-() DELETE r
START n=node(*)
MATCH n DELETE n
A simple query to get nodes of a certain category that MATCH a certain property
MATCH (n:Person) WHERE n.name="Tim" RETURN n
Poner un nombre de etiqueta a un Nodo que no tiene nombre
MATCH (n) WHERE size(labels(n)) = 0 SET n:Nuevo_nombre
Búsqueda de toda la cadena de pertenencia
MATCH (a)-[r:PERTENECE_A*]->(p:Person {name: "Antonio Miguel"}) RETURN r
Devolver todos los Nodos que PERTENECE_A un Nodo dado por su ID
START n=node(0) MATCH (n)<-[:PERTENECE_A*]-(b) RETURN DISTINCT b
Obtiene los nodos de una cadena de relación y muestra datos en columnas
START n=node(0) MATCH (d)-[r:PERTENECE_A*]->(n) RETURN DISTINCT labels(d)[0] as Nodo, d, type(r[0])
Buscar un Nodo cuya propiedad coincida con un patron regular
START n = node(*) WHERE n.name =~ '.*toni.*' RETURN n.name, n
Obtiene el ID de un Nodo
START n = node(*) WHERE n.name =~ '.*toni.*' RETURN ID(n) AS ID,n AS Node