我有一个在 Elastic Search 中索引的 mongodb 集合,我想返回按给定查询的 elastic search _score 排序的 MongoDB 文档。
我很困惑为什么下面的出版物正确地迭代了文档并更新了每个文档的(样本)分数:
Meteor.publish "docs", (params) ->
pub = @
Document.find({}, limit: 20).forEach (doc) ->
doc._score = 123 # some score
pub.added 'documents', doc._id, doc
pub.ready()
但下面使用 Elasticsearch 客户端不返回结果,事实上,迭代器甚至从未执行过:
Meteor.publish "docs", (params) ->
pub = @
client.search
size: 20
body:
query:
filtered:
query:
multi_match:
fields:['body']
query: query
.then (response) =>
results = response.body?.hits?.hits
ids = _.inject _.pluck(results, '_id'), (ids, id)->
ids.push({_id: id}); ids
, []
scores = getScores(results)
Document.find($or: ids).forEach (doc) ->
# Here is never reached.
doc._score = scores[doc._id]
pub.added 'documents', doc._id, doc
, (error) ->
console.trace error if error
pub.ready()
如果我的方法偏离这里,我不会感到惊讶。有没有更好的方法来解决这个问题?
最佳答案
我认为您只需等待调用 pub.ready()
直到“已发送记录集的初始、完整快照”。所以...在搜索成功回调结束时。
关于mongodb - 在 Meteor 中发布 Elasticsearch 结果的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23875228/