ruby-on-rails - 如何在嵌套的Rails模型上设置Elasticsearch方面搜索

标签 ruby-on-rails elasticsearch tire

是否可以在嵌套模型上设置构面搜索?我有一个具有配置文件模型的用户模型。我可以在用户模型中搜索个人文件模型中的术语。现在,我想通过概要文件模型中的location属性过滤搜索结果(用户)。

用户模型:

class User< ActiveRecord::Base
  has_one :profile

  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
   indexes :profiles do
    indexes :first_name
    indexes :last_name
    indexes :summary, type: 'string'
    indexes :location, type: 'string', boost: 50
    indexes :subjects, type: 'string', boost: 100
    indexes :education, type: 'string'
   end
  end

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 10) do |s|
      s.query { string params[:query]} if params[:query].present?
      s.facet "locations" do
        terms :location???
      end
    end
  end

  # Rest of class omitted
end

在搜索方法中,我不知道为“术语”加上什么。 terms :location???
View :
  <% @allusers.facets['locations'??]['terms'].each do |facet| %> **error on this line, locations cannot be nil**
      <li>
      <%= link_to_unless_current Profile.find(facet['term']).location, params.merge(id: facet['term']) %>
        <% if params[:location??] == facet['term'].to_s %>
          (<%= link_to "remove", location: nil %>)
        <% else %>
          (<%= facet['count'] %>)
        <% end %>
      </li>
    <% end %>

在 View 中,我不知道该用什么代替[:location]

最佳答案

您应该可以将terms :location替换为terms "profiles.location"。请注意,它现在是字符串而不是符号。尽管您可能需要改用term,还是允许用户在其个人资料中拥有多个位置?

表单字段和构面名称无需更改。您可以将构面命名为“foobar”,只要构面名称在模型和 View 中相同,它就仍然可以使用。参数是相似的,它只是保存值,可以命名为任何东西。我会考虑将其重命名为profile_location只是为了遵循用下划线分隔关联的rails约定。

关于ruby-on-rails - 如何在嵌套的Rails模型上设置Elasticsearch方面搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20047492/

相关文章:

javascript - Rails 什么时候编译 CoffeeScript?

ruby-on-rails - Soundcloud 播放器出现在 Chrome 中的 colorbox 上

ruby-on-rails - 运行 rspec 测试时关系不存在

java - org.elasticsearch.client.RequestOptions只是在Elasticsearch客户端中找不到的依赖项

ruby-on-rails - 在轮胎上使用Elasticsearch通配符

ruby-on-rails - 防止 Rails 在执行 ssh 时挂起?

c# - ElasticSearch电子邮件地址,带有特殊字符,例如@

elasticsearch - 没有JSON查询的Jest客户端中的聚合

ruby-on-rails-3 - Elasticsearch Tire on Rails,返回该年龄段的所有男性,但也返回女性

ruby-on-rails - 允许在带有轮胎的 ElasticSearch 中过滤的术语不存在