algoliasearch-rails icon indicating copy to clipboard operation
algoliasearch-rails copied to clipboard

Model.reindex raises AlgoliaProtocolError: "attributesToIndex" is deprecated

Open chigginsiii opened this issue 5 years ago • 8 comments

  • Rails version: 5.2.1
  • Algolia Rails integration version:
  • Algolia Client Version: 1.26.0
  • Language Version: ruby 2.5.0p0

Description

Recently, our models started raising exceptions when trying to reindex. Example (paths/user edited out):

[1] pry(main)> Store.reindex
Algolia::AlgoliaProtocolError: Cannot PUT to https://[KEY].algolia.net/1/indexes/stores-dev.tmp/settings: {"message":"You cannot specify \"attributesToIndex\" and \"searchableAttributes\" at the same time. \"attributesToIndex\" is deprecated you should only use \"searchableAttributes\" near line:1 column:851","status":400} (400)
from ./bundle/ruby/2.5.0/gems/algoliasearch-1.26.0/lib/algolia/client.rb:671:in `perform_request'

Steps To Reproduce

  • Configure a model with algoliasearch. Then within that block, define both attributes and searchableAttributes.
  • From console, Model.reindex! still works, Model.reindex raises an error.

chigginsiii avatar Jul 02 '19 21:07 chigginsiii

Can you share your model algoliasearch block? Do you also define attributesToIndex? What version of the rails client are you using? did it start after a recent upgrade?

julienbourdeau avatar Jul 03 '19 23:07 julienbourdeau

Hi Julie!

Here's the gem info from our master branch (rails/ruby are 2.5.0/5.2.1):

 algoliasearch (1.23.2)
 algoliasearch-rails (1.20.4)

It began fairly recently, but the last changes to our Gemfile at these lines was 9 months ago. Since the errors are a new response I think it's happening on Algolia's side.

Store model block (simplified, but still accurate):

 algoliasearch(
    index_name: [...name of the index set from env var, in this case "stores-dev"]
  ) do
    attribute :name, :city, :state, :time_zone_identifier, :type, :full_address, :delivery, :pickup
    attribute(:delivery_zipcodes) { StorePresenter.delivery_zipcodes(self) }

    searchableAttributes [
      :name,
      :city,
      :type,
      :full_address
    ]

    attributesForFaceting %w[state type]

    customRanking [‘asc(name)’]
  end

from console:

Loading development environment (Rails 5.2.1)
[1] pry(main)> Store.reindex
Algolia::AlgoliaProtocolError: Cannot PUT to https://VFM4X0N23A.algolia.net/1/indexes/stores-dev.tmp/settings: {"message":"You cannot specify \"attributesToIndex\" and \"searchableAttributes\" at the same time. \"attributesToIndex\" is deprecated you should only use \"searchableAttributes\" near line:1 column:851","status":400} (400)
from /Users/me/project/api/.bundle/ruby/2.5.0/gems/algoliasearch-1.23.2/lib/algolia/client.rb:572:in `perform_request'

chigginsiii avatar Jul 08 '19 21:07 chigginsiii

@chigginsiii I'm having the same problem as this, did you ever work out what was going on?

nataliethistime avatar Feb 17 '20 03:02 nataliethistime

I think I've figured it out.

For some reason, for some indices SafeIndex#get_settings returns "searchableAttributes" while for some it returns "attributesToIndex" key instead.

Here's the correct behavior:

> idx = SomeModel.instance_eval { algolia_configurations.map { |options, settings| algolia_ensure_init(options, settings) } }[0]
> idx.get_settings.slice("attributesToIndex", "searchableAttributes")
=> {"searchableAttributes"=>["unordered(name)", "product_name", "brand"]}

However, in another environment (same model, just a different index name - I have algoliasearch index_name: "some_model_#{ENV.fetch(...)}"), it behaves differently:

idx.get_settings.slice("attributesToIndex", "searchableAttributes")
=> {"attributesToIndex"=>["unordered(name)", "product_name", "brand"]}

While some libraries post-process settings, the algoliasearch Ruby gem does not perform any transformations on the API response.

I'm not sure what's the logic for this behavior on the Algolia side, why and when it decides to return one key versus another.

drdaeman avatar May 12 '20 20:05 drdaeman

Having the same issue here... is there any update? Thanks!

mustela avatar Nov 02 '20 16:11 mustela

@mustela Deleting my algolia indexes completely and rebuilding them ended up fixing the issue for me.

nataliethistime avatar Nov 03 '20 11:11 nataliethistime

@1vasari yeah... .but that cost a lot of money, specially if you have millions of records :)

mustela avatar Nov 03 '20 11:11 mustela

@mustela Ouch. That sucks.

As I understand it @drdaeman is close the the issue. When debugging this I found that Model.reindex pulls the configuration from the existing index and merges it into the new configuration you specify. So if you have attributesToIndex specified on the live index, it gets merged in again and the exception is thrown.

I don't know the best method, but the solution is to remove the attributesToIndex attribute from the index's configuration before you reindex. My way was to fully delete the index and then rebuild but there must be a better way.

Hope this helps :)

nataliethistime avatar Nov 03 '20 23:11 nataliethistime