从 Rails 添加 Algolia 分面设置

Add Algolia faceting setting from Rails

我将 Algolia 与 Rails 4 一起使用,一切正常。我很好奇从 Rails 添加分面设置,而不是从我的仪表板。

这可能吗?如果是,我如何在我的 Rails 项目中实现它?

我只从 rails 中看到了 attributesForFaceting,但它没有添加到 Algolia facets 设置中。

我是不是用错了attributesForFaceting方法?如果是这样,我如何将它用作`refinementList 的属性?

在索引时,所以在您的 rails 应用程序中,attributesForFaceting 确实是您要更改的参数。
引用 the docs here:

class Contact < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    # [...]

    # specify the list of attributes available for faceting
    attributesForFaceting [:company, :zip_code]
  end
end

您应该检查 您的 Algolia 仪表板 > 您的索引 > 显示选项卡 此设置是否正确设置。

facets parameter 用于指定要在查询时检索的构面列表。

List of object attributes that you want to use for faceting. [...] Only attributes that have been added in attributesForFaceting index setting can be used in this parameter. You can also use * to perform faceting on all attributes specified in attributesForFaceting.

但是,由于您在前端使用 instantsearch.js,因此您应该在分面小部件上设置 attributeName
在这个带有 refinementList 的示例中,我们想要检索 brand 个方面:

search.addWidget(
  instantsearch.widgets.refinementList({
    container: '#brands',
    attributeName: 'brand'
  })
);

然后 instantsearch.js 中的小部件将使用此 attributeName 参数来设置 Algolia API 期望的 facets 参数。