2012-08-04 1 views
0

연령대별로 검색을 필터링하고 싶습니다. 이제탄성 검색 : 타이어 보석이있는 그룹 별 필터링

내가 가진 : program.rb

class Program < ActiveRecord::Base 
    has_many :age_groups, through: :programs_age_groups_relations 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 
    mapping do 
    indexes :name, analyzer: 'snowball', boost: 100 
    indexes :description, analyzer: 'snowball' 
    indexes :age_groups do 
     indexes :name, analyzer: 'snowball' 
    end 
    end 

    def to_indexed_json 
    to_json(include: {age_groups: {only: :name}}) 
    end 
    def self.search(params, options={}) 
    tire.search(load: {include: 'age_groups'}) do 
     query do 
     boolean do 
      must { string params[:name_query] } if params[:name_query].present? 
     end 
     end 

    filter do 
     boolean do 
     if params[:age_groups].present? 
      params[:age_groups].each do |ag_name| 
      must { string ag_name } 
      end 
     end 
     end 
    end 
    end 
end 

index.html.haml

컨트롤러
= form_tag programs_path, method: :get do |f| 
    = text_field_tag :name_query, params[:name_query] 
    - AgeGroup.all.each do |ag| 
    = check_box_tag 'age_groups[]', ag.name, params[:age_groups].include?(ag.name) 

:

@programs = Program.search(params) 

연령 그룹 :

AgeGroup.create([{name: 'Baby'}, {name: 'Toddler'}, {name: 'Preschoolers'}, {name: 'Elementary'}, {name: 'Middle School'}]) 

Realtion :

class ProgramsAgeGroupsRelation < ActiveRecord::Base 
    attr_accessible :age_group_id, :program_id 
    belongs_to :age_group 
    belongs_to :program 
end 

내가 검색 양식 아무것도에서 하나 이상의 age_groups을 확인 검색 결과로 발생했다.

어떻게이 작업에 타이어를 올바르게 사용할 수 있습니까?

답변

2

나는 당신의 질문을 이해할 수 있지만이 StackOverflow 대답을 확인하시기 바랍니다 : Elasticsearch, Tire, and Nested queries/associations with ActiveRecord 타이어와 ActiveRecord 협회에 대한 정보를 확인하십시오.

일단 정렬했다면 "예상치 못한 결과가 발생했습니다"라는 힌트가 나타나기 때문에 질문을 업데이트하십시오. 그러나 어떤 질문인지는 모르겠습니다.