Influxdb merge syntax - influxdb

What's the right syntax to merge 3 or more series on influxdb?
Query below works fine for 2 series:
select mean(value) from "serieA"
merge "serieB"
where time > now ()-3m group by time(60s)
Tried:
merge "serieB" "serieC"
merge ("serieB","serieC")
merge "serieB","serieC"
merge "serieB" and "serieC"
EDIT: Figured that out:
select mean(value) from merge (serieA,serieB,serieC) ...
Thanks!

You can also use wildcards like this, with InfluxDB <= 0.8:
select mean(value) from merge(/my.series.*/) group by time(1d);
NOTE: The syntax has been deprecated since InfluxDB 0.9. See the InfluxDB documentation. In InfluxDB 0.9 neither the MERGE nor JOIN operations are supported.

Related

CREATE works in this statement, but MERGE does't. What's going on here?

I am trying this statement.
CREATE (n:TestEntity), (m1:RelatedEntity)
WITH n,m1
MERGE (m2:RelatedEntity {b:"c"})
WITH n,m1,m2
MERGE (n)-[:REL]->(m1), (n)-[:REL]->(m2)
SET n+={a:1}, m1+={b:"d"}, m2+={d:2}
return n, m1,m2;
This gives an error:
If I change the last MERGE with CREATE, this exact statement works.
If I remove the second relationship and only MERGE the first one, it works. What's going on? Is this a bug?
At this time in Neo4j 4.2.x, MERGE does not support comma-separated patterns, though there is a feature request in the backlog to add that capability.
MERGE does not support comma separation. A workaround is to add the MERGE keyword in front of every node/relation etc.
The below query works:
CREATE (n:TestEntity), (m1:RelatedEntity)
WITH n,m1
MERGE (m2:RelatedEntity {b:"c"})
WITH n,m1,m2
MERGE (n)-[:REL]->(m1) MERGE (n)-[:REL]->(m2)
SET n+={a:1}, m1+={b:"d"}, m2+={d:2}
return n, m1,m2;

Agregate cypher query

This is my database in Neo4j:
CREATE (Alex:Person {name:'Alex', phone:'0420965111'})
CREATE (Oxana:Person {name:'Oxana', email:'oxana#mail.com'})
CREATE (Tango:Dance {name:'Tango'})
CREATE (Ballet:Dance {name:'Ballet'})
CREATE (Zouk:Dance {name:'Zouk'})
CREATE (Saturday:Day {name:'Saturday'})
CREATE (Sunday:Day {name:'Sunday'})
CREATE (Wednesday:Day {name:'Wednesday'})
MERGE (Alex)-[:LIKES]->(Tango)
MERGE (Alex)-[:LIKES]->(Zouk)
MERGE (Oxana)-[:LIKES]->(Tango)
MERGE (Oxana)-[:LIKES]->(Ballet)
MERGE (Alex)-[:AVAILABLE_ON]->(Sunday)
MERGE (Alex)-[:AVAILABLE_ON]->(Wednesday)
MERGE (Oxana)-[:AVAILABLE_ON]->(Sunday)
MERGE (Oxana)-[:AVAILABLE_ON]->(Saturday)
I need a list of more than 1 person who likes the same dance and available on the same day. How to write a query which returns this?:
"Sunday", "Tango", ["Alex","Oxana"]
This almost works: match (p:Person), (d:Dance), (day:Day) where (p)-[:LIKES]->(d) and (p)-[:AVAILABLE_ON]->(day) return day.name, d.name, collect(p.name), count(*) But I don't know how to exclude records where count(*) is less than 2.
You can use WITH:
match (p:Person), (d:Dance), (day:Day)
where (p)-[:LIKES]->(d) and (p)-[:AVAILABLE_ON]->(day)
with day.name as day, d.name as dance, collect(p.name) as names, count(*) as count
where count >= 2
return day, dance, names
From the docs:
The WITH clause allows query parts to be chained together, piping the
results from one to be used as starting points or criteria in the
next.
Also, you can add a constraint (WHERE clause) to filter data.

