我想要做類似這樣的東西我創建了一個名爲應用程序搜索
搜索/ models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
class SearchTerm(models.Model):
query = models.CharField(
verbose_name = _(u'Search Term'),
max_length = 255,
default = None
)
class SearchQuery(models.Model):
term = models.ForeignKey(
SearchTerm
)
user = models.ForeignKey(
User,
blank = True,
null = True,
)
when = models.DateTimeField(
verbose_name = _(u'Date Searched'),
)
搜索/ urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^search/', 'search.views.search', name="search"),
)
那當然在我的項目的URL添加以下規則:
url(r'^', include('search.urls')),
搜索/views.py
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render, redirect
from haystack.forms import ModelSearchForm, SearchForm
from haystack.views import SearchView
from search.models import SearchQuery, SearchTerm
import datetime
def search(request):
if 'page' not in request.GET :
try :
st, created = SearchTerm.objects.get_or_create(query=request.GET['q'])
sq = SearchQuery()
sq.term = st
sq.user = request.user
sq.when = datetime.datetime.now()
sq.save()
except :
pass
view = SearchView()
return view(request)
if 'page' not in request.GET
因此只有在搜索結果的第一頁上,一個SEARCHQUERY保存
每個SEARCHQUERY是在DB獨特。
SearchTerm的每個SearchQuery都會記錄其他信息,例如user
(可爲空)和when
該術語被搜索。
view = SearchView()
和return view(request)
使得它記錄了SearchQuery後,會爲該請求調用默認的乾草堆視圖。
你在用什麼搜索引擎?也許它會更聰明地監視引擎本身? http://wiki.apache.org/solr/SolrLogging否則我認爲擴展SearchView將是最好的舉動。 – fsw 2013-03-03 10:41:52