Manual indexing with rake
The rake command algoliasearch:reindex
looks for all models in your app and indexes them.
Regular reindexing
To reindex all objects in place, use thereindex!
class method.
This method sends all entries to the Algolia index.
Any record with the same objectID
is replaced, and new ones are added.
This method doesn’t delete records from your index.
To delete them, clear your index before reindexing.
Ruby
Zero-downtime reindexing
To reindex all your records without downtime, including deleted objects, use thereindex
class method.
Ruby
INDEX_NAME.tmp
.
After everything is indexed, the temporary index overwrites your production index.
This method guarantees that your index is never empty,
but it temporarily doubles the number of records in your Algolia application.
If you’re using an index-specific API key,
make sure you’re allowing both INDEX_NAME
and INDEX_NAME.tmp
.
If you’ve changed settings, rules, or synonyms in the Algolia dashboard,
this method deletes or resets them, because it recreates your index configuration as defined in your Rails project.
To prevent this, turn off check_settings
.
Indexing subsets
To index a subset of your records, use model scoping. In this case, it’s better not to use zero-downtime indexing, since it would replace the whole index with just the filtered objects. Use regular reindexing withreindex!
.
Ruby
index_objects
.
Ruby
Index single instances
To index a single instance, use theindex!
instance method.
To remove a model from the Algolia index, use remove_from_index!
Ruby
Automatic updates
To keep Algolia indices synced with your Rails models, this gem uses these Rails callbacks:after_validation
before_save
after_commit
Ruby
Temporarily turn off auto-indexing
To temporarily turn off auto-indexing, use thewithout_auto_index
scope.
This can be helpful to achieve better performance if you’re making many changes.
For example, if you delete all contacts from the index and then create 10,000 new contacts,
your app would make 10,001 API requests.
Ruby
reindex!
after,
your app makes fewer API requests,
since the reindex!
method batches the requests automatically.
Ruby
Indexing only if attributes have changed
For database-stored attributes, Rails provides awill_save_change_to_ATTRIBUTE?
method to detect changes.
Add this method for the all dynamic attributes you defined.
Otherwise, Algolia updates every model because it won’t know whether a dynamic attribute was updated.
Ruby
ATTRIBUTE_changed?
.
The Algolia gem checks for both method names.
tags
and geoloc
helpers
The tags
or geoloc
helpers map to the _tags
and _geoloc
attributes.
That’s why you need to use double underscores:
will_save_change_to__tags
will_save_change_to__geoloc
Ruby
Single will_save_change_to
method
If Algolia finds an algolia_dirty?
method,
it calls this method instead of all the will_save_change_to_ATTRIBUTE?
methods for this model.
Ruby
Conditional indexing
To control the indexing of a record, add constraints with the:if
or :unless
options.
Ruby
saveObjects
and deleteObjects
methods are called to keep the index in sync with your database.
Since the gem is stateless and can’t know whether the object doesn’t match your constraints anymore or if it never did,
these operations are always performed.
To prevent this, add an ATTRIBUTE_changed?
method.
Ruby
Indexing errors
AnyRecord at the position XX objectID=XX is too big
errors during indexing are because you’ve exceeded the size limit for records.
Reduce the size of your records and try again.