2012-10-29 52 views
1

輪胎和彈性搜索功能有點困難。輪胎和彈性搜索 - 對象關聯

我有一個列表,它有一個屬性。我試圖讓一個基本的搜索表單工作,以便我可以從屬性創建一個查詢。

# listings/index.html.erb 

<%= form_tag searches_listings_path, method: :get do %> 
    <p> 
    <%= text_field_tag :query, params[:query] %> 
    <%= text_field_tag :property_postcode, params[:property_postcode] %> 
    <%= submit_tag "Search", name: nil %> 
    </p> 
<% end %> 

目前,每當我嘗試過濾搜索結果,似乎property_postcode被忽略,所有結果都被返回。

 # Listing.rb 
include Tire::Model::Search 
     include Tire::Model::Callbacks 

    mapping do 
    indexes :id, type: 'integer' 
    indexes :title, boost: 10 
    indexes :description, analyzer: 'snowball' 
    indexes :posted_at, type: 'date' 
    indexes :property do 
     indexes :postcode, :type => 'string' 
    end 
    end 

    def self.search(params) 

    tire.search(page: params[:page], per_page: 5, load: true) do 
     query do 
     boolean do 
      must { string params[:query], default_operator: "AND" } if params[:query].present? 
      must { term "property.postcode", params[:property_postcode] } if params[:property_postcode].present? 
     end 
     end 
     end 
     end 

    def to_indexed_json 
    to_json(:include => { 
       :property => { 
       :only => [:postcode] 
       } 
      }) 
    end 

最後的財產

#Property.rb 
class Property < ActiveRecord::Base 
    belongs_to :listing, touch: true 
    after_touch() { tire.update_index } 
end 

後來終於

rake environment tire:import CLASS=Listing FORCE=true 

由於提前,

瑞安

+0

另外,如果我不把你在查詢中,只是在網址爲http郵編://本地主機:3000 /列表/搜索= UTF8 %E2%9C%93&property_postcode = w1d2eb它不會引發異常,我把它放在輪胎內。搜索方法 – Ryan

+0

您可以使用'Tire.configure {logger STDERR}開啓日誌併發佈一個鏈接到pastie/hastebin/gist /等輸出? (過濾敏感數據。) – karmi

回答

0
Property.search(page: params[:page], per_page: 5, load: true) do 
    query { string params[:query], default_operator => "AND" } if params[:query].present? 
    filter :term, "property.postcode" => params[:property_postcode] if params[:property_postcode].present? 
end 

一定要打開在映射路由太

mapping :_routing => { :required => true, :path => property.postcode } do 
相關問題