• Resolved Warwick Anderson

    (@scwarwick)


    Does WP Search with Algolia have the functionality to push content to multiple Algolia indices? We have a client that needs an index of the whole site and then a separate index of just one post type and which has different sorting and ranking configuration.

    Is this possible? We have no problem hooking onto hooks and writing custom code, I just want to confirm whether its doable, and get pointed in the right direction if it is.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hi @scwarwick

    Sort of, it depends on what functionality you’re using things for. I know that if you’re using the Search Page settings and/or Instantsearch, all that goes into the wp_searchable_posts index, if using the default prefix.

    However, the Autocomplete setup gets their own indexes if not using the “All Posts” setting there. That said, as could perhaps be surmised, those indexes would also only be made available to the Autocomplete setup.

    Thread Starter Warwick Anderson

    (@scwarwick)

    Hi @tw2113

    Thanks for the quick response!

    We have overriden instantsearch.php and implemented a custom JavaScript solution with the Insantsearch JavaScript API. And we are not using Autocomplete (yet). So, we are sorted for all of the front-end works. I just wanted to know if there are hooks or functions in the WP Search with Algolia plugin that I could use to sync custom post types to an additional Algolia index.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I can’t say “no, not possible”, but I can’t say for certain how easy it would be to do either.

    I’m looking most specifically at the load_indices method that can be seen at https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.8.1/includes/class-algolia-plugin.php#L277-L346

    Here is where we set up the indices for “searchable posts types”, individual post types, terms, and users.

    There is a algolia_indices filter on line 322 that you could use to add your own, in similar ways that is shown in the previous lines there. Worth trying if you can re-use the Algolia_Posts_Index class shown on line 303. However, I’m suspecting you’d probably need to extend your own class off of Algolia_Index so that you could set your own unique ID like seen set at https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.8.1/includes/indices/class-algolia-posts-index.php#L427-L429 Much of the rest could probably be copy/pasted for ease of implementation.

    Last part would be the algolia_changes_watchers filter on line 341 that you could use to set a new watcher class on, for the given index. For this, I think you very likely could just reuse the Algolia_Post_Changes_Watcher class since it just needs an index specified and I am not seeing anything needing necessarily unique in it. Although if you look at that class, there may be some method you don’t need/want that could be trimmed down with your own changes watcher class too. I’ll leave that up to you.

    You’d want to do a similar foreach loop like shown on line 324, so that you can set the enabled status, and whatnot. One thing to note with lines 325-326, you should be able to get that name prefix and client with similar code as below:

    $algolia_plugin     = \Algolia_Plugin_Factory::create();
    $synced_indices_ids = $algolia_plugin->get_settings()->get_synced_indices_ids();
    $index_name_prefix  = $algolia_plugin->get_settings()->get_index_name_prefix();
    $client             = $algolia_plugin->get_api()->get_client();

    A slightly more complete example can be seen at https://github.com/WebDevStudios/algolia-snippet-library/blob/main/indexing/reindex-on-comment-save.md where we have a snippet to re-index a post on comment save. Your case is simply adding a watcher though.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    As a follow-up comment, to help with some separation, I’m curious about the use-case here. Asking because there’s chance that someone else would need a similar setup, and if we can get some tested and confirmed documentation out of this, that’d help everyone.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Syn to multiple Algolia indices’ is closed to new replies.