The Right Way to Publish Search Results in Meteor

I have a mongodb collection indexed in Elastic Search and you want to return MongoDB documents sorted by their _score search query for this query.

I am confused by why the following publication correctly iterates over documents and updates the (sample) score for each document:

Meteor.publish "docs", (params) ->
  pub = @

  Document.find({}, limit: 20).forEach (doc) ->
    doc._score = 123 # some score
    pub.added 'documents', doc._id, doc

  pub.ready()

But the bottom use of an elastic search client does not return results, and in fact the iterator is never executed:

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()

I would not be surprised if my approach leaves here. Is there a better way to approach this?

+4
source share
2 answers

, , pub.ready(), ", ". ... .

Docs: http://docs.meteor.com/#publish_ready

+1

Source: https://habr.com/ru/post/1542072/


All Articles