0

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??

Gabriel Muñumel
  • 1,876
  • 6
  • 34
  • 57

1 Answers1

0

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

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

Then, inside a template named search/indexes/myapp/image_rendered.txt:

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

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

And finally, in search/search.html:

...

{% for result in page.object_list %}
    <div class="search_result">
        {{ result.rendered|safe }}
    </div>
{% endfor %}
Thomas
  • 11,757
  • 4
  • 41
  • 57
  • Thanks for your suggestion, I'll check out. I solved the problem not changing Haystack/woosh behavior but my model. Thank you for your answer anyway. – Gabriel Muñumel Feb 20 '12 at 14:52