2012-02-19 28 views
0

我正在使用Haystack和Whoosh構建網站的搜索引擎部分。在我的情況下,Whoosh運行得非常好,但是我需要從我的視圖中顯示額外的信息取決於搜索的結果。使用Whoosh自定義視圖

在我的Django的觀點我用這樣的事情,在虛擬的信息顯示:

dummy = "dummy" 
    return render_to_response('images/ib_large_image.html', {'dummy': dummy}, 
          context_instance=RequestContext(request)) 

所以,基本上我要進行個性化搜索的視圖我的變量顯示在搜索模板。

這裏有一些配置:

設置

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() 

搜索>指標> 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')), 
    ) 

我配置我的看法是這樣,但它不工作:

imageboard > views.py

from haystack.views import SearchView 
    def search(request): 
     return SearchView(template='search.html')(request) 

任何想法??

+0

您可以擴展'它不起作用'嗎? – Spacedman 2012-02-19 21:49:02

+0

嗯,我解決了我的問題。我重構了我的模型,不需要觸摸搜索引擎。不管怎麼說,還是要謝謝你。 – 2012-02-19 21:49:25

回答

0

我建議你看看haystack「StoredFields」。這些存儲您的搜索結果視圖需要在搜索索引中訪問的任何信息。額外的好處是搜索結果視圖永遠不需要擊中數據庫來呈現其內容。此外,你可以爲每個搜索結果的輸出預渲染成一個存儲領域

class ImageIndex(indexes.SearchIndex, indexes.Indexable): 
    rendered = CharField(use_template=True, indexed=False) 

然後,模板命名裏面搜索/索引/ MYAPP/image_rendered.txt:

<h2>{{ object.title }}</h2> 

<p>{{ object.content }}</p> 

最後,在search/search.html中:

... 

{% for result in page.object_list %} 
    <div class="search_result"> 
     {{ result.rendered|safe }} 
    </div> 
{% endfor %} 
+0

感謝您的建議,我會檢查。我解決了不改變Haystack/woosh行爲,但我的模型的問題。無論如何,謝謝你的回答。 – 2012-02-20 14:52:55

相關問題