1

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,

2
  • Hi, could you clarify what is the use-case? The ActiveModel integration exposes the search method per one model, to be sure. However, with the Tire.search method — as in your example — you can indeed search in more than one index. The thing is, the records are then wrapped in a Tire::Results::Item class, not in you model class. But this is the behaviour I'm gravitating to anyway — wrap results in this fre-form class, and then provide a way to load the "real model". See issues github.com/karmi/tire/issues/11 and github.com/karmi/tire/issues/12.
    – karmi
    Jul 4, 2011 at 19:02
  • 1
    @karmi, I tried Tire.search {...} but it didn't had support, at least on version 0.1.13, which is the one we are currently using. I'll update my gem version ASAP. Anyway, searching in the same index while using type for filtering (if needed) is working pretty good in our case. Thanks for your response. Jul 25, 2011 at 22:46

1 Answer 1

1

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

1
  • 1
    Seems a good approach, even because we have some fields with unstandardized names. Thanks. Jul 4, 2011 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.