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

Need little help how to sort products in a category by 2 fields? #2169

Closed
TitanFighter opened this issue Nov 16, 2016 · 4 comments
Closed

Need little help how to sort products in a category by 2 fields? #2169

TitanFighter opened this issue Nov 16, 2016 · 4 comments

Comments

@TitanFighter
Copy link

TitanFighter commented Nov 16, 2016

In StockReckords there are products, which can be provided by few partners (lets say for now Partner # 1 is our remote partner with a remore warehouse and Partner # 2 is our shop with our warehouse)

At front-end I want to show just products which our warehouse has.

I tried

from oscar.apps.catalogue.search_handlers import SolrProductSearchHandler as SProductSearchHandler

class SolrProductSearchHandler(SProductSearchHandler):

    def get_search_queryset(self):
        sqs = super(SolrProductSearchHandler, self).get_search_queryset()
        return sqs.filter(partner='name_of_our_shop').order_by('-num_in_stock')

But Solr, at the front-end, does not sort products correctly. Solr when does ordering by num_in_stock takes into account not JUST num_in_stock of our shop, but also takes into account num_in_stock of other partners. In other words - Solr starts to show just those products, which has relation to partner # 2 (our shop), but takes num_in_stock from Partner # 1.

For example, there is a product Intel i5-5200, Partner # 1 has 20 items, we have 0. I expect to get this product at the end of the page, but I see it at the top.

How to restict sorting by Partner # 2 (our shop) + our num_in_stock?

Thanks a lot!

@Henk-JanVanHasselaar
Copy link

Henk-JanVanHasselaar commented Nov 29, 2016

@TitanFighter Quite hard to tell because we can't see what your index is like ;)
However if you're going to sort on it, I think this is something you might want to define in your ProductIndex.
If you want to sort on local stock you could do something like this:

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    # your fields
   local_stock = indexes.IntegerField(null=True, faceted=False)

    def prepare_local_stock(self, obj):
        # define product.get_local_stock somewhere, or just get stock here
        return int(obj.get_local_stock())

doing this allows you to simply .order_by(-local_stock)

I hope this helps!

@TitanFighter
Copy link
Author

Finally! Thanks a lot! For others want to mention, that it is necessary to update schema before index rebuilding :)

@TitanFighter
Copy link
Author

Henk-JanVanHasselaar, the code above works just for catalogue page. Could you please give the cue, where can I apply .order_by(-local_stock) for the case of seach results page, i.e http://localhost/search/?q=OBJECT-to-FIND. Can not find proper piece of code.

Thank you.

@Henk-JanVanHasselaar
Copy link

@TitanFighter That should be in view that handles your search results.. Just make sure that in your view you order the SearchQuerySet on -local_stock

On further questions can you please submit them in the mailing list?
Thanks.

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

No branches or pull requests

2 participants