Permalink
Browse files

Added FTS on plugins and tags, with partial string match

  • Loading branch information...
1 parent eb3dda8 commit c29fc0565886e5687d649e4ea0bbf39b5adc19a8 @elpaso elpaso committed Oct 6, 2012
@@ -0,0 +1,32 @@
+# Custom haystack search to match partial strings
+
+from haystack.views import SearchView
+from haystack.query import SearchQuerySet
+from django.conf.urls.defaults import *
+
+class SearchWithRequest(SearchView):
+
+ __name__ = 'SearchWithRequest'
+
+ def build_form(self, form_kwargs=None):
+ if form_kwargs is None:
+ form_kwargs = {}
+
+ if self.searchqueryset is None:
+ sqs1 = SearchQuerySet().filter(description_auto=self.request.GET.get('q', ''))
+ sqs2 = SearchQuerySet().filter(name_auto=self.request.GET.get('q', ''))
+ sqs3 = SearchQuerySet().filter(text=self.request.GET.get('q', ''))
+ form_kwargs['searchqueryset'] = sqs1 | sqs2 | sqs3
+
+ return super(SearchWithRequest, self).build_form(form_kwargs)
+
+ def get_results(self):
+ """
+ Fetches the results
+ """
+ return self.form.searchqueryset
+
+
+urlpatterns = patterns('haystack.views',
+ url(r'^$', SearchWithRequest(load_all=False), name='haystack_search'),
+)
@@ -4,14 +4,17 @@
from plugins.models import Plugin
-class PluginIndex(SearchIndex):
+class PluginIndex(RealTimeSearchIndex):
text = CharField(document=True, use_template=True)
created_by = CharField(model_attr='created_by')
created_on = DateTimeField(model_attr='created_on')
-
+ # We add this for autocomplete.
+ name_auto = NgramField(model_attr='name')
+ description_auto = NgramField(model_attr='description')
+
def index_queryset(self):
"""Only search in approved plugins."""
- return Plugin.objects.approved()
+ return Plugin.approved_objects.all()
-site.register(Plugin, PluginIndex)
+site.register(Plugin, PluginIndex)
@@ -1,4 +1,5 @@
{{ object.name }}
{{ object.description }}
{{ object.created_by.username }}
-{% for tag in object.tag.all %}{{ tag }}{% endfor %}
+{{ object.author }}
+{% for tag in object.tags.all %}{{ tag }} {% endfor %}
View
@@ -32,13 +32,14 @@
(r'^bookmarks/', include('cab.urls.bookmarks')),
(r'^languages/', include('cab.urls.languages')),
(r'^popular/', include('cab.urls.popular')),
- (r'^search/', include('haystack.urls')),
+ #(r'^search/', include('haystack.urls')),
+ (r'^search/', include('custom_haystack_urls')),
# SAM: qgis-users app
(r'^community-map/', include('users.urls')),
# Tim: Feedjack feed aggregator / planet
(r'^planet/', include('feedjack.urls')),
-
+ # ABP: autosuggest for tags
(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
)

0 comments on commit c29fc05

Please sign in to comment.