ArangoDB-Community / ArangoBnB

16 stars 3 forks source link

AQL: Nearby Search #10

Open cw00dw0rd opened 3 years ago

cw00dw0rd commented 3 years ago

Nearby search to show available rentals near a specified address.

Simran-B commented 3 years ago

Find rooms within a 100 meter radius of a coordinate:

LET co = GEO_POINT(13.45477, 52.51171)
FOR doc IN arangobnb
  SEARCH ANALYZER(GEO_DISTANCE(doc.location, co) < 100, "geo")
  LET distance = GEO_DISTANCE(doc.location, co)
  // Note: Web UI map view is triggered by presence of `geometry` attribute!
  RETURN MERGE(KEEP(doc, "name", "_key"), {distance, geometry: location})

Search listing locations within a rectangle:

LET rect = GEO_POLYGON([ [13.4, 52.4], [13.5, 52.4], [13.5, 52.45], [13.4, 52.45], [13.4, 52.4] ])

LET results = (
  FOR doc IN arangobnb
    SEARCH ANALYZER(GEO_CONTAINS(rect, doc.location), "geo")
    RETURN doc.location
)

// Hack to return the shape within we search as well
FOR r IN PUSH(results, rect)
  RETURN r

Find private rooms within a rectangle that feature a hot tub, heating and Wi-Fi and cost between [30, 50] dollars:

LET rect = GEO_POLYGON([ [13.2, 52.6], [13.5, 52.6], [13.5, 52.45], [13.2, 52.45], [13.2, 52.6] ])

LET results = (
  FOR doc IN arangobnb
    SEARCH ANALYZER(GEO_CONTAINS(rect, doc.location), "geo") AND
           doc.room_type == "Private room" /* identity analyzer */ AND
           ANALYZER(TOKENS("tub heating wifi", "text_en") ALL IN doc.amenities, "text_en") AND
           IN_RANGE(doc.price, 30, 50, true, true)
  RETURN doc.location
)

FOR r IN PUSH(results, rect)
  RETURN r

Pick a random neighborhood and find all listings in that polygon:

LET rect = FIRST(FOR n IN neighborhoods SORT RAND() LIMIT 1 RETURN n.geometry)

LET results = (
  FOR doc IN arangobnb
    SEARCH ANALYZER(GEO_CONTAINS(rect, doc.location), "geo")
    RETURN doc.location
)

// Hack to return the shape within we search as well
FOR r IN PUSH(results, rect)
  RETURN r