我正試圖動態地生成rethinkdb查詢服務器端。的最終目標是具有被構造大致像這樣(在JavaScript)查詢:動態地生成rethinkdb查詢 - python
r.db('test').table('data')
.filter(function (row) {
return row('subgroup').eq('x').or(row('subgroup').eq('y'))
.and(
.row('metric_id').eq('a')
.or(row('metric_id).eq('b')
.or(row('metric_id').eq('c'))
})
我需要在一組鍵的通過,每個與一組可接受值。對於上面的例子,我在
{
'subgroup': ['x', 'y'],
'metric_id': ['a', 'b', 'c']
}
查詢傳遞應該返回所有的記錄進行subgroup
是x
或y
和metric_id
是a
或b
或c
。
我正在努力以正確的方式來做到這一點。見下面一個嘗試:
def parse_filters(cls, filters):
def rethink_filter(data):
result = r.expr(True) # initialize filter function
for key in filters:
or_statements = []
for value in filters[key]:
f = {}
f[key] = value
or_statements.append(r.expr(f)) # build a list of
result = result.and_(r.or_(r.expr(or_statements)))
if not result:
break
return result
return rethink_filter
隨着
{
'entity_id': ['a', 'b'],
'metric_id': ['x']
}
rethink_filter
輸入提供了查詢:
r.and_(r.and_(True, r.or_([{'entity_id': 'a'}, {'entity_id': 'b'}])), r.or_([{'metric_id': 'x'}]))
它看起來像它應該給出結果我之後,但它將返回表中的所有項目,而不管entity_id
或metric_id
。
我哪裏錯了?
對於什麼是值得的,雖然RethinkDB可以做這些查詢,但可能難以構建/維護它們,並且難以針對索引級性能優化它們。因此,將Elasticsearch等技術與數據庫集成可能更適合這種搜索用例。 –