我已经升级了haystack,但是我遇到了使用django_ct
的问题我已经使用python manage.py update_index movies
索引了电影的模型但是当我尝试使用2
result = SearchQuerySet().raw_search('django_ct:movies.movie')
result = SearchQuerySet().filter(django_ct='movies.movie')
我没有结果
>>> from haystack.query import SearchQuerySet
>>> result = SearchQuerySet().filter(django_ct='movies.movie')
>>> len(result.all())
0
>>> len(result)
0
>>> result = SearchQuerySet().raw_search('django_ct:movies.movie')
>>> len(result)
0
>>> len(result.all())
0
>>> all_result = SearchQuerySet()
>>> len(all_result)
10924
>>>
电影的索引是
import datetime
from django.db.models import Q
from haystack import indexes
from search.base_search_index import BaseSearchIndex
from search.fields import MultiValueDateField
from movies.models import Movie, Show
class MovieIndex(BaseSearchIndex, indexes.Indexable):
dates = MultiValueDateField(null=True,)
areas = indexes.MultiValueField(null=True)
categories = indexes.MultiValueField(null=True)
venues = indexes.MultiValueField(null=True)
hot = indexes.BooleanField(model_attr='is_hot')
showing = indexes.BooleanField(model_attr='now_showing')
sortorder = indexes.IntegerField(model_attr='sortorder')
name = indexes.CharField(model_attr='name')
def prepare_areas(self, obj):
shows = obj.shows.all()
areas = []
if shows:
for s in shows:
if s.venue and s.venue.area_id:
areas.append(s.venue.area_id)
return self.get_list_for_multivalue_field(areas)
def prepare_venues(self, obj):
shows = obj.shows.all()
if shows:
return [show.venue_id for show in shows]
return None
def prepare_categories(self, obj):
cats = []
for category in obj.categories.all():
cats.extend([c.id for c in category.get_ancestors(include_self=True)])
return self.get_list_for_multivalue_field(cats)
def prepare_dates(self, obj):
dates = [d for d in obj.start_dates()]
return dates
def get_model(self):
return Movie
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(
Q(visible=True) &
Q(publish__lte=datetime.date.today()) &
(Q(expires__isnull=True) | Q(expires__gte=datetime.date.today())) &
Q(shows__starts__gte=datetime.date.today()),
).distinct()
class ShowIndex(BaseSearchIndex, indexes.Indexable):
movie = indexes.IntegerField(null=True)
venue = indexes.IntegerField(null=True)
starts = indexes.DateField(model_attr='starts')
area = indexes.IntegerField(null=True,)
categories = indexes.MultiValueField(null=True)
hot = indexes.BooleanField()
i = indexes.IntegerField(model_attr='movie__pk')
shows = indexes.IntegerField(model_attr='pk')
def prepare_movie(self, obj):
if obj.movie_id:
return obj.movie_id
return -1
def prepare_venue(self, obj):
if obj.venue_id:
return obj.venue_id
return -1
def prepare_area(self, obj):
if obj.venue.area_id:
return obj.venue.area_id
return -1
def prepare_categories(self, obj):
cats = []
for category in obj.movie.categories.all():
cats.extend([c.id for c in category.get_ancestors(include_self=True)])
return self.get_list_for_multivalue_field(cats)
def prepare_hot(self, obj):
return obj.movie.is_hot()
def prepare_slug(self, obj):
return u''
def prepare(self, obj):
today = datetime.datetime.now()
data = super(BaseSearchIndex, self).prepare(obj)
data['boost'] = 1
return data
def get_model(self):
return Show
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(starts__gte=datetime.date.today()).distinct()
它是继承
import datetime
from django.db.models import Q
from haystack import indexes
class BaseSearchIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, use_template=True)
html = indexes.CharField(use_template=True, indexed=False)
json = indexes.CharField(use_template=True, indexed=False)
slug = indexes.CharField()
#pk_xapian = indexes.IntegerField(model_attr='pk')
i = indexes.IntegerField(model_attr='pk')
def prepare_slug(self, obj):
return u'unique%sunique' % obj.slug
def get_updated_field(self):
return 'modified'
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
today = datetime.date.today()
return self.get_model().objects.filter(
Q(visible=True) &
Q(publish__lte=today) &
(Q(expires__isnull=True) | Q(expires__gte=today))
).distinct()
def prepare(self, obj):
today = datetime.datetime.now()
data = super(BaseSearchIndex, self).prepare(obj)
weeks = (today - obj.created).days / 7 + 1
data['boost'] = 1 + (1.0 / weeks)
return data
def get_list_for_multivalue_field(self, value):
if len(value) == 0:
return []
elif len(value) == 1:
return [value[0],]
return value
搜索有效,但用于获取应用程序的json数据的逻辑具有searchqueryset的逻辑和每个模型的过滤器,并且不返回任何内容。