关于嵌套模型的Algolia_search

时间:2016-10-06 13:28:26

标签: ruby-on-rails algolia

我有一个User模型,虽然他的Skills有很多Masteries

我正在使用表单来检索用户,使用Algoliasearch,我想检索所有具有特定技能的用户(IE,如果我有一个名为“John”的用户,它具有“Origami”技能,如果我输入“John”或“Origami”,他应该出现在结果中

我尝试通过映射嵌套技能来实现,但这似乎不起作用

这是模型

class Creator < ActiveRecord::Base
include AlgoliaSearch


algoliasearch do
  # all attributes will be sent
  add_attribute :creator_skills
end

has_many :masteries
has_many :skills, through: :masteries

def creator_skills
  self.masteries.map do |s|
    { name: s.skill.name }
  end
end

[...]

表单返回query param,用于检索带

的创建者
  @creators = Creator.where(display_index: true).algolia_search(params[:query]).shuffle

我错过了什么吗?是否可以映射嵌套模型?

1 个答案:

答案 0 :(得分:2)

您无法将.where子句和algolia_search方法结合使用。此外,您应该使用分面功能来过滤技能。这可能看起来像:

class Creator < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    add_attribute :creator_skills

    attributesForFaceting ['creator_skills', 'display_index']
  end

  has_many :masteries
  has_many :skills, through: :masteries

  def creator_skills
    # array of skill names
    self.skills.map(&:name)
  end

  [...]

end

# empty query (= match all) + facet filters
Creator.algolia_search('', filters: 'creator_skills:Origami AND display_index:true')