-1

I am using alphabetical pagination with haystack that I borrowed from https://djangosnippets.org/snippets/1364/

But named pagination is hitting the database which it shouldn't. Line 15 is causing this, although I have same iteration code in a django template that that works fine.

UPDATED:

search_view.py

import json
from django.core import serializers

from django.core.paginator import InvalidPage, Paginator
from django.http import Http404, HttpResponse
from django.shortcuts import redirect, render_to_response

from haystack.query import SearchQuerySet
from haystack.views import FacetedSearchView

from directory.named_paginator import NamedPaginator


class FacetedSearchCustomView(FacetedSearchView):
"""
Overrides various default methods to allow for additional context, smoother UX for faceting
"""

def build_page(self):
    """
    Paginates the results appropriately.

    Overriden to redirect to page 1 if a page_no is not found
    """
    try:
        page_no = int(self.request.GET.get('page', 1))
    except (TypeError, ValueError):
        raise Http404("Not a valid number for page.")

    if page_no < 1:
        raise Http404("Pages should be 1 or greater.")

    paginator = NamedPaginator(self.results, on="brand", per_page=self.results_per_page)
    # import pdb; pdb.set_trace()
    try:
        page = paginator.page(page_no)
    except InvalidPage:
        # Redirect to page 1 of the
        path = self.request.path
        qs = self.request.GET.copy()
        qs['page'] = 1
        url = '%s?%s' % (path, qs.urlencode())
        return redirect(url)

    return paginator, page

def clean_filters(self):
    """Returns a list of tuples (filter, value) of applied facets"""
    filters = []
    # get distinct facets
    facets = list(set(self.form.selected_facets))
    for facet in facets:
        if ":" not in facet:
            continue
        field, value = facet.split(":", 1)
        field = field.replace('_', ' ').replace('exact', '').title()
        filters.append((field, value))
    return filters

def create_response(self):
    """
    Generates the actual HttpResponse to send back to the user.

    Overriding to allow the redirect to pass through from overriden build_page
    """
    try:
        (paginator, page) = self.build_page()
    except ValueError:
        return self.build_page()

    context = {
        'query': self.query,
        'form': self.form,
        'page': page,
        'paginator': paginator,
        'suggestion': None,
    }

    if self.results and hasattr(self.results, 'query') and self.results.query.backend.include_spelling:
        context['suggestion'] = self.form.get_suggestion()

    context.update(self.extra_context())
    return render_to_response(self.template, context, context_instance=self.context_class(self.request))

def extra_context(self):
    extra = super(FacetedSearchCustomView, self).extra_context()
    extra['filters'] = self.clean_filters()
    if not self.results:
        extra['facets'] = self.form.search().facet_counts()
    else:
        extra['facets'] = self.results.facet_counts()

    model_type = self.request.path.split('/')[1].rstrip('s')
    extra['model_type'] = None if model_type == "search" else model_type

Any help will be much appreciated.

Thanks.

UPDATED:

{  
    "store":{  
        "mappings":{  
            "modelresult":{  
                "_boost":{  
                    "name":"boost",
                    "null_value":1
                },
                "properties":{  
                    "brand":{  
                        "type":"string",
                        "analyzer":"synonym_analyzer"
                    },
                    "brand_auto":{  
                        "type":"string",
                        "analyzer":"edgengram_analyzer"
                    },
                    "brand_slug":{  
                        "type":"string",
                        "analyzer":"synonym_analyzer"
                    },
                    "brand_sort":{  
                        "type":"string",
                        "analyzer":"synonym_analyzer"
                    }
                }
            }
        }
    }
}
Chirdeep Tomar
  • 4,281
  • 8
  • 37
  • 66

1 Answers1

0

I guess the issue is with the indexing. You are trying to perform the pagination on the field "brand" and it is most likely that this field is not indexed in Solr/Elastic search.

Solr performs a DB query only for the fields which are not indexed.

Hope this sorts out the issue.

Animesh Sharma
  • 3,258
  • 1
  • 17
  • 33