Browse Source

WIP: pagination for search results

main
Simon Bowie 2 years ago
parent
commit
1926c3ec63
3 changed files with 45 additions and 4 deletions
  1. +29
    -2
      web/app/search.py
  2. +9
    -2
      web/app/solr.py
  3. +7
    -0
      web/app/templates/search.html

+ 29
- 2
web/app/search.py View File

from . import solr from . import solr
from . import ops from . import ops
import pycountry import pycountry
import math


search = Blueprint('search', __name__) search = Blueprint('search', __name__)


year = request.form.get('year') year = request.form.get('year')
else: else:
year = None year = None
if request.form.get('page') is not None:
page = request.form.get('page')
else:
page = '1'
else: else:
if request.args.get('query') is not None: if request.args.get('query') is not None:
query = request.args.get('query') query = request.args.get('query')
year = request.args.get('year') year = request.args.get('year')
else: else:
year = None year = None
if request.args.get('page') is not None:
page = request.args.get('page')
else:
page = '1'
if (query is None and country is None and year is None): if (query is None and country is None and year is None):
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
else: else:
search_results = solr.query_search(core, sort, query, country, year)
search_results = solr.query_search(core, sort, query, country, year, page)
results = search_results[0] results = search_results[0]
num_found = search_results[1] num_found = search_results[1]
year_facet = search_results[2]['year'] year_facet = search_results[2]['year']
country_full = pycountry.historic_countries.get(alpha_2=country_facet[i]) country_full = pycountry.historic_countries.get(alpha_2=country_facet[i])
if country_full is not None: if country_full is not None:
country_facet[i] = country_full country_facet[i] = country_full
# get total number of records
total_number = solr.get_total_number(core) total_number = solr.get_total_number(core)
return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_facet=country_facet, year_facet=year_facet, query=query, core=core, sort=sort, country=country, year=year)
# work out how many pages to display in the pagination
num_pages = math.ceil(int(num_found) / 10)
raw_pagination = list(range(1, num_pages+1))
if int(page) > 5:
pagination = raw_pagination[int(page)-5:int(page)+4]
#pagination.insert(0,raw_pagination[0])
#pagination.append(raw_pagination[len(raw_pagination)-1])
else:
end = 9-int(page)
pagination = raw_pagination[:int(page)+end]
#pagination.insert(0,raw_pagination[0])
#pagination.append(raw_pagination[len(raw_pagination)-1])
# work out page range for display
page_end = int(page) * 10
page_start = page_end - 9
page_end = page_start + (len(results)-1)
page_range = str(page_start) + '–' + str(page_end)
return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_facet=country_facet, year_facet=year_facet, query=query, core=core, sort=sort, country=country, year=year, page=page, page_range=page_range, pagination=pagination)


# route for id_search page # route for id_search page
@search.route('/search/id/', methods=['GET']) @search.route('/search/id/', methods=['GET'])

+ 9
- 2
web/app/solr.py View File



return output, num_found, facets return output, num_found, facets


def query_search(core, sort, query, country, year):
def query_search(core, sort, query, country, year, page):


# assemble parameters for the query string to Solr # assemble parameters for the query string to Solr
if (sort == 'relevance'): if (sort == 'relevance'):
field = 'year' field = 'year'
year_parameter = '&fq=%7B!term%20f%3D' + field + '%7D' + year year_parameter = '&fq=%7B!term%20f%3D' + field + '%7D' + year


if (page is None or page == 'None'):
page_parameter = ''
else:
start = (int(page) * 10) - 10
start = str(start)
page_parameter = '&start=' + start + '&rows=10'

# assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html # assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&indent=true' + query_parameter + '&wt=json' + sort_parameter + country_parameter + year_parameter + '&facet.field=country&facet.field=year&facet.sort=count&facet.mincount=1&facet=true'
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&indent=true' + query_parameter + '&wt=json' + sort_parameter + country_parameter + year_parameter + page_parameter + '&facet.field=country&facet.field=year&facet.sort=count&facet.mincount=1&facet=true'


output = solr_search(solrurl) output = solr_search(solrurl)



+ 7
- 0
web/app/templates/search.html View File

</div> </div>
</div> </div>


<div class="text-center">
<p>showing {{ page_range }} results of {{ num_found }} found{% if query is not none %} for query '{{ query }}'{% endif %}</p>
{% for page_number in pagination %}
<a {% if page_number|int == page|int %}class="fs-4"{% endif %} href="{{ url_for('search.basic_search', query=query, sort=sort, country=country, year=year, page=page_number) }}">{{ page_number }}</a>
{% endfor %}
</div>

<div class="row p-3"> <div class="row p-3">
<form action="{{ url_for('search.basic_search') }}" method="POST"> <form action="{{ url_for('search.basic_search') }}" method="POST">
<input type="hidden" name="query" value="{{ query }}"> <input type="hidden" name="query" value="{{ query }}">

Loading…
Cancel
Save