This website uses cookies. By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results forΒ

**Head's Up!** Site migration is underway.
Phase 2: migrate recent content

- Neo4j
- Community Corner
- General Discussions
- More efficient way without cartesian product?

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β11-09-2022 12:57 PM

Hello Community,

This does a proper job of building a relationship between two airports where I then indicate the distance between the airports in both miles and kilometers. The query build a cartesian product - is there a more efficient slash elegant way of performing this? I have tested this with a small subset of airports (8 airports, 64 relationships), but fear this will not scale to 10's of thousand of airports.

MATCH (a:Airport), (b:Airport)

WHERE

a.iata_code <> b.iata_code

WITH

a, b,

point({latitude:a.latitude_deg, longitude:a.longitude_deg, height: a.elevation_ft / 3.281}) AS p_a,

point({latitude:b.latitude_deg, longitude:b.longitude_deg, height: b.elevation_ft / 3.281}) AS p_b

MERGE

(a)-[:DISTANCE_BETWEEN {distance_miles: round(point.distance(p_a, p_b) / 1609, 1),

distance_kilometers: round(point.distance(p_a, p_b) / 1000, 1)}]-(b)

Thanks much all,

Douglas

Solved! Go to Solution.

1 ACCEPTED SOLUTION

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β11-09-2022 01:14 PM

Hello @DouglasWiley π

Here is a query where it reduce the number of operations but you still have a Cartesian product:

```
MATCH (a:Airport)
MATCH (b:Airport)
WHERE id(a) > id(b)
WITH
a, b,
point({latitude:a.latitude_deg, longitude:a.longitude_deg, height: a.elevation_ft / 3.281}) AS p_a,
point({latitude:b.latitude_deg, longitude:b.longitude_deg, height: b.elevation_ft / 3.281}) AS p_b
MERGE (a)-[r:DISTANCE_BETWEEN]-(b)
SET r += {
distance_miles: round(point.distance(p_a, p_b) / 1609, 1),
distance_kilometers: round(point.distance(p_a, p_b) / 1000, 1)
}
```

Regards,

Cobra

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β11-09-2022 01:14 PM

Hello @DouglasWiley π

Here is a query where it reduce the number of operations but you still have a Cartesian product:

```
MATCH (a:Airport)
MATCH (b:Airport)
WHERE id(a) > id(b)
WITH
a, b,
point({latitude:a.latitude_deg, longitude:a.longitude_deg, height: a.elevation_ft / 3.281}) AS p_a,
point({latitude:b.latitude_deg, longitude:b.longitude_deg, height: b.elevation_ft / 3.281}) AS p_b
MERGE (a)-[r:DISTANCE_BETWEEN]-(b)
SET r += {
distance_miles: round(point.distance(p_a, p_b) / 1609, 1),
distance_kilometers: round(point.distance(p_a, p_b) / 1000, 1)
}
```

Regards,

Cobra

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β11-10-2022 06:45 AM

Thanks so much @Cobra! This is excellent. What is the rational with "WHERE id(a) > id(b)"?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

β11-12-2022 03:10 AM

Nodes 2022

All the sessions of the conference are now available online

Related Content

- Cypher Sleuthing: Dealing with Dates Part 4 in General Discussions
- Will It Graph? Identifying A Good Fit For Graph Databases β Part 2 in General Discussions
- Overview of the Neo4j Graph Data Platform in General Discussions
- This tutorial should explain how to avoid Cartesian Products in Community Suggestions