Home how to use searchkick to index according to some conditions
Reply: 1

how to use searchkick to index according to some conditions

makeapp
1#
makeapp Published in 2017-11-30 00:45:42Z

I am using searchkick and rails4.

I have an activerecord People, with attributes a,b,c. How can I do indexing only when b equals "type1", not indexing otherwise?

Currently what I know is

def search_data
  {
    a:a,
    b:b,
    c:c,
  }
end
BigRon
2#
BigRon Reply to 2017-12-01 16:46:57Z

Per the docs:

By default, all records are indexed. To control which records are indexed, use the should_index? method together with the search_import scope.

This should work for your case:

class People < ApplicationRecord
  searchkick # you probably already have this
  scope :search_import, -> { where(b: "type1") }

  def should_index?
    self.search_import # only index records per your `search_import` scope above
  end

  def search_data # looks like you already have this, too
    {
      a:a,
      b:b,
      c:c,
    }
  end
end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.386519 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO