Search code examples
databaseapirethinkdbrethinkdb-ruby

Is there a way to query the closest locations on a filter in RethinkDB?


Currently, the Rethink API documentation says that the get_nearest command only works on a table. Ofcourse I can filter the results afterwards, but that seems inefficient, plus that requires to have all the items sorted by distance when I want to limit the result to a specific number of items.

Is there a way I'm overlooking to get the closest results from a filtered list in one query?


Solution

  • The reason it only works on a table is because of mandatory index. Index only works on table level. Thinking of it a bit, that makes sense because it is an expensive query.

    However, if you have a filtered list, the best you can do is to use distance and order by its result.

    Something like this will work:

    r.db('db').table('table')
      .filter(function_to_filter)
      .orderBy(function(doc) {
        return r.distance('your_point_to_compare', doc('point'))
      })