meteorhacks / search-source

Reactive Data Source for Search
MIT License
145 stars 33 forks source link

FR: Merge search results with local collection #19

Open lorensr opened 9 years ago

lorensr commented 9 years ago

This would be a great feature:

If my ES type, for example items, is a mirror of a Mongo collection Items, it would be great if SearchSource could add the search results to the local client Items._collection so that I can do Items.find x and get results that were either published OR found through an ES search.

arunoda commented 9 years ago

This is already there. You can define the source in the client too. see: https://github.com/meteorhacks/search-source#defining-data-source-in-the-client

lorensr commented 9 years ago

I'm sorry, I don't think that's what I'm talking about? If it is, I don't understand how to use it in this way. What I'm suggesting is this:

both:

Items = new Meteor.Collection('items')

server:

SearchSource.defineSource 'search-items', (search, options) ->
  query = 
    match: 
      name: 
        query: search
        fuzziness: 'AUTO'
        operator: 'and'

  result =  EsClient.search
    index: 'myapp'
    type: 'items'
    body: {query}

  data = result.hits.hits.map (doc) ->
    source = _.clone doc._source
    source._score = doc._score
    source

  {data}

Meteor.publish 'items', ->
  Items.find name: 'Dave'

client:

Meteor.subscribe 'items'

ItemSearch = new SearchSource 'search-items', fields, options
ItemSearch.search 'Bob'
ItemSearch.search 'Alice'
ItemSearch.search 'Eve'

and then instead of ItemSearch.getData, I do Items.findOne({name:'Alice'}). And I can also do Items.findOne({name:'Dave'}).

It would need some way to tell SearchSource to merge with Items._collection. Maybe add an option:

ItemSearch = new SearchSource 'search-items', fields, {mergeWith: Items}
lorensr commented 9 years ago

I've edited my last comment to also show the publish + subscribe.

lorensr commented 9 years ago

Let me know if part of that doesn't make sense :)

raisudeen commented 8 years ago

@lorensr @arunoda Me too facing the same issue. Im having a set of "items" in search result. But updating an item is not getting reflected in the search result. Do i need to do anything in "Meteor.publish". Please guide me to make it workable.