2

I am doing the following Cypher query: MATCH (a) WHERE a.itemId IN ['Q2', 'Q24', 'Q30', 'Q23', 'Q16', 'Q20'] RETURN a

I'd like it to return the same results as when I set the Auto-Complete switch. This question was asked and answered 1 but I don't understand the answer. Can this be performed in one query, and if so, what would the modified query be?

Community
  • 1
  • 1
JavaFXpert
  • 23
  • 2

2 Answers2

1

Yeah, you should be able to do it all in one query. To get the nodes in question along with their relationships you can do:

MATCH (a)
WHERE a.itemId IN ['Q2', 'Q24', 'Q30', 'Q23', 'Q16', 'Q20']
OPTIONAL MATCH (a)-[rel]-()
RETURN a, collect(rel)

That will return you, for each node, an array of the relationships.

Depending on your library that you're using for Neo4j, that may or may not return you something useful to give you the startnode/endnode. You could also do:

MATCH (a)
WHERE a.itemId IN ['Q2', 'Q24', 'Q30', 'Q23', 'Q16', 'Q20']
OPTIONAL MATCH (a)-[rel]-()
RETURN a, collect({rel: rel, start_node: startNode(rel), end_node: endNode(rel)})

That would give you an array for each node containing objects/maps of the data.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • Thanks for your response, Brian. Each of those queries returned over 5000 nodes. My original query when auto-complete is on only returns the 6 nodes specified and 8 relationships, which is what I'd like to replicate in a Cypher query. – JavaFXpert Jan 05 '16 at 06:49
  • Oh, my bad! I just edited to switch the `OPTIONAL MATCH` and the `WHERE` (the `WHERE` is filtering `a` and thus should go directly after that `MATCH` clause). Try that. – Brian Underwood Jan 05 '16 at 06:52
  • Thanks Brian. Those queries return 300+ nodes, but 6 is what is required (same as with auto-complete). – JavaFXpert Jan 05 '16 at 07:03
  • That's strange... Do you mean it returns 300 rows? If so, are the nodes being returned in the `a` column duplicated on different rows? – Brian Underwood Jan 05 '16 at 07:12
  • The Neo4j browser Graph view is showing 393 nodes. Switching to Rows view, I correctly see 6 Nodes. Thanks for your help! – JavaFXpert Jan 05 '16 at 07:18
0

Got some separate clarification on the question about how to only return the relationships between the nodes that are matched. This should be possible like so:

MATCH (a)
WHERE a.itemId IN {itemIds}
OPTIONAL MATCH (a)-[rel]-(b)
WHERE b.itemId IN {itemIds}
RETURN a, collect(rel)

Again, you also might want to return the startNode/endNode of the relationships

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34