干草堆的分页问题

时间:2015-05-20 07:43:40

标签: django django-haystack django-pagination

我使用从https://djangosnippets.org/snippets/1364/

借来的干草堆的字母分页

但命名分页正在击中它不应该的数据库。第15行引起了这种情况,虽然我在django模板中有相同的迭代代码,但工作正常。

更新:

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

任何帮助将不胜感激。

感谢。

更新:

{  
    "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"
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我想问题在于索引。您正尝试在字段"brand"上执行分页,并且很可能此字段未在Solr / Elastic搜索中编入索引。

Solr仅对未编制索引的字段执行数据库查询。

希望这可以解决这个问题。