如何通过时间戳return最新记录?

how to return the latest record by timestamp?

我有一个类似于此的数据集:

{"user":333,"product":943, "rating":2.025743791177902, "timestamp":1481675659}
{"user":333,"product":3074,"rating":2.1070657532324493,"timestamp":1481675178}
{"user":333,"product":3074,"rating":2.108323259636257, "timestamp":1481673546}
{"user":333,"product":943, "rating":2.0211849667268353,"timestamp":1481675178}
{"user":333,"product":943, "rating":2.041045323231024, "timestamp":1481673546}
{"user":333,"product":119, "rating":2.1832303461543163,"timestamp":1481675659}
{"user":333,"product":119, "rating":2.1937538029700203,"timestamp":1481673546}
{"user":111,"product":123, ...

我想查询一个用户的所有记录(例如333),但只有return最新的时间戳:

{"user":333,"product":119, "rating":2.1832303461543163,"timestamp":1481675659}     

map/reduce 索引是否可行?如果是,怎么做?

理想情况下我也想按评级值排序。

如果你像这样创建一个 Map 函数

function(doc) {
  emit([doc.user, doc.timestamp], null);
}

将按用户和时间顺序创建索引。

要return给定用户的最新(最新)时间戳,您可以使用以下参数查询索引:

startkey=[333,9999999999]
endkey=[333,0]
descending=true
limit=1

我们正在以相反的顺序查询索引,从该用户的最大时间戳开始,但将结果集大小限制为 1 - 最新条目。