-
Notifications
You must be signed in to change notification settings - Fork 58
Index Replicas
Create indices with the same content but with a different ranking formula.
Sometimes you may want to rank your results based on different criteria.
For example, in a WordPress shop powered by WooCommerce, you might want to list products by displaying the least expensive products first.
In Algolia, to achieve different rankings on the same datasets, you can use Index Replicas.
Index Replicas are automatically synced with the Master index, and write operations are not deducted from your monthly quota.
To register custom index replicas, there are 2 filters:
Filter Name | Params |
---|---|
algolia_index_replicas | array $replicas, Algolia_Index $index |
algolia_{$index_id}_index_replicas | array $replicas, Algolia_Index $index |
Here is an example of creating an index replica that will order results based on the lowest price
.
The price
field is not natively available in WordPress, here we assume it is available.
<?php
/**
* @param array $replicas
* @param Algolia_Index $index
*
* @return array
*/
function my_products_index_replicas( array $replicas, Algolia_Index $index ) {
if ( 'posts_product' === $index->get_id() ) {
$replicas[] = new Algolia_Index_Replica( 'price', 'asc' );
$replicas[] = new Algolia_Index_Replica( 'price', 'desc' );
}
return $replicas;
}
add_filter( 'algolia_index_replicas', 'my_products_index_replicas', 10, 2 );
Note that you could also have used the second form of the filter to directly scope the replicas to a given index:
<?php
/**
* @param array $replicas
*
* @return array
*/
function my_products_index_replicas( array $replicas) {
$replicas[] = new Algolia_Index_Replica( 'price', 'asc' );
$replicas[] = new Algolia_Index_Replica( 'price', 'desc' );
return $replicas;
}
add_filter( 'algolia_posts_product_index_replicas', 'my_products_index_replicas');
When using Algolia in the backend, you can use 2 hooks to customize the replica to search on.
This example will register a custom replica to allow listing of searchable posts by date from oldest to newest:
<?php
/**
* @param array $replicas
* @param Algolia_Index $index
*
* @return array
*/
function my_custom_index_replicas( array $replicas, Algolia_Index $index ) {
if ( 'searchable_posts' === $index->get_id() ) {
$replicas[] = new Algolia_Index_Replica( 'post_date', 'asc' );
}
return $replicas;
}
add_filter( 'algolia_index_replicas', 'my_custom_index_replicas', 10, 2 );
After this change in your code, you will need to push the settings of the searchable posts index from the "autocomplete" page of the plugin.
You should now have a newly created replica index which you can see in the Algolia dashboard.
You now need to use filters to correctly target the new replica:
<?php
function my_search_order_by() {
return 'post_date';
}
add_filter( 'algolia_search_order_by', 'my_search_order_by' );
function my_search_order() {
return 'asc';
}
add_filter( 'algolia_search_order', 'my_search_order' );
Your search page should now return results from oldest to newest.
You could also dynamically switch ordering based on a $_GET
parameter:
<?php
function my_search_order_by_with_query_parameter( $attribute_name ) {
if ( isset( $_GET['orderby'] ) && $_GET['orderby'] === 'date-asc' ) {
return 'post_date';
}
return $attribute_name;
}
add_filter( 'algolia_search_order_by', 'my_search_order_by_with_query_parameter' );
function my_search_order_with_query_parameter( $order ) {
if ( isset( $_GET['orderby'] ) && $_GET['orderby'] === 'date-asc' ) {
return 'asc';
}
return $order;
}
add_filter( 'algolia_search_order', 'my_search_order_with_query_parameter' );