Commit f306e5e7 authored by Archerfou's avatar Archerfou

Implementation autocomplete search on representatives full name using solr

parent 72a829b0
Pipeline #993 failed with stage
in 9 minutes and 30 seconds
import datetime
from haystack import indexes
from representatives.models import Representative
class RepresentativeIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
slug = indexes.CharField(model_attr='slug', faceted=True)
first_name = indexes.CharField(model_attr='first_name', faceted=True)
last_name = indexes.CharField(model_attr='last_name', faceted=True)
full_name = indexes.EdgeNgramField(model_attr='full_name')
ascii_name = indexes.NgramField(model_attr='ascii_name')
def get_model(self):
return Representative
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
......@@ -58,6 +58,7 @@ INSTALLED_APPS = (
'fontawesome',
'rest_framework',
'taggit',
'haystack',
# memopol apps
'core',
......@@ -341,3 +342,13 @@ if os.path.exists(RAVEN_FILE):
with open(RAVEN_FILE, 'r') as f:
RAVEN_CONFIG = {'dsn': f.read().strip()}
# Haystack with Solr config
DJANGO_HAYSTACK_ENABLE = True
if DJANGO_HAYSTACK_ENABLE:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': 'http://127.0.0.1:8080/solr',
},
}
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -20,6 +20,8 @@
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type="text/css" />
<link rel="stylesheet" href="{% static 'libs/flag-icon-css/css/flag-icon.min.css' %}" type="text/css" />
<link rel="stylesheet" href="{% static 'css/jquery-ui.min.css' %}" type="text/css" />
{% load compress %}
{% compress css %}
......@@ -28,6 +30,7 @@
{% endcompress %}
<script type="text/javascript" src="{% static 'libs/jquery/dist/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-ui.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
{{ position_form.media }}
......@@ -86,5 +89,16 @@
$('[data-toggle="tooltip"]').tooltip()
})
</script>
<script>
$(function() {
$("#search").autocomplete({
source: "{% url 'representative-search-name-autocomplete' %}",
minLength: 2,
select: function(event, ui){
window.location.href = ui.item.link;
}
});
});
</script>
</body>
</html>
{{ object.id }}
{{ object.full_name }}
{{ object.slug }}$
{{ object.ascii_name}}
......@@ -26,6 +26,7 @@ from views.representative_detail_votes import RepresentativeDetailVotes
from views.representative_detail_mandates import RepresentativeDetailMandates
from views.representative_detail_positions import RepresentativeDetailPositions
from views.representative_list import RepresentativeList
from views.representative_search import search_autocomplete
from views.redirects import (
RedirectRepresentativeDetail,
......@@ -241,5 +242,10 @@ urlpatterns = [
ThemeDetailBase.as_view(),
name='theme-none'
),
url(
r'^representative/search/$',
search_autocomplete,
name="representative-search-name-autocomplete"
),
] + legacy_patterns
import unicodedata
def strip_accents(value):
return ''.join(c for c in unicodedata.normalize('NFD', value)
if unicodedata.category(c) != 'Mn')
......@@ -21,7 +21,6 @@ class RepresentativeAutocomplete(autocomplete.Select2QuerySetView):
return qs
class ProposalAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Proposal.objects.all()
......
import json
from django.http import HttpResponse
from haystack.query import SearchQuerySet
from django.core.urlresolvers import reverse
from memopol.utils import strip_accents
def search_autocomplete(request):
if request.is_ajax():
q = strip_accents(request.GET.get('term', ''))
print q
if q is not None:
json_results = []
sqs = SearchQuerySet().autocomplete(ascii_name=q)
for result in sqs:
result_json = {}
result_json['id'] = result.id
result_json['label'] = result.full_name
result_json['value'] = result.full_name
result_json['link'] = reverse('representative-detail', kwargs={'slug': result.slug})
json_results.append(result_json)
print result.ascii_name
data = json.dumps(json_results)
else:
data = "Fail"
return HttpResponse(data, "application/json")
......@@ -5,7 +5,7 @@ from datetime import datetime
from django.db import models
from django.utils.encoding import smart_unicode
from django.utils.functional import cached_property
from memopol.utils import strip_accents
class TimeStampedModel(models.Model):
"""
......@@ -76,6 +76,9 @@ class Representative(TimeStampedModel):
genders = {0: 'N/A', 1: 'F', 2: 'M'}
return genders[self.gender]
def ascii_name(self):
return strip_accents(self.full_name).lower()
class Meta:
ordering = ['last_name', 'first_name']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment