首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行rebuild_index时未创建索引

运行rebuild_index时未创建索引
EN

Stack Overflow用户
提问于 2012-07-05 08:48:21
回答 1查看 113关注 0票数 0

我遵循入门指南,但当我运行manage.py rebuild_index时,我的模型似乎都没有索引。

当我尝试在/search中搜索时,我没有得到任何结果,这在日志中是这样的:

代码语言:javascript
复制
Problem accessing /solr/select/. Reason: no field name specified in query and no defaultSearchField defined in schema.xml

这是我的应用程序的search_indexes.py.它比指南的模型要复杂一些,所以也许这就是它不起作用的原因。

代码语言:javascript
复制
import datetime

from myproject.apps.lead import Lead, Opportunity

from haystack import site
from haystack.indexes import *

class LeadIndex(SearchIndex):
    text = CharField(document=True, use_template=True)

    company = CharField(model_attr='company__name')

    contact_name = MultiValueField()
    contact_city = MultiValueField()
    contact_state = MultiValueField()
    contact_postcode = MultiValueField()
    contact_email = MultiValueField()
    contact_phone = MultiValueField()

    product = CharField(model_attr='product__name')
    campaign = CharField(model_attr='campaign__name')

    def index_queryset(self):
        """Used when the entire index for model is updated."""
        return Lead.objects.filter(pub_date__lte=datetime.datetime.now())

    def prepare_contact_name(self, obj):
        return [contact.name for contact in obj.contact_set.order_by('name')]

    def prepare_contact_city(self, obj):
        return [contact.city for contact in obj.contact_set.order_by('city')]

    def prepare_contact_state(self, obj):
        return [contact.state for contact in obj.contact_set.order_by('state')]

    def prepare_contact_postcode(self, obj):
        return [contact.postcode for contact in obj.contact_set.order_by('postcode')]

    def prepare_contact_email(self, obj):
        return [contact.email for contact in obj.contact_set.order_by('email')]

    def prepare_contact_phone(self, obj):
        return [contact.phone for contact in obj.contact_set.order_by('phone')]

site.register(Lead, LeadIndex)

我的/templates/search/indexes/lead/lead_text.txt只是

代码语言:javascript
复制
{{ object.company__name }}

我遗漏了什么?

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-07-05 15:18:40

索引类需要从indexes.SearchIndexindexes.Indexable继承。

代码语言:javascript
复制
class LeadIndex(indexes.SearchIndex, indexes.Indexable):
    ...

请不要使用通配符导入(*).这是非常糟糕的形式,并导致名称空间的污染。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11340817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档