Search code examples
graph-databasesarangodbaql

How to query for a node that is connected to multiple nodes in Arango DB graph


I have a graph containing nodes of two types: Attractions and Hotels.

What I want to do is that query for Hotels that are surrounded by three Attractions. The query I have now (below) gives Hotels that are connected to at least one Attraction.

FOR document IN Attraction
FOR vertex, edge, path IN 1..2 OUTBOUND document GRAPH "LondonAttractionDB" 
FILTER path.vertices[0].entityTypes[0] == "Attraction" OR path.vertices[0].entityTypes[0] == "Attraction" OR path.vertices[0].entityTypes[0] == "Attraction"
FILTER path.vertices[1].entityTypes[0] == "Hotel"
FOR prop4 IN path.edges[0].properties  
FILTER prop4.name == "name" AND prop4.value == "Food_and_beverage_location" 
OR prop4.name == "name" AND prop4.value == "Food_and_beverage_location" 
OR prop4.name == "name" AND prop4.value == "Accommodation_location" 
RETURN DISTINCT path

This gives the following result. (Orange - Hotel, Green - Attraction)

enter image description here

How do I get the result shown within circles? (Hotels connected to exactly three attractions)

Any help is much appreciated.


Solution

  • It's difficult to answer without details about the dataset (document structure and collections), but based on your example query and description, I would use a different approach:

    • Go over all documents (attractions and hotels seem to reside in the same collection)
    • Filter by type Hotel
    • For every hotel node, get neighbors with incoming edges
    • Filter these nodes by type Attraction
    • Count how many nodes there are for the hotel
    • Return hotels with exactly three connected attractions
    FOR hotel IN Attraction
      FILTER doc.entityTypes[0] == "Hotel"
      LET attractions = (
          FOR vertex IN 1..1 INBOUND hotel GRAPH "LondonAttractionDB"
          FILTER vertex.entityTypes[0] == "Attraction"
          RETURN 1
      )
      FILTER LENGTH(attractions) == 3
      RETURN hotel