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 10Calcular 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 totalCambiar la dirección de una RELACIÓN
MATCH (n)-[r:OLD_REL]->(d) CREATE (n)<-[r2:NEW_REL]-(d) SET r2=r DELETE rRenombrar un Nodo
MATCH (n:OldNodeName) REMOVE n:OldNodeName SET n:NewNodeNameBuscar nodos que no tiene etiquetas (labels)
MATCH (n) WHERE size(labels(n)) = 0 RETURN nBuscar una relación según su ID (ejemp. ID = 14) y Borrarla
START r=rel(14) DELETE rBuscar un nodo según su ID y devolverlo
START n=node(15) RETURN nDevolver todos los Nodos
START n=node(*) RETURN labels(n)[0] AS Etiq, n.name AS Nodo, n.direccion AS DirecciónEliminar todos los Nodos
MATCH (n) DETACH DELETE nCargar 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 nFind all nodes that have a specific property:
MATCH (n) WHERE EXISTS(n.name) RETURN nFind all nodes that have a specific relationship (regardless of the direction of the relationship):
START n=node(*) MATCH (n)-[:SOME_RELATIONSHIP]-() RETURN DISTINCT nShow 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 DESCGet 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 rSTART n=node(*)
MATCH n DELETE nA simple query to get nodes of a certain category that MATCH a certain property
MATCH (n:Person) WHERE n.name="Tim" RETURN nPoner un nombre de etiqueta a un Nodo que no tiene nombre
MATCH (n) WHERE size(labels(n)) = 0 SET n:Nuevo_nombreBúsqueda de toda la cadena de pertenencia
MATCH (a)-[r:PERTENECE_A*]->(p:Person {name: "Antonio Miguel"}) RETURN rDevolver todos los Nodos que PERTENECE_A un Nodo dado por su ID
START n=node(0) MATCH (n)<-[:PERTENECE_A*]-(b) RETURN DISTINCT bObtiene 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, nObtiene el ID de un Nodo
START n = node(*) WHERE n.name =~ '.*toni.*' RETURN ID(n) AS ID,n AS Node