Haystack按IntegerField

时间:2016-04-28 14:50:32

标签: python django django-haystack whoosh

我正在与haystack + Whoosh和DJango合作建立一个基本的搜索引擎。我想要做的是我的每个搜索结果都会有一个"收藏夹"值(默认为0)。我希望按这个整数对这些结果进行排序,结果是收藏夹数量较多。我已经按照Haystack教程的指南进行了操作,并查找了有关该主题的其他答案,但我似乎无法让它工作。

我的models.py看起来像这样:

class Recipe(models.Model):
primary_url = models.CharField(max_length=500, null=False, blank=False, primary_key=True)
title = models.CharField(max_length=500)

favourite_count = models.IntegerField(null=False, default= 0)

def __unicode__(self):
    return self.title

在我的search_indexes.py中,我给了“收藏夹”属性这样的名字:

class RecipeIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    favourite_count = indexes.IntegerField(model_attr='favourite_count', stored=True)

    def get_model(self):
        return Recipe

    def index_queryset(self, using=None):
        #Used when entire index for model is updated
        return self.get_model().objects.all()

最后我的urls.py设置如下:

sqs = SearchQuerySet().order_by('-favourite_count')

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^search/', search_view_factory(view_class=views.mySearchView, template='search/search.html', searchqueryset=sqs, form_class=views.myModelSearchForm))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

尽管如此,我的结果仍未正确排序。尽管我努力,它们仍然是随机的。我知道urls.py正在理解我对sqs的声明,因为如果我拼写错误就会抛出错误:

KeyError: "No field named 'favrite_count'"

修改

这是views.mySearchView()

class mySearchView(SearchView):

def __call__(self, request):
    """
    Generates the actual response to the search.

    Relies on internal, overridable methods to construct the response.
    """
    self.request = request

    self.form = self.build_form()

    #print(self.get_query())

    querywords = self.get_query().split()

    resultwords = [word for word in querywords if word.lower() not in stopwords]

    result = ' '.join(resultwords)

    #print(result)

    self.query = result
    self.results = self.get_results()

    return self.create_response()

def get_results(self):
    """
    Fetches the results via the form.

    Returns an empty list if there's no query to search with.
    """

    #sortedQuerySet = sorted(self.form.search(), key=token_similarity)

    #return sortedQuerySet
    return self.form.search()

def extra_context(self):

    request = self.get_query()

    if request is not None and not "":
        related_recipes = []


        words = request.split(' ')

        if(len(words) > 1):

            for word in words:
                temp_list = SearchQuerySet().auto_query(word)[:8]
                for obj in temp_list:
                    if str(self.query).lower() not in str(obj.object.title).lower():
                        #print(str(self.query).lower()+" is not in "+ str(obj.object.title).lower())
                        #print('here')
                        if obj.object.primary_image_url != "test_url":
                            related_recipes.append(obj)
                if len(related_recipes) >= 8:
                            break

        #print(related_recipes)

        return {'related': related_recipes}

    else:

        return {}

编辑2:

按原样取得我的结果,按顺序排列它们的顺序:

sqs = SearchQuerySet().order_by('-favourite_count')
第一页的

是这样的:

15
251
10
21
0
15
14
0
0
0
5
8
...

将其更改为sqs = SearchQuerySet().order_by('favourite_count')时会返回:

0
0
25
7
0
0
0
0
0
0
2
0
7
0
....

同样,我正在排序的字段是IntegerField。除非我遗漏了某些东西,否则按简单数字排序不应该这么困难。

0 个答案:

没有答案