Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.reindex raises AlgoliaProtocolError: "attributesToIndex" is deprecated #357

Open
chigginsiii opened this issue Jul 2, 2019 · 8 comments

Comments

@chigginsiii
Copy link

chigginsiii commented Jul 2, 2019

  • 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.
@julienbourdeau
Copy link
Contributor

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?

@chigginsiii
Copy link
Author

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'

@nataliethistime
Copy link

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

@drdaeman
Copy link

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.

@mustela
Copy link

mustela commented Nov 2, 2020

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

@nataliethistime
Copy link

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

@mustela
Copy link

mustela commented Nov 3, 2020

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

@nataliethistime
Copy link

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants