Question

I have a Cypher Query, currently the data fetch by depth-first-search(DFS, Neo4j default algorithm)

MATCH p1=(a)--(b)
WHERE a.profileId = '203'
OPTIONAL MATCH p2=(a)--(b)--(c) 
WHERE a.profileId = '203'
OPTIONAL MATCH p3=(a)--(b)--(c)--(d) 
WHERE a.profileId = '203'
RETURN DISTINCT a,b,c,d 
LIMIT 500

I have tried to use SORT(Length(p3)), where query is running very very slow.

Is it possible to call this query through REST API by using breadth-first-search(BFS)?

Was it helpful?

Solution

Why do you re-match things time and again? So you generate a multitude of paths that are duplicate. You should also use labels + and index or constraint for :User(profileId) so that your lookup of a is fast enough. And you probably want to specify a relationship type and direction.

create index on :User(profileId);

MATCH (a:User)--(b)
WHERE a.profileId = '203'
OPTIONAL MATCH (b)--(c)--(d) 
RETURN distinct a,b,c,d limit 500

Optional match is like an outer join

OTHER TIPS

Thanks for the answer and teaching of Michael. :)

Actually my understanding of Cypher has some error, I was trying to write Cypher query as SQL.

In fact that is a wrong.

If we using a path in the match, like (a)--(b), no need to refer it again in the clause i.e. an optional match. Just directly use (b)--(c) is enough. (a)--(b)--(c) will returns duplicated results.

Here is my current query:

MATCH (a:Profile)-[r1]-(b)
WHERE a.profileId = 'twitter_20357508'
and r1.dateTimePostedMil >  1388534400000  
and r1.dateTimePostedMil <  1417392000000
OPTIONAL MATCH (b)-[r2]-(c)
WHERE r2.dateTimePostedMil >  1388534400000  
and r2.dateTimePostedMil <  1417392000000
OPTIONAL MATCH (c)-[r3]-(d)
WHERE r3.dateTimePostedMil >  1388534400000  
and r3.dateTimePostedMil <  1417392000000
RETURN distinct a,r1,b,r2,c,r3,d limit 100
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top