Haystack + solr在更新时重复

时间:2016-03-10 00:42:17

标签: django solr django-haystack

我是haystack / solr的新手,所以这可能是一个新手错误。我正在使用solur和haystack。

当我运行update_index时,它似乎是重复记录。我得到了:

get() returned more than one Doctor -- it returned 3!

这段代码:

self._object = self.searchindex.read_queryset().get(pk=self.pk) 

如果我再次运行update_index,则数字返回值增加1,如果我运行rebuild_index,它将只显示一条记录,直到我再次更新。

因此,似乎update_index正在重复索引中的记录。如何做到这一点?

这是我的haystack搜索索引:

from haystack import indexes
from .models import Doctor, Zipcode
from django.contrib.gis.measure import D
from django.conf import settings

class DoctorIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    name = indexes.EdgeNgramField(model_attr='name')
    specialty = indexes.MultiValueField()
    condition = indexes.MultiValueField()
    procedure = indexes.MultiValueField()
    premium = indexes.BooleanField()
    location = indexes.LocationField(model_attr='main_office__location')

    latitude = indexes.DecimalField(indexed=False)
    longitude = indexes.DecimalField(indexed=False)
    docid = indexes.IntegerField()
    slugify_name = indexes.CharField(indexed=False)
    rendered = indexes.CharField(use_template=True, indexed=False)
    premium_rendered = indexes.CharField(use_template=True, indexed=False)
    include = indexes.BooleanField(indexed=False)

    def get_model(self):
        return Doctor

    def prepare_specialty(self, obj):
        return ["%s %s"%((specialty.parent.name if specialty.parent else ""), specialty.name) for specialty in obj.specialty.all()]

    def prepare_condition(self, obj):
        return [condition.name for condition in obj.conditions.all()]

    def prepare_procedure(self, obj):
        return [procedure.name for procedure in obj.procedures.all()]

    def prepare_premium(self, obj):
        return obj.display()['premium']

    def prepare_latitude(self, obj):
        return obj.main_office.lat

    def prepare_longitude(self, obj):
        return obj.main_office.lon

    def prepare_docid(self,obj):
        return obj.id

    def prepare_slugify_name(self,obj):
        return obj.slugify_name()

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(specialty__search_include=True)

这是我的solr架构:https://gist.github.com/anonymous/5d5b011ca7fa0f3f3e29

我做了很多谷歌搜索,但似乎无法找到答案。

2 个答案:

答案 0 :(得分:2)

所以这个跟踪是很棘手的,但问题实际上在我的index_queryset函数中。

此:

return self.get_model().objects.filter(specialty__search_include=True)

实际应该是这样的:

return self.get_model().objects.filter(specialty__search_include=True).distinct()

该函数在其中有重复并导致我的错误,而不是像我想象的那样的solr架构。专业是ManyToManyField。

答案 1 :(得分:0)

我刚遇到同样的问题。 根据此topic,有必要删除.pyc个文件。在项目内部只需执行下一个(对于Linux):

find . -name "*.pyc" -type f -delete