Skip to content
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

Open
thornycrackers opened this issue Mar 6, 2018 · 6 comments
Open

Enhancement: Sorting for Models? #245

thornycrackers opened this issue Mar 6, 2018 · 6 comments
Labels
Enhancement request for an improvement.

Comments

@thornycrackers
Copy link

thornycrackers commented Mar 6, 2018

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
Copy link
Member

PLNech commented Mar 8, 2018

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:

  • What would you like to be able to do with a more integrated faceting?
  • How would you like to use faceting in your django apps?

@PLNech PLNech added the Enhancement request for an improvement. label Mar 8, 2018
@thornycrackers
Copy link
Author

thornycrackers commented Mar 15, 2018

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
Copy link
Member

PLNech commented Mar 16, 2018

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 thornycrackers changed the title Enhancement: Faceting for Models? Enhancement: Sorting for Models? Mar 16, 2018
@thornycrackers
Copy link
Author

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
Copy link
Member

PLNech commented Mar 19, 2018

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

@watsonhaw5566
Copy link

watsonhaw5566 commented Oct 22, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement request for an improvement.
Projects
None yet
Development

No branches or pull requests

3 participants