【发布时间】:2017-01-05 16:34:57
【问题描述】:
我需要为我的应用程序做方面。目前,我希望分面出现在搜索后的结果页面上。我是新来的,很抱歉我的英语不好,我是法国人。
我有 2 个模型:Camping.rb 和 Caracteristiquetest.rb。我做了关联has_many/belongs_to。在 caracteristiquetest 中,我有一列 camping_id 和其他字符串列“piscine”和“barbecue”。
所以,经过大量测试,我找到了一个解决方案,可以在 caracteristiquetest.barbecue 上找到一个值为“非”的帖子(见下文)。现在我尝试将其植入应用程序。
我有一个错误:
C:/Sites/campsite/app/models/camping.rb:66:语法错误,意外 ':', 期待 => {caracteristiquetest.barbecue : "non"}}] ^ C:/Sites/campsite/app/models/camping.rb:66:语法错误,意外 '}',期待keyword_end {caracteristiquetest.barbecue : "non"}}] ^ C:/Sites/campsite/app/models/camping.rb:85:语法错误,意外 输入结束,期待keyword_end
如何解决? 顺便说一句,在修复之后我怎么能把它植入我的视野?我想在搜索后植入此功能以允许用户过滤“烧烤”“是”/“否”。
感谢您的帮助。
_search with head_plugin elasticsearch 当我运行它时,我得到了很好的结果
{
"query": {
"bool": {
"must": [
{"term":
{"caracteristiquetest.barbecue": "non"}}]
}
}
}
campings_controller.rb
def homesearch
@campings = Camping.custom_search((params[:q].present? ? params[:q] : '*'))
end
#Page de résultats
def result
if params[:q].blank?
redirect_to action: :index and return
else
@campings = Camping.custom_search((params[:q].present? ? params[:q] : '*')).page(params[:page]).per(14).results
end
end
camping.rb
mapping do
indexes :name, boost: 8
indexes :adresse
indexes :commune, boost: 10
indexes :description
indexes :nomdep, boost: 10
indexes :nomregion, boost: 10
indexes :ville_id
indexes :region_id
indexes :departement_id
indexes :latitude
indexes :longitude
indexes :etoile
indexes :user_id
indexes :caracteristiquetest_id
#On implante les données du modèle supplémentaire
indexes :caracteristiquetests, type: 'nested' do
indexes :id, type: 'integer'
indexes :piscine, type: 'string'
indexes :barbecue, type: 'string'
indexes :camping_id, type: 'integer'
end
end
def as_indexed_json(options = {})
self.as_json(only: [:name, :adresse, :code_postale, :commune, :description, :nomdep, :nomregion, :latitude, :longitude, :etoile, :caracteristiquetest_id],
include: {caracteristiquetest: {only: [:id, :piscine, :barbecue, :camping_id]}})
end
class << self
def custom_search(query)
__elasticsearch__.search(query: multi_match_query(query), aggs: aggregations)
end
def multi_match_query(query)
{
multi_match: {
query: query,
type: "best_fields",
fields: ["name^6", "nomdep^10", "commune^8", "nomregion^7"],
operator: "and"
}
}
end
def aggregations
{
query: {
bool: {
must: [
{term:
{caracteristiquetest.barbecue : "non"}}]
}
}
}
end
end
homesearch.html.erb
<div class="container">
<div class="row">
<div class="search">
<%= form_tag(result_path, method: :get) %>
<%= text_field_tag :q, params[:q], class:"search-query form-control" %>
<%= submit_tag "GO", class:"btn btn-danger", name: nil %>
</div>
</div>
</div>
【问题讨论】:
-
您正试图在
nil上调用.facets。在您的情况下,@camping是nil。这可能是因为您从未在呈现result.html.erb模板的控制器操作中设置它。我看到您在index操作中设置了@camping,但在result操作中没有设置。 -
@CarlosRamirezIII 你是对的,这是我忘记@camping 的“s”的错误。我编辑了帖子。现在我有另一个错误未定义的方法“方面”#<:model::response::results:0x130b3d38>
标签: ruby-on-rails search elasticsearch ruby-on-rails-5 facet