我需要为我的应用程序做方面。目前,我希望分面出现在搜索后的结果页面上。我是新来的,很抱歉我的英语不好,我是法语。
我有 2 个模型:Camping.rb 和 Caracteristiquetest.rb。我做了关联has_many/belongs_to。在 caracteristiquetest 中,我有一列 camping_id 和其他字符串列“piscine”和“barbecue”。
因此,经过大量测试,我找到了一个解决方案,可以在 caracteristiquetest.barbecue 上找到一个值为“非”的帖子(见下文)。现在我尝试将其植入应用程序。
我有一个错误:
C:/Sites/campsite/app/models/camping.rb:66: syntax error, unexpected ':', expecting => {caracteristiquetest.barbecue : "non"}}] ^ C:/Sites/campsite/app/models/camping.rb:66: syntax error, unexpected '}', expecting keyword_end {caracteristiquetest.barbecue : "non"}}] ^ C:/Sites/campsite/app/models/camping.rb:85: syntax error, unexpected end-of-input, expecting keyword_end
怎么办才能解决?
顺便说一句,在修复之后我怎么能把它植入我的视野?我想在搜索后植入此功能以允许用户过滤“烧烤”"is"/“否”。
谢谢你的帮助。
_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
露营.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>
最佳答案
您正在尝试与 results
进行交互Elasticsearch 返回的响应的接口(interface)。尝试直接与 response
交互对象,例如
# don't call `.results` here because you want the full response object
@campings = Camping.__elasticsearch__.search(...)
根据文档
The returned response object is a rich wrapper around the JSON returned from Elasticsearch, providing access to response metadata and the actual results ("hits").
来源:http://www.rubydoc.info/gems/elasticsearch-model/#Search_results
通过检查生成的 JSON 并阅读 Elasticsearch 文档本身,您应该能够弄清楚如何获取构面和您需要的任何其他数据。
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
关于ruby-on-rails - 如何在 Rails 上使用 elasticsearch 植入 Facets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209743/