How can I rename property in the Neo4j web admin application? - neo4j

I have created a relationship (for example "KNOWS") between 2 nodes on the Neo4j webAdmin application. If I want to rename the relationship (from "KNOWS" to "LOVES"), how can I do it?
The solution I have so far is delete the "KNOWS" relationship and create a new "LOVE" relationship.
Is there any easier way to do this?
Thanks,

Yes, that is how you do it. In the cypher console, you can do
start n=node(1) match n-[r:KNOWS]->m create n-[:FRIEND]->m delete r
see http://tinyurl.com/7umvpro for an example.

If you are using the Embedded neo4j then renaming is not possible. For this, you'll have to delete the existing relationship b/w nodes and then create new relationship b/w same nodes again.
Make sure you all do this in a transaction.
Regards,
Sushil Jain
Click here

Related

How to use Structr platform to add a new node to an existing relationship?

I am new to Neo4j and I want to add a new node to an existing relationship using Structr platform.
This is the Cypher query that I tested in Neo4j web browser and it works.
match(projects:Project {name:'IRIS Recognition Java'})
create(client:Client {name:'Andreas Pal'}) CREATE(projects)-[w:IS_PART_OF]->(client)
return w
In the Structr platform I created a table that contains all the existing projects from database and I want to assign a member to the project.
I tried to put in a table date a query to bring also the clients. And I don't know how to create the assign. Any help would be very much appreciated. Thank you.
You need to use Structr's means of editing the data in the database. If you use Cypher directly, you are modifying the data on a different (wrong) level. You can either use Structr directly to create relationships, or you have to make the relationships in a way that Structr can detect and "see" those relationships.
Please have a look at https:/support.structr.com/article/295 for more information.

How to create a new relationship in Neo4j, starting from an existing one, using a Cypher query?

Is there a simple way to create a new relationship in Neo4j, starting from an existing one?
Starting from the actor-director-movie database used in the tutorials, what I would like to do is to get all the {(actor1),(actor2)} couples of nodes in the graph satisfying the relationships:
(actor1)-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(actor2)
and use them to create a new relationship like:
(actor1)-[:ACTED_IN_THE_SAME_MOVIE_AS]-(actor2)
in whatever direction (I am interested in both directed and undirected graphs).
Is there a way to do this with a simple Cypher query?
Many thanks,
sTe
Using the sample movie dataset:
MATCH (actor1:Person)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(actor2:Person)
WITH actor1, actor2
MERGE (actor1)-[:ACTED_IN_THE_SAME_MOVIE_AS]-(actor2)
I'd do that :
MATCH (actor1)-[:ACTED_IN]->()<-[:ACTED_IN]-(actor2)
CREATE UNIQUE (actor1)-[:ACTED_IN_THE_SAME_MOVIE_AS]-(actor2)
which is basically what you said. Relations are uni-directional (no way around), but the api (Cypher queries or Traversal) can read them both ways (so it doesn't really matter which way your create them in some cases).
To check if what you did is ok, you can run the following :
MATCH (actor1)-[:ACTED_IN_SAME_MOVIE]-(actor2)
RETURN actor1, actor2

Creating relationship conditionally with cypher (neo4j)

I am attempting to create a linked list with neo4j, and have a root node with no relationships. Here is the pseudo cypher I am trying to create, but I am not sure how, or even if it is possible:
START root=node(1), item=node(2)
MATCH root-[old?:LINK]->last
WHERE old IS NOT NULL
CREATE root-[:LINK]->item-[:LINK]->last
DELETE old
WHERE old IS NULL
CREATE root-[:LINK]->item
Basically I am trying to insert a node into the list if the list exists, and simply create the first list item otherwise. Obviously you cannot do multiple WHEREs like I have done above. Any ideas how I can achieve this desired functionality with a cypher?
The docs solve the problem by first creating a recurrent :LINK relationship on the root node, but I would like to solve this without doing that (as you then need to create possibly unnecessary relationships for each node).
For anyone interested, I figured out a way to solve the above using some WITH tricks. This is essentially a solution for creating linked lists in neo4j without having to first create a self referencing relationship.
START root=node(1), item=node(2)
MATCH root-[old?:LIST_NEXT]->last
CREATE root-[:LIST_NEXT]->item
WITH item, old, last
WHERE old IS NOT NULL
CREATE item-[:LIST_NEXT]->last
DELETE old
This works by first looking for an existing link relationship, and creating the new one from the root to the item. Then by using WITH we can chain the query to now examine whether or not the matched relationship did in fact exist. If it did, then remove it, and create the remaining link piece from the new item to the old one.
For this, you might want to look at MERGE, http://docs.neo4j.org/chunked/snapshot/query-merge.html#merge-merge-with-on-create-and-on-match
And maybe at the linked list example, http://docs.neo4j.org/chunked/snapshot/cookbook-linked-list.html

Delete all relations and connected nodes in Neo4j for a user

We have selected neo4j as the DB for our web application. The user has a large number of relations and connected nodes. As of now there are about 20 relations for a user. One of the features is a newsfeed feature. If i want to delete a user completely, is the cypher query the best way to delete or is there any other alternative?
Since we are still planning to add new features, the relationships and nodes connected to the user also will increase. So if we use cypher query, the query has to be modified for every new relationship added. Please advise.
Thanks,
Pavan
Yes, you can use Cypher to remove a user. Of course, there are alternative methods, depending on the language or framework you're using with your web application. If you like to have advise on that, please specifiy how you're using Neo4j in detail.
Note that you have to remove all relationships (outgoing and incoming) first in order to be able to remove the node.
Example:
START n = node(3)
MATCH n-[r]-()
DELETE n, r
This example was taken from the official manual: http://docs.neo4j.org/chunked/milestone/query-delete.html
As of Neo4j 2.3 there is another way to do this:
MATCH (n { name:'Andres' })
DETACH DELETE n
I found this example in the documentation at: http://neo4j.com/docs/stable/query-delete.html
An alternative could be to write a gremlin script that traverses your graph starting with your user and is putting in two collection the relationships and the nodes that you intend to delete. If you want to delete everything, perhaps you can implement your depth first traversal in Gremlin and delete while traversing.

neography / neo4j change relationship end point to another node

i am using neography as wrapper of the REST api for Neo4j graph database.
I often need to change the end node of a relationship, but i am not able to do this using neography.
Of course, i can delete the relation then recreate it with the new end point node, but it's not optimized.
Does anyone had this problem?
Thanks.
Neo4j does not allow replacing the nodes of a relationship (see javadoc). So you only can create a new relationship to the new end node and delete the other relationship.

Resources