gpt4 book ai didi

ruby-on-rails-3 - 轮胎和 Elasticsearch -对象关联

转载 作者:行者123 更新时间:2023-12-03 00:30:25 26 4
gpt4 key购买 nike

我在使用Tire和Elastic Search功能时遇到了一些困难。

我有一个包含属性的 list 。我正在尝试使用基本的搜索表单,以便可以从媒体资源创建查询。

# 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

提前致谢,

瑞安

最佳答案

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

关于ruby-on-rails-3 - 轮胎和 Elasticsearch -对象关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com