Search code examples
ruby-on-railsrubydatabasesearch-engineelasticsearch

ElasticSearch search on many types


I'm using Rails with the Tire gem (for ElasticSearch) and I need to search across multiple models. Something like:

# title is a field in all models
Tire.search :tasks, :projects, :posts, { :title => "word" } 

I know I can search models one by one and then handle these results, but that should be unecessary considering ElasticSearch(Lucene) is document oriented.

Any thoughts?

Thanks,


Solution

  • One possibility is to see them not as distinct models. A compound model could be that every document can be an item belonging to one or many differnt submodels identified by a string constant which can be multivalued.

    If you want to retrieve only results from one of those submodels you could add a fixed part to the query which identifies the set of documents belonging to this submodel.

    The only caveeat is that you need to have a primary key which is unique(which is not that bad because you can use something like an implicit document key).