How to put ranking in queryrules

I want to rank my data depanding on the users choosen filters. Is there a way to do this in the queries? The only way i found is to do this with index.setSettings(); But then I have to search like index.search() /… but then my queries not working.

  var queries = [{
    indexName: 'restaurants',
    params: {
      hitsPerPage: hitsPerPage,
      aroundLatLng: currentPosition.lat+', '+currentPosition.lng,
      getRankingInfo: true
    }
  }];

  return new Promise((resolve, reject) => {
    algoliaclient.search(queries, function searchCallback(err, content){
        if (err) {
          reject(err);
        }
        resolve(content.results[0].hits);
    });
  });

Hi @duellit,

Thanks for contacting Algolia. Can you clarify what you are trying to do with a specific example, maybe walk us through the experience verbally “I will have records with attribute X, my user will type in query Y, I want it to sort like this…”

Hi @ajay.david

My users should have the choice between sorting the results by date and by location. So if the user choose to order his results by date the ranking should look like this:

customRanking: [
‘geo’,
‘desc(startTimeInt)’
]

otherwise the ranking should look like that:
customRanking: [
‘desc(startTimeInt)’,
‘geo’
]

It does work if I change the ranking in the Algolia-Dashboard. But I am not sure how to do this dynamically in the code.

Hi @duellit,

Thanks for the clarification.

Sorts and ranking in Algolia must always be pre-computed. There is no dynamic sorting at query time.

To achieve this you use “replicas”. Replicas are a complete duplication of your records data, except they’ll have a different sorting or custom ranking.

You’ll need to create two replicas, one for each of your desired custom rankings. For more information on that:

When you created your replicas your user can then select their sort using a sortBy widget:

I hope this clarifies how you can get different sorts with Algolia

Hi @ajay.david,

Thank you for your answer!

I have one more question. If I want to set replicas I have to do this with index.setSettings(…). But then I can’t search like this - right?

algoliaclient.search(queries, function searchCallback(err, content){
        if (err) {
          reject(err);
        }
        resolve(content.results[0].hits);
    });

I think I have to search like this:

index.search(queries, function searchCallback(err, content){
            if (err) {
              reject(err);
            }
            resolve(content.results[0].hits);
        });

But if I search like this my queries not working. How do I have to set the queries if I search in the index like above?