简体   繁体   中英

Full text search in embedded one to many, MongoID child docs

I have a MongoID User model

class User
  include Mongoid::Document
  field :name

  embeds_many :watchlists
end

which embedded one to many

class Watchlist
  include Mongoid::Document
  field :description

  embedded_in :user
end

I want to search full text into :description field which is in Watchlist child embedded in User. The most heavy search goes through 1.5k descriptions, with a max of 30, 40 words each.

The constrain here, is that I'll deploy on Heroku and they have not free plan for indexing currently.

Then I've tried with mongoid_fulltext (as well as mongoid_search and mongoid-searchable ) but without success.

Does anybody have an idea how to do that ?

UPDATE:

This is an example of mongoid_fulltext . The User model embeds many Watchlist(s). I'm searcing a string in :description field, which is in Watchlist child doc :

class Watchlist
  include Mongoid::Document

  field :description
  ...

  embedded_in :user
end

Watchlist is embedded in User :

class User
  include Mongoid::Document
  include Mongoid::FullTextSearch

  field :name
  ...

  embeds_many :watchlists

  def search_in_description
    self.watchlists.map{ |w| w.description }.join(' ')
  end
  fulltext_search_in :search_in_description
end

... but in that way, running User.fulltext_search("a presentation framework based on the power of CSS3") gives me back just the parent doc causing the match (a user instance) and not the watchlists doc ( the child instance ).

See the output: http://pastie.org/3226179

How can I get just the matching "watchlists"? ( I tryed few ways without success )

In MongoDB you cannot query directly for embedded documents. You can query they way you explained in the example to get the parent document, then query on the returned top document user for the desired embedded document among current user watchlists.

If you're going to do that frequently, perhaps you might want to consider using relational has_many rather than embeds_many.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM