I have followed multiple different examples multiple different times, but it seems that I am missing something along the line. --> point short, with both whoosh, or elasticsearch along with haystack, search results to nothing.
model
class Note(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField()
def __unicode__(self):
return self.title
form
class NotesSearchForm(SearchForm):
def no_query_found(self):
return self.searchqueryset.all()
def search(self):
# First, store the SearchQuerySet received from other processing. (the main work is run internally by Haystack here).
sqs = super(NotesSearchForm, self).search()
# if something goes wrong
if not self.is_valid():
return self.no_query_found()
# you can then adjust the search results and ask for instance to order the results by title
sqs = sqs.order_by(title)
return sqs
Views
def search(request):
# we retrieve the query to display it in the template
form = NotesSearchForm(request.GET)
# we call the search method from the NotesSearchForm. Haystack do the work!
results = form.search()
return render(request, 'search.html', {'search_query' : search_query,
'notes' : results,
})
template
<!-- EXTENDS BASE
================================================== -->
{% extends 'base.html' %}
<!-- NAVBAR
================================================== -->
{% block nav %}
{% include 'nav1.html' %}
{% endblock %}
<!-- Marketing messaging and featurettes
================================================== -->
<!-- Wrap the rest of the page in another container to center all the content. -->
{% block marketing %}
<div class="container marketing">
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<h2>Search</h2>
<form type="get" action="/search/">
<input type="text" name="q">
<button type="submit">Search</button>
</form>
<p>Your query "{{ search_query }} has returned {{ notes.count }} result{{ notes|pluralize }}"</p>
{% for note in notes %}
<h1>{{ note.title }}</h1>
<p>
{{ note.body }}
</p>
{% endfor %}
</div>
</div>
<hr class="featurette-divider">
<!-- /END THE FEATURETTES -->
</div><!-- /.container -->
{% endblock %}
I know that this is a bit long, but I cant seem to figure out what is going wrong. the search page is working, but when text is searched no result is found.
I want to enter a 'search_text' in the text form and for it to return all instances in which the 'search_text' appears in the documents in the database with a certain amount of tokens before and after as such
| title | user | {number of tokens preceding } 'search_text' {number of tokens following}
The reason as to why it was not working is that I did not have elasticsearch running on my machin. Therefore, by installing and running elasticsearch server on the machin, I was able to have search results. Instructions can be found here : http://chriskief.com/2014/05/14/django-haystack-and-elasticsearch-part-1/
additionaly pyelasticsearch and elasticsearch is needed to run the search.