I want to get all the distinct values of a particular node property, and map each one to a unique integer.
MATCH (n) RETURN DISTINCT n.property
But how do I continue to use the RETURN
ed list in the query?
I want to get all the distinct values of a particular node property, and map each one to a unique integer.
MATCH (n) RETURN DISTINCT n.property
But how do I continue to use the RETURN
ed list in the query?
The thing you're looking for is the WITH clause, which is like a RETURN but lets you continue operating on the results. This is where you would use aggregations, DISTINCT, additional filtering, and other stuff, and it also defines which variables remain in scope for later in the query: any variables you do not include in the WITH clause are dropped out of scope.
Edit: I got it, thanks:
MATCH (n)
WITH DISTINCT n.property AS properties
...
I played with WITH
a little bit but could not get to work with DISTINCT
. Can you provide an example of using both those keywords?
The snippet you provided looks correct, what errors and/or unexpected behavior are you encountering? There may be a misassumption about what you think this is doing, so adding your understanding about what this is doing may help us figure that out.
Also if possible you may want to have a label present in the MATCH pattern, otherwise it will be looking to get distinct values for the property
property for all nodes in your graph.