1

Tire と Elastic Search の機能に少し問題があります。

プロパティを持つリスティングがあります。プロパティからクエリを作成できるように、基本的な検索フォームを機能させようとしています。

# 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 に答える