Reputation: 761
I'm trying to improve an already published query : neo4j improving cypher query performace
Again, I have items
graph db. each item
is connected to multiple properties
, which can be shared by multiple items
.
BUT , this time I want to search for items
starting with few property
nodes and finding the most connected items.
So, I have a set of properties pr=["pr1","pr2","pr3"]
and I want to find the most related items
i.e.
match (pr)-[r]-(item)
return item, count(r) as matching_properties
order by matching_properties
But, I want to find similar items too. There are SIMILAR
relationships between properties
, and I want to find:
MATCH (pr) -[:SIMILAR]->(pr2)-[r]-(item)
I tried retrieving all items by doing:
MATCH (pr) -[r*1..2]-(item)
The problem is that I want only the paths that got with a SIMILAR
relationship through a property
node, and unfortunately I have a lot of paths through some other nodes with other relationships.
So I can do Something like :
MATCH (pr) -[r*1..2]-(item)
where (length(r)= 1 or ANY (x in r where (type(x)="SIMILAR")))
but that's really not efficient because it's only a small portion of the paths. I really would want to do an optional matching similar to this :
MATCH (pr) -[r2?:SIMILAR]->(pr2?)-[r]-(item)
where pr2.type = "property"
Or, using an "OR" operand between the two path options (which I know that it doesn't exist..)
Is there any way to do that? Currently I'm using neo4j 1.9.2 but I am planning to move to 2.0. So an answer for 2.0 would be good for me too.
Upvotes: 2
Views: 1362
Reputation: 41676
Something like this?
Paths starting at 0 (*0..
) also include the zero length path to the start-node.
MATCH (pr:property)-[:SIMILAR*0..1]->(pr2:property)<-[:HAS_PROPERTY]-(item:item)
RETURN pr2, item
Upvotes: 4