Why doesn't sorting the search results in django-haystack?

0 like 0 dislike
Good afternoon. To sort the search results by date - newest first, then more old news.
In Django is model News:
# news/models.py class Post(ContentNode, ContentTypeBase): title = models.CharField(max_length=255, verbose_name='Title') BLOCK_NAME_CHOICES = ( (0, u Not in blocks on main page'), (1, u 'Block 1'), (2, u Block 2'), (3, u Block 3'), ) block_name = models.IntegerField(verbose_name='Unit on the main page', choices=BLOCK_NAME_CHOICES, default=0) block_image = models.ImageField(upload_to='news/', verbose_name='Image block', blank=True, null=True) block_text = models.CharField(max_length=25, verbose_name='the Text for the unit (FRIEZE)', blank=True, null=True) block_text_2 = models.CharField(max_length=25, verbose_name='Text 2nd line (FRIEZE)', blank=True, null=True) date = models.DateField(verbose_name='date', db_index=True) update_time = models.DateTimeField(verbose_name='date updated', auto_now=True) region = models.ForeignKey(Region, verbose_name='Region', blank=True, null=True) main_image_file = models.ImageField(upload_to='news/', verbose_name='Primary image', blank=True, null=True) main_image_url = models.CharField(max_length=255, verbose_name='Link to main image', blank=True, null=True) gallery = PluploadGalleryField(verbose_name='Additional images', blank=True, null=True) text = RichTextField(verbose_name='Text') no_persons = models.BooleanField(verbose_name='No person', default=False) mentioned_persons = ManyToManyFieldCheckbox(Person, verbose_name='Referred person', blank=True, null=True, related_name='news_mentions') tags = ManyToManyFieldCheckbox(Tags, verbose_name='Tags', blank=True, null=True, related_name='news_set') post_to_twitter = models.BooleanField(verbose_name='share link to Twitter', default=True) posted_to_twitter = models.BooleanField(editable=False, default=False) post_to_facebook = PostToFacebookModelField(verbose_name='share link to Facebook', default=True) posted_to_facebook = models.BooleanField(editable=False, default=False) default_template_path = 'news/post.html' section_node_id = 7 objects = ContentTypeManager() published_objects = ContentTypePublishedManager() objects_with_deleted = models.Manager() def __unicode__(self): return self.title def get_update_time(self): return self.update_time def get_block_name(self): return self.block_name def get_template_name(self): if settings.IS_MOBILE: return 'mobile/news/post.html' else: return super(Post, self).get_template_name() def get_seo_title(self): if self.meta_title: return self.meta_title else: return u'{title}'.format(title=self.__unicode__()) def get_seo_description(self): if self.meta_description: return self.meta_description else: return u'{title} - see on TV the Union.'.format(title=self.__unicode__()) def get_seo_keywords(self): if self.meta_keywords: return self.meta_keywords else: result = self.get_seo_title() signs = ['.',',',':',';'] for sign in signs: result = result.replace(sign, ' ') return result def generate_human_readable_url(self): return clean_friendly_url('/news/' + strip_chars(self.title)) def get_gallery(self): return self.gallery.all() def has_video(self): ""Returns True if there is a news video"" if '{flvremote}' in self.text or '{youtube}' in self.text: return True else: return False def get_main_image_url(self): current_site = Site.objects.get_current() if self.main_image_url: if self.main_image_url.startswith('http://'): return self.main_image_url else: return u http://{0}{1}'.format(current_site.domain self.main_image_url) elif self.main_image_file: return u http://{0}{1}{2}'.format(current_site.domain self.main_image_url, self.main_image_file.url) else: return u http://{0}{1}'.format(current_site.domain settings.MAIN_IMAGE_PLACEHOLDER_URL) def get_related_news(self, limit=4, offset=0): if limit != 0 or offset != 0: news = Post.published_objects.filter(date=self.date).exclude(pk=self.pk)[offset:limit+offset] else: news = Post.published_objects.filter(date=self.date).exclude(pk=self.pk) return news def get_related_news_count(self): return self.get_related_news(limit=0, offset=0).count() class Meta: verbose_name = 'News' verbose_name_plural = 'news' ordering = ('-date') permissions = ( ('can_edit_foreign_posts', 'Can see and edit other people's entries'), )

File describing the search indexes:
# news/search_indexes.py from haystack import indexes from news.models import Post class NewsIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.EdgeNgramField(document=True, use_template=True) date = indexes.DateTimeField(model_attr='date') def get_model(self): return Post def index_queryset(self, using=None): return self.get_model().published_objects.all()

Published_manager is described as:
class ContentTypePublishedManager(models.Manager): def get_query_set(self): qs = super(ContentTypePublishedManager, self).get_query_set().filter( is_published=True, is_service=False, is_deleted=False ) return qs

Template templates/search/indexes/news/post_text.txt:
{{ object.title }}
{{ object.category.title }}
{{ object.text }}
{% for tag in object.tags.all %}
{{ tag.title }}
{% endfor %}

Search view:
# search/views.py from django.template import RequestContext from django.shortcuts import render_to_response from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from search.forms import SearchForm from soyuz_tv.utils import convert_from_en_to_ru_keyboard_layout def search(request, node): per_page = 20 context = RequestContext(request) context['node'] = node context['content_object'] = node search_form = SearchForm(request.GET) if search_form.is_valid(): search_term = search_form.cleaned_data['q'].replace('"', ") context['search_term'] = search_term search_results = search_form.search() if not search_results.count(): # Maybe the query was typed in wrong layout, try to look for another layout translated_query = convert_from_en_to_ru_keyboard_layout( search_term ) translated_query = translated_query search_results_trans = search_form.searchqueryset.auto_query(translated_query) if search_results_trans.count(): context['suggested_query'] = translated_query search_results = search_results_trans paginated_search_results = Paginator(search_results, per_page) page = request.GET.get('page') try: search_results = paginated_search_results.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. search_results = paginated_search_results.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. search_results = paginated_search_results.page(paginated_search_results.num_pages) context['search_results'] = search_results context['paginator'] = paginated_search_results context['pagination_url_prefix'] = request.path + '?q=' + search_form.cleaned_data['q'] template_path = node.get_template_name() return render_to_response(template_path, context)

And search form:
# search/forms.py from haystack.forms import SearchForm as HaystackSearchForm class SearchForm(HaystackSearchForm): def clean_q(self): q = self.cleaned_data.get('q') q = q.replace('"', ") return q

Despite the fact that in the model of Post news lists the order of the results to the ordering = ('-date'), in the search results, the news issued in a chaotic manner, changing the code in search view:
def index_queryset(self, using=None): return self.get_model().published_objects.all().order_by('-date')

also, the issuance of news has not changed.
by | 5 views

1 Answer

0 like 0 dislike
Haystack returns the objects from the search index and not from the database. In the order in which they were returned used the back end of search (like ElasticSearch). Use order_by to search results. index_queryset is generally used only when full indexing. Read the documentation on packages that apply, there are simple moments exactly considered.

You have coded a lot of problems. For example, this:
return u'{title}'.format(title=self.__unicode__())
equivalent to:
return self.__unicode__()
return unicode(self)

Only the last two options are read easier, and in General, it is not clear why such complexity. This level is a = 1 to write setattr(__builtin__, 'a', 1).

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked Mar 26, 2019 by Dark_Knight
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 11, 2019 by V-ampre
0 like 0 dislike
1 answer
asked May 20, 2019 by Dev_nab
110,608 questions
257,186 answers
28,659 users