【发布时间】: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 中设置(
id和uniqueKey)。如果每个文档的id列中有不同的值,则会添加一个新文档。如果存在id字段中具有相同值的文档,则应改为更新它(默认情况下)。因此,请检查id字段中包含的重复文档的内容。
标签: django solr django-haystack