algoliasearch-rails
algoliasearch-rails copied to clipboard
Model.reindex raises AlgoliaProtocolError: "attributesToIndex" is deprecated
- 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 bothattributes
andsearchableAttributes
. - From console,
Model.reindex!
still works,Model.reindex
raises an error.
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?
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 I'm having the same problem as this, did you ever work out what was going on?
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.
Having the same issue here... is there any update? Thanks!
@mustela Deleting my algolia indexes completely and rebuilding them ended up fixing the issue for me.
@1vasari yeah... .but that cost a lot of money, specially if you have millions of records :)
@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 :)