-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: Sorting for Models? #245
Comments
Hi @thornycrackers, thanks for this feedback! Improving the faceting handling in this project is definitely something we will consider, although I'm not sure what will be the timeline for implementing this feature. This being said, your opinion is very valuable as it will help us shape the design of this feature! Can you share your thoughts about:
|
So this is the code that I ended up using to make this happen myself. You guys will have a better idea on how to make a cleaner API for it than I. from typing import Dict, List # noqa: F401
from algoliasearch_django import AlgoliaInde
class ReplicaExtendedIndex(AlgoliaIndex):
SORTING_REPLICAS = {} # type: Dict[str, List[str]]
# (https://www.algolia.com/doc/api-reference/api-parameters/customRanking/)
# E.G. 'model_name_ascending': {'ranking': ['asc(name)']},
def set_settings(self):
super().set_settings()
for index_name, settings in self.SORTING_REPLICAS.items():
index = self._AlgoliaIndex__client.init_index(index_name)
index.set_settings(settings)
def raw_search_sorted(self, model, sort_index: str, query='', params=None):
if params is None:
params = {}
if sort_index not in self.SORTING_REPLICAS.keys():
msg = 'sort {} by index {} is not implemented yet.'.format(model, sort_index)
raise ValueError(msg)
adapter = self._AlgoliaIndex__client.init_index(sort_index)
return adapter.search(query, params) That way when I run import ReplicaExtendedIndex
class MyModelIndex(ReplicaExtendedIndex):
SORTING_REPLICAS = {
'mymodel_name_ascending': {'ranking': ['asc(name)']},
'mymodel_name_descending': {'ranking': ['desc(name)']},
}
fields = ('name', 'description')
settings = {
'searchableAttributes': ['name', 'description'],
'replicas': list(SORTING_REPLICAS.keys())
} Again this was pretty hasty but I think the easiest way of adding this feature is having some extra variable on the index class that allows you to define the additional indexes with their sorting and then have the methods incorporate them. |
Thanks for sharing this! So if I understand correctly, you're asking for better replicas support in Django, and especially supporting search with sort parameter by using the appropriate replica. This is a good idea, we'll definitely consider it for our next iterations on this project! Just to make sure I don't get you wrong, this is unrelated to
|
I've updated the title, I can see that being a bit confusing. Sorting is what I meant originally but thought it was called Faceting. I see those are two separate things. You've understood correctly about the better sorting and replica support. |
Thanks for the clarification! We'll update you here as we make progress on this feature 🙂 |
@thornycrackers Hello,sir. How can i using your ReplicaExtendedIndex in Django View? |
Thanks for building this useful package. I looking through the docs tryig to fing something about sorting and wasn't able to find anything. Is this a feature that could be added?
The text was updated successfully, but these errors were encountered: