0
votes

While using neo4j, I encountered a problem in an use case where i need to fetch data using cypher order by number of relation from input set of nodes.

I am very new to neo4j and regrets if the problem is too naive.

Demo graph:

enter image description here

What i am trying to do is, for an input string say "java developer engineer" return all nodes, connected to node java, developer and engineer but give highest priority to node connected to all three of them then low priority to nodes connected to 2 and lowest to nodes connected with only 1 of them.

I have written a basic query:

match(n:Token{name:"java"})-[res]->(y)
match(n1:Token{name:"developer"})-[res1]->(y1)
match(x:Token{name:"engineer"})-[res2]->(y2)
return n,n1,x,res,res1,res2,y,y1,y2

Issues i am facing is with prioritizing nodes connected with all 3 input nodes, handling case if any incorrect input token present (say java, developer, engineer and tesla, where tesla is not a token).

Thanks

1

1 Answers

0
votes

The following query assumes it is passed an input parameter containing the input string, and returns, for each distinct term:

  • the term itself
  • a collection of paths to that term from relevant Token nodes,
  • the number of paths found for the term

The results are also returned in descending order by the number of paths found.

UNWIND SPLIT($input, ' ') AS name
MATCH p=(n:Token {name: name})-[:RELATED_TO]->(term)
WITH term, COLLECT(p) AS paths
ORDER BY SIZE(paths) DESC
RETURN term, paths, SIZE(paths) AS count