【问题标题】:Filter issue with tire elasticsearch轮胎弹性搜索的过滤器问题
【发布时间】:2014-04-01 20:53:30
【问题描述】:
Want to search for the title from the board with live_flag true.

class Board < ActiveRecord::Base
   has_many :deals
   include Tire::Model::Search

  tire.mapping do
    indexes :title,  type: 'string'
    indexes :deals do
      indexes :title, type: 'string'
    end
  end

  def to_indexed_json
    {
    :title          => title,
    :deals     => {:title => self.deals.map(&:title)},
    }.to_json
  end

  def self.search(params)
    tire.search(load: true) do
      query  {string params[:query]+"*"}
      filter :term, live_flag: true
      sort { by :created_at, "desc" } if params[:query].blank?
    end
  end

end



Now it will not search anything.


It works properly when below code is used. Difference is that i have removed filter text from it.

   def self.search(params)
    tire.search(load: true) do
      query  {string params[:query]+"*"}
      sort { by :created_at, "desc" } if params[:query].blank?
    end
  end

   **But i want to get boards whose live_flag is true.**

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch tire


    【解决方案1】:

    目前您的索引不包括 live_flag 只需将 live_flag 添加到您的 to_indexed_json 和映射

    tire.mapping do
        indexes :title,  type: 'string'
        indexes :live_flag, type: 'boolean'
        indexes :deals do
          indexes :title, type: 'string'
        end
      end
    
      def to_indexed_json
        {
        :title          => title,
        :live_flag     => live_flag,
        :deals     => {:title => self.deals.map(&:title)},
        }.to_json
      end
    

    【讨论】:

    • 现在,在您编辑帖子后,这是一个完全合格的答案。第一个版本(一个句子没有代码)was presented to me for REVIEW by Stack Overflow作为一个低质量的帖子。这是我的职责,也是我评估你的答案的权利。详细了解 SO 的工作原理。不要亲自服用 cmets。我们都在努力提高问题和答案的质量。这种方法有效的最好证明是您已经编辑了您的答案并且它现在满足 SO 标准 - 为了其他用户的利益。
    • 我以为你在哪里问错了问题,那是你我说的。还是很抱歉。
    猜你喜欢
    • 2013-07-27
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2012-03-13
    • 1970-01-01
    相关资源
    最近更新 更多