Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algolia: Best way to query slave index to get sort by date ranking functionality

Tags:

algolia

I have a data set where I want to dynamically sort by date (both ascending and descending) on the fly. I read through the docs and as instructed I've created a slave index of my master index, where the top ranking value is my 'date' ordered by DESC. The date is in the correct integer and unix timestamp format.

My question is how do I query this new index on the fly using the front end Javascript Algolia API?

Right now, my code looks like the following:

this.client = algoliasearch("xxxx", "xxxxx");
this.index = this.client.initIndex('master_index');

this.index.search( 
    this.query, {
        hitsPerPage: 10,
        page: this.pagination,
        facets: '*',
        facetFilters: facetArray
    },
    function(error, results) {
        // do stuff
}.bind(this));

What I've tried doing is to just change the initIndex to use my slave index instead and this does work...but I'm thinking that this is slow and inefficient if I need to reinitialize the index every time the user just wants to sort by date. Isn't there a parameter instead that I can insert in the query to sort by date?

Also, my second question is that even when I change the index to the slave index, it only sorts by descending. How can I have it sort by ascending as well?

I really do not want to create ANOTHER slave index just to sort by ascending date since I have many thousands of rows and am already close to exceeding my record limit. Surely there must be another way here?

Thanks!

like image 948
Ollie Murphy Avatar asked Feb 18 '16 07:02

Ollie Murphy


1 Answers

What I've tried doing is to just change the initIndex to use my slave index instead and this does work...but I'm thinking that this is slow and inefficient if I need to reinitialize the index every time the user just wants to sort by date. Isn't there a parameter instead that I can insert in the query to sort by date?

You should store all the indices you want to do sorts in different properties on the this object:

this.indices = {
  mostRelevant: this.client.initIndex('master_index'),
  desc: this.client.initIndex('slave_desc')
};

Then you can use this.indices.mostRelevant.search() or this.indices.desc.search().

This is not a performance issue to do so.

Also see the dedicated library to create instant-search experiences: https://community.algolia.com/instantsearch.js/

Also, my second question is that even when I change the index to the slave index, it only sorts by descending. How can I have it sort by ascending as well?

I really do not want to create ANOTHER slave index just to sort by ascending date since I have many thousands of rows and am already close to exceeding my record limit. Surely there must be another way here?

This is the only true way to do sorts in Algolia. This is by design what makes Algolia so fast and is currently the only way to do so.

like image 90
vvo Avatar answered Oct 03 '22 00:10

vvo