Justin Sainton
init
bootstrap and notice something important. We’re hooking into filters from the Algolia plugin for our previously defined terms: indices, watchers, and replicas. Our add_indices
callback is fairly simple – if BuddyPress components for either groups or activities is active, we add an object instance of those index classes to the array that we are filtering.add_watchers
routine is slightly more complex. We have to iterate through all the indexes, detect which ones to target with the contains_only
method, and add an instance of that watcher to the array.add_shared_attributes
and add_replicas
callbacks do some heavy lifting. In very few lines of code, we tell Algolia that we want to store a favorites
attribute on our indices and that we want to create a replica index to sort by that attribute with (in both ascending and descending orders). We do this utilizing the Algolia_Index_Replica
class, which is part of the Algolia plugin.Algolia_Index
class, found in the Algolia plugin. We set out to learn the best ways to extend it by first looking at internal examples of indexes for core object types. The core class covers much more than this abbreviated example, but for our purposes here today, we can understand the class in the following sections:contains_only
property. This is used in a number of areas in the base class and throughout the core plugin to check what index is in use. You also see us using the base class method, contains_only()
, in our bootstrap. Next, we set our admin name to a human-readable name in get_admin_name()
and set our get_id()
method as well.should_index()
– This method is checked prior to any synchronization of records between WordPress and Algolia. This includes the initial indexing of records, but also any updates to them as well.supports()
– Similar to should_index()
, supports()
allows you to confirm whether or not this particular item supports indexation. This is checked prior to record deletion and used within watchers.get_records()
– This method transforms the output of an individual entity in WordPress to a suitable entity record for Algolia. Important to note, the output of this function should be an array containing the entity. That entity must contain a unique objectID
parameter.delete_item()
– Called from the watcher, this function synchronizes the deletion of an entity in WordPress with the deletion in Algolia.get_items()
– Note the important parameters passed for the function, $page
and $batch_size
. When indexing, the Algolia plugin will intelligently handle batching large amounts of data, so you don’t have to worry about timeouts. Be sure to pass these along to any functions you use internally to retrieve data.get_re_index_items_count()
– Returns total number of entities locally. For the Users index, it’s the total number of site users. For posts, it’s all your site posts. get_settings()
– Set your base settings for this Algolia index. These settings are incredibly powerful, and worth spending a day researching. At the very least, take a look at how the plugin handles the settings for some of the core entities. get_synonyms()
– You won’t see this used in the core plugin, but as an abstract method, it is required in your class. It can actually be a powerful tool for defining alternate usages for terms found in your searchable attributes. An array of arrays is the expected output, with unique object IDs within each array. See the documentation for more information.watch()
method. Additionally, we need to pass the related index through to the constructor, making it available on the index
property throughout the class.watch()
method, you can see that we’re watching the group actions, as well as the group meta actions, for any updates. After checking the validity and type of data in each method – we’re essentially calling the sync()
method of the index whenever we’re managing updates, an calling the delete_item()
method of the index whenever we are deleting an item.delete_item()
method looks a little bit different in activities than it did in groups. You may also find that you need to tweak how your methods are implemented based on the variables available in each hook that you implement the watcher on.