I am using Neo4j 2.0 RC1. Problem comes from very simple command:
START n=node:nodes(name = "A")
RETURN n
But my Browser return: "Unknown error" like bellows picture
Could someone pls tell me how to fix this problem...??
did you create and populate an index named "nodes" upfront? If you didn't and don't want to, go with
MATCH (n) WHERE n.name = "A" RETURN n
or
MATCH (n:Label) WHERE n.name = "A" RETURN n
Start is depreciated in 2.0, though the tutorials might not all be updated yet to reflect it. Instead of Start, use Match:
Match (n:Label)
Related
I want to find all loops that originate and terminate with a specific node in a Neo4j database. I tried:
START n=node:Event(time=",timestamp,")
MATCH p=(n)-[:LINKED_TO*1..5]->(n)
WHERE NONE (n IN nodes(p) WHERE size(filter(x IN nodes(p) WHERE n = x))> 2)
RETURN p, length(p)
This is the best I can mashup from what is on the web. There are two things I don't like about this:
1. it crashes
2. the count threshold must be ">2" to allow for the start+termination node. That means that loops that visit the same intermediate node twice will be included, which I wish was not the case.
I'm not interested in the shortest path. I want to know all loops that return to my starting node.
Thank you in advance!
This query should return all loops that start and end at the specified node and have no other repeated nodes:
START n=node:Event(time=",timestamp,")
MATCH p=(n)-[:LINKED_TO*1..5]->(n)
UNWIND TAIL(NODES(p)) AS m
WITH p, COUNT(DISTINCT m) AS cm
WHERE LENGTH(p)-1 = cm
RETURN p, LENGTH(p);
Thank you, cybersam! That was helpful. As typed, it gave a few errors and warned me that "START" is deprecated. I found the following modifications worked:
MATCH (n:Event{time:1458238060505007})
MATCH p=(n)-[:LINKED_TO*1..5]->(n)
UNWIND TAIL(NODES(p)) AS m WITH p RETURN p
The only problem with this is that it appears to give all paths that go through the desired start node, n. Is that true? If so, is there a way to correct this?
This what finally worked for me. It is very close to what cybersam suggested. Apologies for doing this "the wrong way". I'm sure cybersam will yell at me, again, but adding code via Comment is not very easy to read.
MATCH p=(n:Event{time:",timestamp,"})-[:LINKED_TO*1..5]->(n)
UNWIND TAIL (NODES(p)) AS m
WITH p,COUNT(DISTINCT m) AS cm
WHERE LENGTH(p) = cm
RETURN p
As I noted earlier, one sticking point was the use of "START", which is deprecated and causes errors (for example, when using RNeo4j in R, which I'm using). The new way appears to be to use MATCH and specify your starting node in the path pattern. The other confusing thing for me was the use of "LENGTH(p)-1" instead of "LENGTH(p)". For one node connecting to another, the path has a length of 2, not 3 and there are only 2 distinct nodes. For my application, "LENGTH(p)=cm" worked.
Finally, if you want the nodes in the paths, do NOT try to use "WITH m,..." because this messes up the "COUNT(DISTINCT(m))" computation for some reason that I do not understand.
I have a Cypher query that returns closed circle results like
a-b-t-y-a
b-t-y-w-b
and so on ...
From time to time I get results like
a-b-c-b-e-f-a
c-e-r-e-f-g-c
c-e-r-c-d-a-c
that I do not need
In this two results I have b-in first and e in second visited twice , and c is
shown also not only at the start and at the end as it should be but in the middle also .
.
How to avoid getting results where I get same nodes in inner part of results or getting the start and end node inside also .The same node at the start and at the end of result is fine .
My current cypher query is :
start n=node(*) match p=n-[r:OWES*1..200]->n
where HAS(n.taxnumber)
return extract(s in `relationships(p) : s.amount), extract(t in nodes(p) : ID(t)), length(p);
I am getting all nodes that have taxnumber property and connected with relation OWES , all
up to level of 200 .
If you can't use CASE you could try this. I tested it on a 1.8.3 server and it runs ok. I was not able to get it working with reduce. I got several different unexpected errors, including 'unclosed parenthesis' and type comparison problems, this was the only query that I got to work. I also ran it on a 2.0 server and it finished in ~39s what Michael's query with case and reduce did in ~23s on a mockup data set.
START n=node(*)
MATCH p=n-[r:OWES*1..200]->n
WHERE HAS(n.taxnumber) AND
ALL(x IN tail(nodes(p)) WHERE SINGLE(y IN tail(nodes(p)) WHERE x=y))
RETURN EXTRACT(s IN relationships(p) : s.amount), EXTRACT(t IN nodes(p) : ID(t)), length(p)
Can't you check for non-duplicates in the path?
Right now cypher misses a uniq function that would make this simple, here is a workaround.
This is for neo4j 2.0, as 1.9 doesn't have case when there it might be more involved, probably using filter
start n=node(*)
match p=n-[r:OWES*1..200]->n
where HAS(n.taxnumber) AND
reduce(a=tail(nodes(p)), x in tail(nodes(p)) |
case when a IS NOT null OR x in tail(a) then null else tail(a) end) IS NOT NULL
return extract(s in relationships(p) | s.amount), extract(t in nodes(p) | ID(t)), length(p);
for 1.9 you might use an expression like this to find duplicates:
WITH [1,2,3] AS coll
reduce(a=false, x IN coll : a OR length(filter(y IN coll : x = y))> 1)
It works like this:
uses tail(nodes(path)) b/c you have the same start and end node which would always be a duplicate
it reduces over all elements of the collection and when it doesn't find the element again in the rest of the collection it returns the rest of the collection and repeats
otherwise it returns null and shortcuts
This is my cypher query:
start n=node(*) match p=n-[r:OWES*1..200]->n
with count(n) as numbern ,count(r) as numberr
where HAS(n.taxnumber) and numbern >= numberr
return extract(s in `relationships(p) : s.amount), extract(t in nodes(p) : ID(t)), length(p); `
This gives me Unknown identifier n error. What is wrong with this? I use Neo4j 1.8.2 for this.
n is no longer visible after your WITH,
p is also not visible then.
This query doesn't make any sense, what do you want to achieve with the aggregation?
Both counts return the same number btw.
Besides what we already discussed in the other issues, what do you want to achieve?
I'm playing with 2.0 M6 neo4j server (oracle jdk7 on win7 64).
I'm trying to delete a node and its relationships using a single cypher query over the REST API.
The query I create (which works if I run it in the browser UI) looks like:
START n = node( 1916 ) MATCH n-[r]-() DELETE n, r
Which by the time I put it through gson comes out as:
{"query":"START n \u003d node( 1916 ) MATCH n-[r]-() DELETE n, r"}
Which when sent to the server gets the response:
{
"columns" : [ ],
"data" : [ ]
}
My test fails because the node can still be found in neo4j server by its id...
If I simplify my query to just delete a node (that has no relationships) so its:
START n = node( 1920 ) DELETE n
Which becomes
{"query":"START n \u003d node( 1920 ) DELETE n"}
Then the node is deleted.
Have I missed something?
Thanks, Andy
For neo4j 2.0 you would do
START n=node(1916)
OPTIONAL MATCH n-[r]-()
DELETE r, n;
MATCH n-[r]-() will only match the node if there is at least one relationship attached to it.
You want to make the relationship match optional: MATCH n-[r?]-()
Also, you need to delete the relationships before the node.
So, your full query is:
START n=node(1916)
MATCH n-[r?]-()
DELETE r, n
Both START and the [r?] syntax are being phased out. It's also usually not advised to directly use internal ids. Try something like:
match (n{some_field:"some_val"})
optional match (n)-[r]-()
delete n,r
(see http://docs.neo4j.org/refcard/2.1/)
The question mark(?) is not supported in Neo4J 2.0.3, so the answer would be to use OPTIONAL MATCH
START n=node(nodeid)
OPTIONAL MATCH n-[r]-()
DELETE r, n;
And again there is a pretty syntax change. Neo4j 2.3 introduces the following:
MATCH (n {id: 1916})
DETACH DELETE n
Detach automatically deletes all in- and outgoing relationships.
Based upon latest documents and I have also tested it
START n=node(1578)
MATCH (n)-[r]-()
DELETE n,r
We have to put () around n and there is also no need of ? in [r?].
Even it works without OPTIONAL.
How can I show all nodes and relationships in Data Browser tab?
What are sample index queries that I can type in in search field?
You may also want to try a cypher query such as:
START n=node(*) RETURN n;
It's very obvious, and it will return all the existing nodes in the database.
EDIT : the following displays the nodes and the relationships :
START n=node(*) MATCH (n)-[r]->(m) RETURN n,r,m;
More simple way is
MATCH (n) RETURN (n)
MATCH (n) OPTIONAL MATCH (n)-[r]-() RETURN n, r;
You can show everything with simple MATCH (n) RETURN n, as offical documentation suggests.
START n=node(*) RETURN n from Neo4j 2.0 is deprecated:
The START clause should only be used when accessing legacy indexes
(see Chapter 34, Legacy Indexing). In all other cases, use MATCH
instead (see Section 10.1, “Match”).
There is a little help icon beside the search field, if you hoover over it it shows the syntax.
If a property of your nodes and relationships is indexed you can search for all of them like this.
node:index:indexname:fieldname:*
rels:index:indexname:fieldname:*
I found that this worked, retrieving all nodes including orphans, and all relationships:
MATCH (n) MATCH ()-[r]->() RETURN n, r
Other good way for get ALL nodes (and nodes without relationship) :
MATCH (n) RETURN n UNION START n = rel(*) return n;