Hi, when I try to display my search results from elasticsearch using pagination, it does not work. I suspect the problem is that my view doesn’t accept GET methods, also maybe the way I call upon the elasticsearch client to get my results. Below is my code. Any help is appreciated!
views.py:
def search(request):
# page_number = request.GET.get('page', 1)
results_per_page = 20 # You can adjust this number
# If this is a POST request we need to process the form data
if request.method == "POST":
if request.method == "POST":
# Create a form instance and populate it with data from the request:
form = SearchForm(request.POST)
# check whether it's valid:
if form.is_valid():
# Get query from form.cleaned_data
query = form.cleaned_data['query']
print(query)
# Start ES client
client = Elasticsearch(ENDPOINT, api_key=API_KEY, ca_certs=CERT)
resp = client.search(index= INDEX, size=100, query={ "match": { "userId": query }})
# If match query has no hits/results, try wildcard query
if(resp['hits']['total']['value'] == 0):
formatted_hits = []
print("No results found")
else:
hits = resp['hits']['hits']
# print(hits)
formatted_hits = []
#Format hits to get desired fields and store them in array
for hit in hits:
formatted_hit = {
'datetime': hit['_source']['datetime'],
'brand': hit['_source']['brand'],
'userId': hit['_source']['userId'],
'role': hit['_source']['role'],
'request': hit['_source']['request'],
}
formatted_hits.append(formatted_hit)
paginator = Paginator(formatted_hits, results_per_page)
page_number = request.GET.get('page', 1)
page_obj = paginator.get_page(page_number)
context = {
'hits': formatted_hits,
'form': form,
'page_obj': page_obj,
'query': query,
'total_hits': len(formatted_hits)
}
# return results html with formatted_hits array and form
return render(request, 'audit_logs/results.html', context)
# if a GET (or any other method) we'll create a blank form
else:
form = SearchForm()
return render(request, "audit_logs/search.html", {"form": form})
templates:
search.html:
<h1>You are searching for</h1>
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search">
</form>
results.html
<h1>You are searching for</h1>
<form action="" method="get">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search">
</form>
<h2>Results:</h2>
<!-- Debug info -->
<p>Debug info:</p>
<ul>
<li>Total hits: {{ total_hits }}</li>
<li>Number of pages: {{ page_obj.paginator.num_pages }}</li>
<li>Current page: {{ page_obj.number }}</li>
<li>Has next page: {{ page_obj.has_next }}</li>
<li>Has previous page: {{ page_obj.has_previous }}</li>
</ul>
{% if hits %}
<table class="result">
<thead>
<tr>
<th>Datetime</th>
<th>Brand</th>
<th>userId</th>
<th>role</th>
<th>request</th>
</tr>
</thead>
{% for hit in page_obj %}
<tbody>
<tr>
<td>{{ hit.datetime }}</td>
<td>{{ hit.brand }}</td>
<td>{{ hit.userId }}</td>
<td>{{ hit.role }}</td>
<td>{{ hit.request }}</td>
</tr>
</tbody>
{% endfor %}
</table>
<!-- Pagination -->
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
{% elif hits is not None %}
<p class="no-results">No results found.</p>
{% else %}
<p>No results found</p>
{% endif %}