Search code examples
ruby-on-railssearchkick

How to exclude records in searchkick?


I need to exclude records from searchkick when it has less than 3 tags. Right now, my search_data function is as follow:

def search_data
  {
    ...
    tag_ids: tags.map(&:id)
  }
end

Can I do this?

def search_data
  return nil if tags.count < 3
  {
    ...
    tag_ids: tags.map(&:id)
  }
end

Or can I add some condition when I perform lookup(), so it only search records that have tag_ids array with 3 or more id? (Other than add a tag_count field and reindex everything?)

Thanks!


Solution

  • Searchkick provides the ability to define a should_index? method that does exactly what you're looking for. Check it out here!