如何创建简单的搜索引擎? 我有这样的事情:
def search(request):
if 'search' in request.GET:
term = request.GET['search']
if len(term) > 3:
d = Data.objects.filter(Q(content__contains=term) | Q(
desc__contains=term))
count = d.count()
return render_to_response('search_result.html', {'d': d, 'count': count}, context_instance=RequestContext(request))
return render_to_response('search_result.html', context_instance=RequestContext(request))
如果我在模型中搜索但我需要在html内容中搜索(我使用django-chunks),这是可以的
答案 0 :(得分:0)
Django块模型中的数据也是stored。您可以像任何其他模型一样导入并过滤它
class Chunk(models.Model):
"""
A Chunk is a piece of content associated
with a unique key that can be inserted into
any template with the use of a special template
tag
"""
key = models.CharField(_(u'Key'), help_text=_(u"A unique name for this chunk of content"), blank=False, max_length=255, unique=True)
content = models.TextField(_(u'Content'), blank=True)
description = models.CharField(_(u'Description'), blank=True, max_length=64, help_text=_(u"Short Description"))
class Meta:
verbose_name = _(u'chunk')
verbose_name_plural = _(u'chunks')
def __unicode__(self):
return u"%s" % (self.key,)
contains
是一种可以查询是否有极小数据集的方法。它只发出LIKE
查询,因此无法很好地扩展。如果您要获得大量数据,最好寻找专门为全文搜索而创建的引擎。