algolia / algoliasearch-django

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

Enhancement: Sorting for Models? #245

Open thornycrackers opened 6 years ago

thornycrackers commented 6 years ago

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?

PLNech commented 6 years ago

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:

thornycrackers commented 6 years ago

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 algolia_applysettings I will be able to have my replica indexes created. The search is added so that I can do raw searching with the ability to sort. I think these are pretty important, I would guess most people will incorporate some kind of sorting in there use of algolia. This way when I want to add a new index for a model I can use it like this

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.

PLNech commented 6 years ago

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 Faceting, but rather about better handling Sorting and replica indices?

~If I'm right, then let's move your comment to a new issue about Sorting and replicas to make sure we address each topic separately! And in this case, do you still want to discuss Faceting for Models?~

~If I'm wrong here, could you help me understand the link with Faceting?~

thornycrackers commented 6 years ago

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.

PLNech commented 6 years ago

Thanks for the clarification! We'll update you here as we make progress on this feature 🙂

watsonhaw5566 commented 3 years ago

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.

@thornycrackers Hello,sir. How can i using your ReplicaExtendedIndex in Django View? The 'raw_search_sorted' instead of 'raw_search' right?