Ivar
Ivar

Reputation: 5732

With sunspot-rails, how do I conditionally index specific fields?

I would like to add a conditionally index some data inside the sunspot 'searchable' method in my model. Ideally it would look something like this:

searchable do
    string :important_text
    if address_visible?
      string :address
    end
end

In the above example, I would like to index the address field only if the address_visible? method (on the model) returns true. Unfortunately, the address_visible? method throws a 'NoMethodError' because the context is now a Sunspot::DSL::Fields, not the model.

Upvotes: 1

Views: 358

Answers (1)

polmiro
polmiro

Reputation: 1986

I don't think you can actually do exactly what you want. Nonetheless you could index a different value for address when address is not visible. For example:

searchable do
    string :important_text
    string :address { |model| model.address_visible? ? model.address : '' }
end

Upvotes: 2

Related Questions