0
votes

I have graph with different types of nodes and edges. I would like to get all paths between two nodes (index WORD) which are connected by edges (relation NEXT_WORD) with property edgeLevel=1. I don't want to get connections where "edgeLevel != 1". I wrote a query

MATCH p=(a:WORD{wholeWord:"are"})-[r:NEXT_WORD*1..3{edgeLevel:1}]->(b:WORD{wholeWord: "you"}) RETURN length(p);

but it is very heavy. I'm trying to figure out how to optimize this cypher query, but I have no idea. Is there any quicker and less heavy way to do this? This query returned 32 rows in 7931 ms.

Cypher PROFILE query

1

1 Answers

1
votes

You appear to already have an index on :WORD(wholeWord). However, your query is not using the index to find both a and b. This query should be faster:

MATCH p=(a:WORD { wholeWord:"are" })-[r:NEXT_WORD*..3 {edgeLevel:1}]->(b:WORD { wholeWord: "you" })
USING INDEX a:WORD(wholeWord)
USING INDEX b:WORD(wholeWord)
RETURN length(p);

But your query can be even faster if you do not require a specific property value for your relationship (because of fewer DB hits), especially since there is no way to create an index for relationships. So, if you could use a [:NEXT_WORD_1] relationship instead of a [:NEXT_WORD {edgeLevel:1}] relationship, this would be the fastest:

MATCH p=(a:WORD { wholeWord:"are" })-[r:NEXT_WORD_1*..3]->(b:WORD { wholeWord: "you" })
USING INDEX a:WORD(wholeWord)
USING INDEX b:WORD(wholeWord)
RETURN length(p);