Haystack + solr 更新时重复

Haystack + solr duplicate on update

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

当我运行update_index的时候,好像是在重复记录。我得到:

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

对于这段代码:

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

如果我再次 运行 update_index,数字 return 增加 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

我用谷歌搜索了很多,但似乎找不到答案。

所以这个很难追踪,但问题实际上出在我的 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。

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

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