首页 文章

轮胎和弹性搜索 - 对象关联

提问于
浏览
1

我对轮胎和弹性搜索功能有点困难 .

我有一个包含Property的列表 . 我正在尝试使用基本搜索表单,以便我可以从Property创建查询 .

# 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

#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

提前致谢,

瑞安

1 回答

  • 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
    

相关问题