使用filter和getNearest命令进行rethinkdb [英] rethinkdb with filter and getNearest commands
问题描述
如何执行关于其他命令(例如过滤器命令)结果的getNearest查询?
How can execute getNearest query about the result of other command, for example a filter command?
var point = r.point(-122.422876,37.777128);
r.db('test').table('users').
filter({tags : 'tag'}).
getNearest(point, {index: 'geodata', maxResults: 30, unit :'km'})
我有一个用户"表:
[
{id: 1, tags : ['music', 'cups'], geodata: r.point()}
{id: 2, tags: ['music', 'play'], geodata: r.point()}
]
首先,我想按标签"字段进行过滤,然后返回最接近的值.
First I want to filter by 'tags' field and then return the nearest.
我指定的查询不正确,返回以下错误:" RqlRuntimeError:预期类型为TABLE但找到了SELECTION "
The query that I have specified is incorrect, return the follow error: "RqlRuntimeError: Expected type TABLE but found SELECTION"
推荐答案
The reason why your query throws an error is because .getNearest
only works with tables. That's what the documentation says.
选项1
您可以通过反转命令的顺序来解决问题:
You can solve your problem by just inverting the order of of commands:
var point = r.point(-122.422876,37.777128);
r.db('test')
.table('users')
.getNearest(point, {index: 'geodata', maxResults: 30, unit :'km'})
.filter({tags : 'tag'})
之所以可行,是因为.getNearest
返回一个数组,而.filter
可以在一个数组上工作.
This works because .getNearest
returns an array and .filter
can work on an array.
var point = r.point(-122.422876,37.777128);
r.db('test')
.table('users')
.getNearest(point, {index: 'geodata', unit :'km'})
.filter({tags : 'tag'})
.limit(30)
此方法的问题在于,为了保证所有可能的结果都存在,您不能将maxResults
选项传递给getNearest,这意味着将查询并过滤每一行.
The problem this approach is that, in order to guarantee that all possible results are there, you can't pass a maxResults
option to getNearest, which means every single row will be queried and then filtered.
这种情况最适合大多数物品通过过滤器的情况.
This scenario works best for when most items will pass through the filter.
根据您的数据,还可以编写一个递增地递增maxResults
的函数,直到获得所需的所有结果为止,但这可能很复杂而且不太优雅.
Depending on your data, it's also possible to write a function that increases maxResults
incrementally until it gets all the results it needs, but this might be complex and not very elegant.
选项2
如果过滤器要过滤掉大部分结果,则可以使用 getAll
(创建tags
索引),并先getAll
过滤结果,然后根据距离对结果进行排序:
If the filter is going to filter out most of the results, then you can filter the results using getAll
(creating a tags
index) and have getAll
filter the results first and then order the results based on distance:
var point = r.point(-122.422876,37.777128);
r.table('users')
.getAll('tag', {index: 'tags'})
.orderBy(r.row('geodata')
.distance(point))
.limit(30)
数据类型
使用 .typeOf
方法.
You can always get the type of a anything at any point by using the .typeOf
method.
示例:
r.db('test')
.table('users').typeOf() // "TABLE"
r.db('test')
.table('users')
.filter({tags : 'tag'}).typeOf() // "STREAM"
如果您想了解有关ReQL数据类型的更多信息,可以查看此博客文章我写的文章解释了ReQL数据类型及其不同之处.
If you want to know more about ReQL data types, you can check out this blog post I wrote which explains ReQL data types and how they are different.
这篇关于使用filter和getNearest命令进行rethinkdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!