WITH is required between MERGE and MATCH (line 4, column 1 (offset: 63))

I am trying to use CYPHER to create a simple graph on NEO4J.
Below is the query:
MERGE (nut:asset{name:'nut'})
MERGE (bolt:asset{name:'bolt'})
MATCH (nut:asset)
WITH nut,bolt
MERGE (nut:asset)-[:hasPart]->(washer:asset{name:'washer',domain:'tool'})
Its throws me an error
WITH is required between MERGE and MATCH (line 4, column 1 (offset: 63))
"MATCH (nut:asset)"
^
When I try to change my query to
MERGE (nut:asset{name:'nut'})
MERGE (bolt:asset{name:'bolt'})
MERGE (nut:asset)-[:hasPart]->(washer:asset{name:'washer',domain:'tool'})
Its says
Can't create node `nut` with labels or properties here. The variable is already declared in this context
How to use the MERGE statement in this context. I have used the tutorial from Neo4j link to construct my query.
The first error was caused because after creating two nodes, you directly used MATCH. The query is continuous but you broke it into parts using MATCH with a label. In order to maintain continuation you've to use WITH.
The second error was caused because you are using the same variable nut twice.
Using WITH you can reduce the cardinality and time taken by the query.
The first query can be written like this:
MERGE (nut:asset{name:'nut'})
with nut
MERGE (bolt:asset{name:'bolt'})
with nut,bolt
MERGE (nut)-[:hasPart]->(washer:asset{name:'washer',domain:'tool'})
and the second one:
MERGE (nut:asset{name:'nut'})
MERGE (bolt:asset{name:'bolt'})
MERGE (nut)-[:hasPart]->(washer:asset{name:'washer',domain:'tool'})

Inserting a Relation into Neo4j using MERGE or MATCH runs forever

I am experimenting with Neo4j using a simple dataset of Locations. A location can have a relation to another relation.
a:Location - [rel] - b:Location
I already have the locations in the database (roughly 700.000+ Location entries)
Now I wanted to add the relation data (170M Edges), but I wanted to experiment with the import logic with a smaller set first, so I basically picked 2 nodes that are in the set and tried to create a relationship as follows.
MERGE p =(a:Location {locationid: 3616})-[w:WikiLink]->(b:Location {locationid: 467501})
RETURN p;
and also tried the approach directly from the docu
MATCH (a:Person),(b:Person)
WHERE a.name = 'Node A' AND b.name = 'Node B'
CREATE (a)-[r:RELTYPE { name : a.name + '<->' + b.name }]->(b)
RETURN r
I tried using a directional merge, undirectional merge, etc. etc. I basically tried multiple variants of the above queries and the result is: They run forever, seeming to no complete even after 15 minutes. Which is very odd.
Indexes
ON :Location(locationid) ONLINE (for uniqueness constraint)
Constraints
ON (location:Location) ASSERT location.locationid IS UNIQUE
This is what I am currently using:
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///edgelist.csv' AS line WITH line
MATCH (a:Location {locationid: toInt(line.locationidone)}), (b:Location {locationid: toInt(line.locationidtwo)})
MERGE (a)-[w:WikiLink {weight: toFloat(line.edgeweight)}]-(b)
RETURN COUNT(w);
If you look at the terminal output below you can see Neo4j reports 258ms query execution time, the realtime is however somewhat above that. This query already takes a few seconds too much in my opinion (The machine this runs on has 48GB RAM, 16 Cores and is relatively new).
I am currently running this query with LIMIT 1000 (before it was LIMIT 1) but the script is already running for a few minutes. I wonder if I have to switch from MERGE to CREATE. The problem is, I cannot understand the callgraph that EXPLAIN gives me in order to determine the bottleneck.
time /usr/local/neo4j/bin/neo4j-shell -file import-relations.cql
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| p |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [Node[758609]{title:"Tehran",locationid:3616,locationlabel:"NIL"},:WikiLink[9422418]{weight:1.2282325516616477E-7},Node[917147]{title:"Khorugh",locationid:467501,locationlabel:"city"}] |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row
Relationships created: 1
Properties set: 1
258 ms
real 0m1.417s
user 0m1.497s
sys 0m0.158s
If you haven't:
create constraint on loc:Location assert loc.locationid is unique;
Then find both nodes, and create the releationship.
MATCH (a:Location {locationid: 3616}),(b:Location {locationid: 467501})
MERGE p = (a)-[w:WikiLink]->(b)
RETURN p;
or if the locations don't exist yet:
MERGE (a:Location {locationid: 3616})
MERGE (b:Location {locationid: 467501})
MERGE p = (a)-[w:WikiLink]->(b)
RETURN p;
You should also use parameters if you do that from a program.
Have you indexed the Location nodes on locationid?
CREATE INDEX ON :Location(locationid)
I had a similar problem adding edges to a graph and indexing the nodes led to the linking running over 150x faster.
If the nodes aren't indexed neo4j will do a serial search for the two nodes to link together.
USING PERIODIC COMMIT <value>:
Specifies number of records(rows) to be commited in a transaction. Since you have high RAM, it is good to use value that is greater than 100000. This will reduce the number of transactions committed and might further reduce the overall time.

