简体   繁体   English

适用于HABTM关联模型的Tire / Elasticsearch

[英]Tire / Elasticsearch for HABTM associated models

I am trying to implement a full-text searching with tire/elasticsearch for my Client model. 我正在尝试使用我的客户模型的tire / elasticsearch进行全文搜索。 I have a set of users, working with clients. 我有一组与客户合作的用户。 There is a HABTM association between Client and User models. 客户模型和用户模型之间存在HABTM关联。

There is a manager function to scope only clients, managed by a particular user (in most cases it is the user currently logged in). 有一个manager功能,可以仅对由特定用户(大多数情况下是当前登录的用户)管理的客户端进行作用域管理。 I would like to implement a search filter in tire that would work much the same way as it works with ActiveRecord. 我想在轮胎中实现一个搜索过滤器,该过滤器的工作方式与ActiveRecord相同。 It does not work with tire (throwing the no filter registered for [user_ids] error). 它不适用于轮胎( no filter registered for [user_ids]错误而no filter registered for [user_ids]no filter registered for [user_ids] )。

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?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM