2017-03-08 62 views
1

假設我有一個RethinkDB表,其中有typetimestamp字段。 type可以是"good""bad"。我想寫一個RethinkDB查詢,獲取最近的"good"文檔的timestamp,同時使用compound indextypetimestamp如何使用RethinkDB的複合索引的最小/最大函數

這裏是用一種溶液的示例腳本:

import faker 
import rethinkdb as r 
import dateutil.parser 
import dateutil.tz 

fake = faker.Faker() 
fake.seed(0)   # Seed the Faker() for reproducible results 

conn = r.connect('localhost', 28016) # The RethinkDB server needs to have been launched with 'rethinkdb --port-offset 1' at the command line 

# Create and clear a table 
table_name = 'foo' # Arbitrary table name 
if table_name not in r.table_list().run(conn): 
    r.table_create(table_name).run(conn) 
r.table(table_name).delete().run(conn)  # Start on a clean slate 

# Create fake data and insert it into the table 
N = 5  # Half the number of fake documents 
good_documents = [{'type':'good', 'timestamp': dateutil.parser.parse(fake.time()).replace(tzinfo=dateutil.tz.tzutc())} for _ in range(N)] 
bad_documents = [{'type':'bad', 'timestamp': dateutil.parser.parse(fake.time()).replace(tzinfo=dateutil.tz.tzutc())} for _ in range(N)] 
documents = good_documents + bad_documents 
r.table(table_name).insert(documents).run(conn) 

# Create compound index with 'type' and 'timestamp' fields 
if 'type_timestamp' not in r.table(table_name).index_list().run(conn): 
    r.table(table_name).index_create("type_timestamp", [r.row["type"], r.row["timestamp"]]).run(conn) 
    r.table(table_name).index_wait("type_timestamp").run(conn) 

# Get the latest 'good' timestamp in Python 
good_documents = [doc for doc in documents if doc['type'] == "good"] 
latest_good_timestamp_Python = max(good_documents, key=lambda doc: doc['timestamp'])['timestamp'] 

# Get the latest 'good' timestamp in RethinkDB 
cursor = r.table(table_name).between(["good", r.minval], ["good", r.maxval], index="type_timestamp").order_by(index=r.desc("type_timestamp")).limit(1).run(conn) 
document = next(cursor) 
latest_good_timestamp_RethinkDB = document['timestamp'] 

# Assert that the Python and RethinkDB 'queries' return the same thing 
assert latest_good_timestamp_Python == latest_good_timestamp_RethinkDB 

之前運行此腳本,我使用命令

rethinkdb --port-offset 1 

我也使用faker包以產生髮射RethinkDB在端口28016假數據。

,我使用的查詢,它結合了betweenorder_bylimit,似乎並不特別優雅或簡潔,我想知道是否可以使用max用於此目的。但是,從文檔(https://www.rethinkdb.com/api/python/max/)中我不清楚如何執行此操作。有任何想法嗎?

回答

1

理想情況下,你可以取代你的查詢這一部分:

.order_by(index=r.desc("type_timestamp")).limit(1) 

有了:

.max(index="type_timestamp") 

當然,這是目前不可能。見https://github.com/rethinkdb/rethinkdb/issues/5141

+0

不幸的是這一點讓'rethinkdb.errors.ReqlQueryLogicError:預期類型表卻發現TABLE_SLICE:([ '好',R之間 r.table( '富'): 選擇的表(富)。 .minval],['good',r.maxval],index ='type_timestamp').max(index ='type_timestamp')'。 –

+0

我的建議確實無效。我已經更新了答案 – AtnNn

相關問題