I am trying to implement a full-text searching with tire/elasticsearch for my Client model. I have a set of users, working with clients. There is a HABTM association between Client and User models.
There is a manager
function to scope only clients, managed by a particular user (in most cases it is the user currently logged in). I would like to implement a search filter in tire that would work much the same way as it works with ActiveRecord. It does not work with tire (throwing the no filter registered for [user_ids]
error).
s.filter :user_ids, manager(params[:manager]).map(&:id)
if params[:manager].present?
I believe I might be doing something completely off the wall here, but what is wrong with the way I do that and what tutorial/reading would you recommend?
Here is the model.
class Client < ActiveRecord::Base
has_and_belongs_to_many :users, :uniq => true
## Tire would-be implementation
# include Tire::Model::Search
# include Tire::Model::Callbacks
# mapping do
# indexes :name
# indexes :comment
# indexes :user_ids
# end
def self.manager(user_id)
joins(:users).merge(User.current(user_id))
end
def self.index_search(params={})
## Tire would-be-implementation - does not work
# tire.search(load: true) do |s|
# s.query { string params[:search], default_operator: "AND" } if params[:search].present?
# s.filter :user_ids, manager(params[:manager]).map(&:id) if params[:manager].present?
# end
# ActiveRecord implementation - works
if params[:manager].present?
if params[:search].present?
@clients = manager(params[:manager]).where(['name LIKE ?', "%#{params[:search]}%"])
else
@clients = manager(params[:manager])
end
else
if params[:search].present?
@clients = where(['name LIKE ?', "%#{params[:search]}%"])
else
@clients = all
end
end
end
end
Try something like
t.filter :not , {:ids => { :values => self.manager(params[:manager]).map(&:id) }} if params[:manager].present?
or
t.filter :ids, :values => self.manager(params[:manager]).map(&:id) if params[:manager].present?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.