Neo4j - Cypher query to import category tree without duplicate entry

I have three independent category trees that is going to import at any order using cypher.
(c2)-[PARENT]->(c1)
(c4)-[PARENT]->(c3)->[PARENT]->(c1)
(c5)-[PARENT]->(c3)
and need to create the structure mentioned in the figure using the query . The query I written is
MERGE (:Category {name:'c2'})-[:PARENT]->(:Category {name:'c1'})
MERGE (:Category {name:'c4'})-[:PARENT]->(:Category {name:'c3'})-[:PARENT]->(:Category {name:'c1'})
MERGE (:Category {name:'c5'})-[:PARENT]->(:Category {name:'c3'})
But above query creating duplicate category c1 for second merge query which I need to avoid . Also the third query should create new category c3 which is happening correctly now.
One more thing is that these three cypher query should be independently executable .eg: System already have a category tree (c2)-[PARENT]->(c1) and need to add (c4)-[PARENT]->(c3)->[PARENT]->(c1) in to the category tree using cypher.
I can go with some similar approach mention in the documentation
http://neo4j.com/docs/stable/cypherdoc-linked-lists.html . but just want to check is there a simple way to solve this problem
Try this (without typo in third query)
MERGE (:Category {name:'c2'})-[:PARENT]->(c1:Category {name:'c1'})
MERGE (:Category {name:'c4'})-[:PARENT]->(:Category {name:'c3'})-[:PARENT]->(c1)
MERGE (:Category {name:'c5'})-[:PARENT]->(:Category {name:'c3'})
You can use single query to avoid duplicate entry
MERGE (:Category {name:'c4'})-[:PARENT]->(:Category {name:'c3'})-[:PARENT]->(:Category {name:'c1'})<-[:PARENT]-(:Category {name:'c2'})
MERGE (:Category {name:'c5'})-[:PARENT]->(:Category {name:'c5'})
I solve the problem by adding one more label called Root for the top level category .
Cypher query for first tree - (c2)-[PARENT]->(c1)
MERGE (nc1:Category:Root{name:'c1'})
MERGE (nc3:Category {name:'c2'})-[:PARENT]->(nc1)
Cypher query for second tree - (c4)-[PARENT]->(c3)->[PARENT]->(c1)
MERGE (nc1:Category:Root{name:'c1'})
MERGE (nc3:Category {name:'c3'})-[:PARENT]->(nc1)
MERGE (:Category {name:'c4'})-[:PARENT]->(nc3)
Cypher query for third tree - (c5)-[PARENT]->(c3)
MERGE (nc3:Category:Root{name:'c3'})
MERGE (nc5:Category {name:'c5'})-[:PARENT]->(nc3)

Resources