Trying to create Cartesian product

Please keep the following things in mind:

I have this input WITH [
{tagKey: "x", values: ["1", "2", "3", "4"]},
{tagKey: "y", values: ["5", "6", "7", "8", "9"]}
] AS tagData
need the Cartesian product [[1,5],[1,6],[1,7]....]
what is best approach

Try this:

WITH [
{tagKey: "x", values: ["1", "2", "3", "4"]},
{tagKey: "y", values: ["5", "6", "7", "8", "9"]}
] AS tagData
UNWIND tagData[0].values as x_value
UNWIND tagData[1].values as y_value
RETURN x_value, y_value
1 Like

Thanks for the response. I wanted it in general format. It was an example with two values, but my use case has more than two tag keys. I tried using Unwind, but I can't get it to work.

Thanks for the response. I wanted it in general format. It was an example with two values, but my use case has more than two tag keys. I tried using Unwind, but I can't get it to work.

WITH [

{tagKey: "x", values: ["1", "2", "3", "4"]},

{tagKey: "y", values: ["5", "6", "7", "8", "9"]}

] AS tagData

// Start with an empty list of combinations

WITH tagData, [] AS combinations

// Iterate over each tagKey entry

UNWIND tagData AS tagEntry

WITH tagEntry, combinations

// Unwind the values for the current tagKey

UNWIND tagEntry.values AS value

WITH tagEntry, value, combinations

// Use UNWIND to expand each existing combination with the current value

UNWIND combinations AS combination

WITH combination + [{tagKey: tagEntry.tagKey, value: value}] AS newCombination

// Collect all new combinations

WITH collect(newCombination) AS combinations

// Return each combination

UNWIND combinations AS combination

RETURN combination , i tried this
β”‚combination β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•‘
β”‚[{tagKey: "x", value: "1"}]β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚[{tagKey: "x", value: "2"}]β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

Unwinding tagData followed by unwinding values will not work. You should just get a sequence of rows, with one value on each row. The Cartesian product works when the two arrays are on the same line and you unwind each sequentially.

The problem with generalizing it is you need to execute an unwind for each array in the initial data. I could write a general solution if you could define the maximum number of array elements to unwind.

This seems like an academic problem. Do you have an actual scenario you want to solve?

this is a actual scenario , is there any way we can meet on a zoom to explain the issue that i am trying to solve. it wont take more than 5 mins to show .

Sure. Tomorrow or Monday is fine. Glilienfield@yahoo.com

Please let me know what time works for you so I will set it up for tomorrow.

How about tomorrow at 11am?

sure, will schedule it

I tried this code:

with [1,2,3,4] as n, ['a','b','c','d'] as a, as c

unwind n as i

unwind a as j

with c+[i,j] as nc

return nc

and the result was:
nc
1 [1, "a"]
2 [1, "b"]
3 [1, "c"]
4 [1, "d"]
5 [2, "a"]
6 [2, "b"]
7 [2, "c"]
8 [2, "d"]
9 [3, "a"]
10 [3, "b"]
11 [3, "c"]
12 [3, "d"]
13 [4, "a"]
14 [4, "b"]
15 [4, "c"]
16 [4, "d"]

sorry this one:
with [1,2,3,4] as n, ['a','b','c','d'] as a

unwind n as i

unwind a as j

with [i,j] as nc

return collect(nc) as rsult

and the result was:
[[1, "a"], [1, "b"], [1, "c"], [1, "d"], [2, "a"], [2, "b"], [2, "c"], [2, "d"], [3, "a"], [3, "b"], [3, "c"], [3, "d"], [4, "a"], [4, "b"], [4, "c"], [4, "d"]]

Gary Helped me write a custom plugin and solved the issue

1 Like