0

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?

Fritzz
  • 656
  • 6
  • 27

1 Answers1

1

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'))
  })
kureikain
  • 2,304
  • 2
  • 14
  • 9
  • Cool, looks good. I'll try it out and mark it as the answer when this works. Do you expect this performs better or worse then first ordering by distance and then filtering? – Fritzz Sep 24 '15 at 12:12
  • It depends on your filter function and size of data so it is hard to say which one is better. But usually, filter first, then orderBy later is better. If you want the best of performance, try to turn filter function into an index if possible and use `getAll` on that index. If you can post the content of filter function, I will try to turn it into an index if possible for using with `getAll` – kureikain Sep 24 '15 at 16:51