As the Adventure Continue
One of the nice thing about django, it have all sort of application. Haystack is such a application. Not just haystack support solr, it also support a few other backends. This include whoosh, and xapian.
The fun part about this, it is quite pleasant to work with. So we redo all of out previous code to work with this.
Let The Game Begin
Again the tutorial is helpful, it pretty much describe what i am have done anyway. And the documentation covers a lot!! So I will skip the setup, and index.
After following the tutorial to setup.And modify the search_indexes.py for each models that we need to find. And generate the solr index, and copy to the solr/conf.
We basically have a django application ready to do search. To test, is a matter of going to shell.
from haystack.query import SearchQuerySet
q = SearchQuerySet().all()
of course, you can also, do
q = SearchQuerySet().filter(fieldname='value')
for i in q:
print i.object.value
It is similar to django queryset api. Except it is for search. You can do a lot more, like faceted search, chain the filter, etc.
Are we There Yet.
Not quite, there yet. Because haystack also have a search page, which you can enable, also covered in the tutorial. You can inherit from the search view, and forms to fit your need.
An example is for a form that I need(it is generic anyway)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from haystack.forms import SearchForm | |
from django import forms | |
from haystack.query import SearchQuerySet | |
SEARCH_FILTER =[('all','all'),('name','name'),('business','sector'),('address','address')] | |
class FilterSearchForm(SearchForm): | |
def __init__(self,*args,**kwargs): | |
super(FilterSearchForm,self).__init__(*args,**kwargs) | |
self.fields['keys'] = forms.ChoiceField(choices=SEARCH_FILTER,required=True, | |
label='search in') | |
def search(self): | |
d = {} | |
sqs = super(FilterSearchForm,self).search() | |
key = str(self.cleaned_data['keys']) | |
data = str(self.cleaned_data['q']) | |
d[key] = data | |
if key == 'all': | |
return sqs | |
else: | |
print d | |
return sqs.filter(**d) |
So you don''t even need to create your own form, and views.
Just import the views, and change the parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls.defaults import * | |
from views import * | |
from haystack.views import SearchView | |
from haystack.forms import SearchForm | |
from forms import FilterSearchForm | |
urlpatterns = patterns('', | |
(r'^$',SearchView(form_class=FilterSearchForm, | |
load_all=False)), | |
) |
haystack is pretty comprehensive, have many stuff done for us, from views, forms that is functional. Doing indexing, generating index for solr, etc. I think this is a nice project to use in django for providing search in your web app
No comments:
Post a Comment