Algolia rails 动态排序

Algolia rails sortBy dynamically

我正在使用 Algolia rails 构建搜索,但在动态排序时遇到困难。例如,用户可以从下拉列表中选择按价格升序或价格降序排序。

这是我定义索引的模型

class Product < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch per_environment: true, if: :publishable? do
    attribute :id, :name, :description, :seller_id, :condition, :location, :slug, :status, :city, :state, :stock_quantity,
      :shipping_method, :price
    attribute :created_at_i do
      created_at.to_i
    end
    attribute :updated_at_i do
      updated_at.to_i
    end
    attribute :price do
      price.to_f
    end
    attributesToIndex ['name', 'unordered(description)', 'seller_id',
      'condition', 'location', 'slug', 'created_at', 'updated_at', 'status',
      'geo(city)', 'geo(state)']
    geoloc :latitude, :longitude
    numericAttributesToIndex ["price", "stock_quantity"]
    attributesForFaceting ['price', 'condition', 'shipping_method']
  end

和产品控制器

def index
queries = { hitsPerPage: 5, page: params[:page].to_i, facets: '*',
      facetFilters: [
        "condition: #{params[:condition]}",
        "shipping_method: #{params[:shipping_method]}"
      ],
      numericFilters: [
        "price:#{params[:min_price] || 0} to #{params[:max_price] || 999999999999999}"
      ],
      sortBy: ["asc(price)"]
    }

    if latLng.present?
      queries[:aroundLatLng] = latLng
      queries[:aroundRadius] = radius
    end


    @response = Product.search(params[:query],queries)

Algolia returns 错误 "invalid parameter sortBy"。我试图搜索 algolia 文档,但找不到任何信息。 感谢您的帮助。

以下是 Algolia 网站上的相关文档页面列表,可帮助您了解 Algolia 的排名工作原理:

  • [Getting started] Tweak Ranking and Relevance

    The reason our engine is so fast is because each index has its own settings and rankings. This means you are able to create different sets of searchable attributes and attribute ranking relevance by storing your data in multiple indices. This is accomplished using slave indices which are seamlessly synchronized with a master index. Each slave index can then be configured with its own set of business metrics to tune the relevance calculation.

    一个指数有一个无法更改的特定排名公式。但是,您可以通过使用具有不同排名公式的从索引轻松克服此限制。

  • [Tutorials][Ranking Formula] Tie Breaking algorithm
    [FAQ] How does Algolia's tie breaking algorithm work?
    这两个 link 将帮助您了解 Algolia 排名的工作原理以及如何对其进行调整。基本上,根据您的 use-case,在文本相关性之前按价格排序并没有多大意义。因此,根据您的用例,您可能只想将 customRanking 更改为 price,或者您可能想在排名公式的顶部添加 price 属性。

  • [FAQ] What are slave indices and what are their benefits?
    最后一个 link 更深入地解释了 "slave index" 对 Algolia 的意义。
  • [Rails documentation] Multiple sort criteria
    [Rails GitHub documentation] Master/Slave
    在这些最后的 link 中,您会找到使用 Algolia 的 Rails 客户端和多个从站的代码示例。第一个实际上准确地展示了您的用例:按价格排序。

考虑到所有这些,最后,您只是希望将这些添加到您的模型中(如果您想使用 customRanking 方式):

add_slave 'Product_by_price_asc', per_environment: true do
  customRanking ['asc(price)']
end

add_slave 'Product_by_price_desc', per_environment: true do
  customRanking ['desc(price)']
end

然后在你的控制器中,你可以这样查询它们

query_params = { slave: "Product_by_price_#{params[:sort_order]}", hitsPerPage: 5, ... }
Product.search params[:query], query_params

您可能还应该在 front-end 中执行此实现,以便能够充分利用 Algolia 的即时搜索功能,请参阅 [FAQ] Searching from the front-end or the back-end