【问题标题】:How implant Facets with elasticsearch on rails如何在 Rails 上使用 elasticsearch 植入 Facets
【发布时间】: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。在您的情况下,@campingnil。这可能是因为您从未在呈现result.html.erb 模板的控制器操作中设置它。我看到您在index 操作中设置了@camping,但在result 操作中没有设置。
  • @CarlosRamirezIII 你是对的,这是我忘记@camping 的“s”的错误。我编辑了帖子。现在我有另一个错误未定义的方法“方面”#<:model::response::results:0x130b3d38>

标签: ruby-on-rails search elasticsearch ruby-on-rails-5 facet


【解决方案1】:

您正在尝试与 Elasticsearch 返回的响应的results 接口进行交互。尝试直接与response 对象交互,例如

# don't call `.results` here because you want the full response object
@campings = Camping.__elasticsearch__.search(...)

根据文档

返回的响应对象是 JSON 的丰富包装器 从 Elasticsearch 返回,提供对响应元数据的访问和 实际结果(“命中”)。

来源:http://www.rubydoc.info/gems/elasticsearch-model/#Search_results

您应该能够通过检查生成的 JSON 并阅读 Elasticsearch 文档本身来弄清楚如何获取方面和您需要的任何其他数据。

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

【讨论】:

  • 感谢@Carlos 的回复,我删除了@campings = Camping.elasticsearch 中的.results ... 我总是有一个错误“未定义的方法'facets' for #<:model::response: :response:0x10488c18>" JSON 有问题吗?我能做些什么 ?对不起菜鸟问题...
  • 您正在调用.facets,但这可能不是您需要的方法名称。我的建议是检查@campings 变量并查看它定义了哪些方法。由于它只是 Elasticsearch JSON 响应的包装器,因此 Elasticsearch 文档本身应该有助于弄清楚如何从响应中访问方面。请阅读文档并查看响应对象的结构以及方面的位置。
  • 好的,我会这样做的,我会回来的。对于初学者来说,弹性搜索肯定不是 esy :) 只有一件事:如何检查它?
  • 同意 :) 希望这为您指明了正确的方向,并且您能够得到它。 LMK 结果如何。
  • 之前只有一件事:如何检查它?在 head_plugin 中?
猜你喜欢
  • 2014-06-14
  • 1970-01-01
  • 2015-01-23
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多