gorrch
gorrch

Reputation: 551

neo4j query conversion to use in function

I have such query, which normally accepts one argument in {} brackets.

MATCH (pr:Person)-[:person_successor]->(next:Person)
  WHERE (:Queue {name: 'A'}) -[:queue_person]-> (pr)
  RETURN pr.name, next.name

I want to pass as Queue name several possible arguments instead of one, e.g. {name: 'A' or name: 'B'}. So I want to have people and their successors from many queues. Queues are also connected one to another. But such query is not possible and throws exception:

MATCH (pr:Person)-[:person_successor]->(next:Person)
  WHERE (:Queue {name: 'A' or name: 'B'}) -[:queue_person]-> (pr)
  RETURN pr.name, next.name

Instead of above, works sth like this:

MATCH (q:Queue)-[:queue_person]->(pr:Person)-[:person_successor]->(:next:Person)
  WHERE q.name in ['A','B']
  RETURN pr.name, next.name

But I would like to use (pr:Person)-[:person_successor]->(next:Person) instead of (q:Queue)-[:queue_person]->(pr:Person)-[:person_successor]->(:next:Person) because somehow it spoil person order so as a result I do not have:

A B

B C

C D

D E ...

It is disturbed somehow because of queue in match clause.

Is it possible to pass arguments, in second line, in where clause and keep also there relation between queue and person -[:queue_person]-> (pr) like in below not working snippet query?

WHERE (:Queue {name in ['A','B']) -[:queue_person]-> (pr)

Upvotes: 0

Views: 61

Answers (2)

A Sarshar
A Sarshar

Reputation: 294

I think the problem of this query in your question

MATCH (q:Queue)-[:queue_person]->(pr:Person)-[:person_successor]->(:next:Person)
WHERE q.name in ['A','B']
RETURN pr.name, next.name

is in this : (:next:Person). and you just need to change to (next:Person)

But if it is not , you can use UNWIND :

      UNWIND ["A", "B"] AS name
      MATCH (pr:Person)-[:person_successor]->(next:Person)
      WHERE (:Queue {name: name}) -[:queue_person]-> (pr)
      RETURN pr.name, next.name

Upvotes: 1

Luanne
Luanne

Reputation: 19373

I'm not clear how the order is different with

MATCH (q:Queue)-[:queue_person]->(pr:Person)-[:person_successor]->(:next:Person)
WHERE q.name in ['A','B']
RETURN pr.name, next.name

It is hard to tell from the response you shared.

Another way to write it would be

MATCH (q:Queue)
WHERE q.name in ['A','B']
WITH q
MATCH (pr:Person)-[:person_successor]->(next:Person)
WHERE (q)-[:queue_person]->(pr)
RETURN pr.name, next.name

Upvotes: 0

Related Questions