1
votes

I'm modelling a search term transition graph in a e-commerce software as a graph of nodes (terms) and edges (transitions). If a user types e.g. iphone in the search bar and then refines the query to iphone 6s this will be modeled as two nodes and a edge between those nodes. The same transition of terms of the different users will result in several edges between the nodes.

enter image description here

I'd now like to create an edge with a cumulated weight of 4 to represent that 4 users did this specific transition. How can I combine the results of a count(*) query with a create query to produce an edge with a property weight = 4

My count(*) query is:

MATCH (n:Term)-[r]->(n1:Term)
RETURN type(r), count(*)

I'd expect the combined query to look like this, but this kind of sql like composition seems not to be possible in cypher:

MATCH (n:Term), (n1:Term)
WHERE (n)-[tr:TRANSITION]->(n1)
CREATE (n)-[actr:ACC_TRANSITION {count: 
    MATCH (n:Term)-[r]->(n1:Term) RETURN 
    count(*)}
]->(n1)
RETURN n, n1

A non generic query to produce the accumulated transition that works is:

MATCH (n:Term), (n1:Term)
WHERE n.term = 'iphone' AND n1.term ='iphone 6s'
CREATE (n)-[actr:ACC_TRANSITION {count: 4}]->(n1)
RETURN n, n1

Any other ideas on how to approach and model this problem?

2
Use with in first query is count(*) and in second query create your relationmastisa
Can you please formulate the query? How would it look like with the with? Thanks!paweloque
Sorry I am busy at work and can't explain more my answer for now :)mastisa

2 Answers

3
votes

Use WITH like this:

MATCH (n:Term)-[r]->(n1:Term)
WITH n as n, count(*) as rel_count, n1
CREATE (n)-[:ACC_TRANSITION {count:rel_count}]->(n1)
RETURN n, n1
0
votes

If you match the nodes and relationship first and then use set, you will not produce duplicate nodes or relationships

Match (n:Term)-[r]->(n1.Term)
with n as nn,count(r) as rel_count,n1 as nn1
set r.ACC_TRANSITION=rel_count
return nn,nn1,r

The create function will create duplicates.