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

提前致谢,

瑞安

4

1 回答 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
于 2012-12-04T18:59:11.157 回答