使用 Whoosh 自定义视图
我正在使用 Haystack 和 Whoosh 构建网站的搜索引擎部分。 Whoosh 在我的情况下效果很好,但我需要从我的角度显示额外的信息,具体取决于搜索发现的内容。
在我的 Django 视图中,我使用类似这样的东西,其中虚拟是要显示的信息:
dummy = "dummy"
return render_to_response('images/ib_large_image.html', {'dummy': dummy},
context_instance=RequestContext(request))
所以,基本上我想个性化搜索视图以将我的变量显示到搜索模板中。
以下是一些配置:
settings:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
'DEFAULT_OPERATOR': 'AND',
'SITECONF': 'search_sites',
'SEARCH_RESULTS_PER_PAGE': 20
},
}
search_sites.py:
import haystack
haystack.autodiscover()
search >指数>图像板> image_text.txt:
{{ object.name }}
{{ object.description }}
图像板> search_indexes.py:
import datetime
from haystack import indexes
from imageboard.models import Image
class ImageIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Image
def index_queryset(self):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(uploaded_date__lte=datetime.datetime.now())
图像板> urls.py:
urlpatterns = patterns('imageboard.views',
(r'^search/', include('haystack.urls')),
)
我像这样配置了我的视图,但它不起作用:
imageboard > views.py:
from haystack.views import SearchView
def search(request):
return SearchView(template='search.html')(request)
有什么想法吗?
I'm using Haystack and Whoosh to build the search engine part of a site. Whoosh works very well in my case but i need to show an extra information from my view depends on what the search found.
In my Django view i use something like this, where dummy is the information to show:
dummy = "dummy"
return render_to_response('images/ib_large_image.html', {'dummy': dummy},
context_instance=RequestContext(request))
So, basically i want to personalize the view of the search to show my variable into the search template.
Here are some configuration:
settings:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
'DEFAULT_OPERATOR': 'AND',
'SITECONF': 'search_sites',
'SEARCH_RESULTS_PER_PAGE': 20
},
}
search_sites.py:
import haystack
haystack.autodiscover()
search > indexes > imageboard > image_text.txt:
{{ object.name }}
{{ object.description }}
imageboard > search_indexes.py:
import datetime
from haystack import indexes
from imageboard.models import Image
class ImageIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Image
def index_queryset(self):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(uploaded_date__lte=datetime.datetime.now())
imageboard > urls.py:
urlpatterns = patterns('imageboard.views',
(r'^search/', include('haystack.urls')),
)
I configured my view like this, but it doesn't work:
imageboard > views.py:
from haystack.views import SearchView
def search(request):
return SearchView(template='search.html')(request)
Any idea??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你看看 haystack "StoredFields”。它们存储搜索结果视图需要在搜索索引中访问的任何信息。额外的好处是搜索结果视图永远不需要访问数据库来呈现其内容。此外,您可以将每个搜索结果的输出预渲染到存储字段中
然后,在名为 search/indexes/myapp/image_rendered.txt 的模板中:
最后,在 search/search.html 中:
I suggest you take a look at haystack "StoredFields". These store any information that your search result view needs access to in the search index. The extra benefit is that search result views never need to hit the DB in order to render their contents. In addition, you can pre-render the output for each search result into a stored field
Then, inside a template named search/indexes/myapp/image_rendered.txt:
And finally, in search/search.html: