Problem sorting Elasticsearch/Django query

Asked

Viewed 54 times

0

I have this model of Django:

class Profile(User):

rg = models.CharField(max_length=20, null=True,blank=True)
cpf = models.CharField(max_length=15, unique=True, null=True)
about = models.TextField(_('about'),max_length = 10000, null=True, blank=True)
knowledge = models.ManyToManyField(SubCategory, related_name="profile_knowledge")

class Meta:
    verbose_name = _('profile')
    verbose_name_plural = _('profiles')
    db_table = 'pd_profile'
    permissions = (
        ("view_all_users", "Can view user details"),
        ("edit_all_users", "Can edit user details"),
        ("search_profile", "Can search talents"),
        ("user_config", "Can access user config page "),
        ("add_users_permissions", "Can add global permissions to user"),
        ("export_user_data", _("Can export users data")),
    )

def __str__(self):
    return self.full_name

And I want to query by Profiles that way:

query = SearchQuerySet().all().models(Profile)
t = query.filter(content=data.get('value'))
if not t:
    suggestion = query.spelling_suggestion(data.get('value'))
    t = query.filter(content=suggestion)
query = t
query = query.order_by("full_name")

My problem is that it does not return in alphabetical order at all.. Am I doing something wrong? Thanks!

Django-haystack==2.5.0 djangorestframework==3.3.2 Elasticsearch==1.5.2

  • Hello Lucas I take the opportunity to welcome Stackoverflow, take a look at the code of conduct, https://answall.com/conduct.

  • As for your question, take advantage and put the result (some results) and what you are waiting for will return.

1 answer

1

If anyone had that same problem what was missing was adding the field to the search_indexes.py as follows:

full_name = indexes.CharField(model_attr='full_name', indexed=False, stored=True)

That done, I gave one rebuild_index and worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.