【问题标题】:Haystack + solr duplicate on updateHaystack + solr 更新时重复
【发布时间】:2016-03-10 00:42:16
【问题描述】:

我是 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,返回的数字会增加一,如果我运行rebuild_index,它将只显示一条记录,直到我再次更新。

因此,似乎 update_index 正在复制索引中的记录。我如何从不这样做中得到它?

这是我的干草堆搜索索引:

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

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

【问题讨论】:

  • 索引中的id字段是否有相同的值?是否添加了任何 id 字段?这似乎是 uniqueKey 字段根本不是唯一的问题..
  • @MatsLindh uniquekey 字段设置在哪里?我认为它使用了我的 Doctor 对象的 id。如果需要,我没有手动设置 id。它在哪里设置? (对不起。我是 haystack/solr 的新手)
  • 它在您的 schema.xml 中设置(iduniqueKey)。如果每个文档的 id 列中有不同的值,则会添加一个新文档。如果存在 id 字段中具有相同值的文档,则应改为更新它(默认情况下)。因此,请检查 id 字段中包含的重复文档的内容。

标签: django solr django-haystack


【解决方案1】:

所以这个很难追踪,但问题实际上出在我的 index_queryset 函数中。

这个:

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

实际上应该是这样的:

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

该函数中有重复项并导致我的错误,而不是我想象的 solr 架构。 Specialty 是一个ManyToManyField。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多