algolia / algoliasearch-django

Seamless integration of Algolia into your Django project.
https://www.algolia.com
MIT License
173 stars 65 forks source link

possible to set replica settings in config? #273

Closed jaredmcdonald closed 5 years ago

jaredmcdonald commented 5 years ago

hello! i know it's possible to configure replicas in the index settings, but is there a way to configure settings on those replicas declaratively?

i.e., is there a algoliasearch-django equivalent of this:

products_by_price_asc.setSettings({
  ranking: [
    "asc(price)",
    "..."
  ]
});

(from https://www.algolia.com/doc/guides/ranking/sorting/#algolia-replica-indices)

thanks! let me know if i can clarify in any way.

nunomaduro commented 5 years ago

Hi @jaredmcdonald,

There is no equivalent of setSettings in replicas the algoliasearch-django. A workaround may be to override the set_settings method on your AlgoliaIndex:

class MyModelIndex1(AlgoliaIndex):
    name = 'MyModelIndex1'

    # ...

    def set_settings(self):
        super(MyModelIndex1, self).set_settings()

        # Code similar to (workaround):
        client = algoliasearch.Client("YourApplicationID", 'YourAPIKey')
        index = client.init_index('your_replica_name')
        index.set_settings({"customRanking": ["desc(followers)"]})

What do you think? 👍

alexanderatallah commented 5 years ago

@nunomaduro This looks helpful! Looks like we could iterate through each replica, init a new index for each one, and save per-replica rankings

nunomaduro commented 5 years ago

Exacly!

watsonhaw5566 commented 3 years ago

@alexanderatallah @nunomaduro

Hello guys,How can i use "MyModelIndex1" is My Django View.

I don't how to using replica_index in "algoliasearch-django" native.

I wannt to using diffrent replica_name to sored my index ranking.

My code now look like that.


 # index.py
from algoliasearch_django import AlgoliaIndex
from algoliasearch import algoliasearch
class MyModelIndex1(AlgoliaIndex):
    name = 'MyModelIndex1'
    # ...
    def set_settings(self):
        super(MyModelIndex1, self).set_settings()

        # Code similar to (workaround):
        client = algoliasearch.Client("YourApplicationID", 'YourAPIKey')
        index = client.init_index('your_replica_name')
        index.set_settings({"customRanking": ["desc(followers)"]})
register(MyModel, MyModelIndex1)

 # view.py  I'm using Django-rest-framework so like.
from rest_framework.views import APIView
from algoliasearch_django import raw_search
class MyIndexView(APIView):
    def get(self,request):
           return response = raw_search(MyModel, request.query_params['query'], params)

I'm using another 'replica_name_index' but not work.

so i wanna asking you how to do that .Thanks your very much.