Browse Source

added year search function

solr_update
Simon Bowie 2 years ago
parent
commit
8d98156adf
4 changed files with 36 additions and 5 deletions
  1. +27
    -1
      web/app/search.py
  2. +3
    -3
      web/app/solr.py
  3. +3
    -1
      web/app/templates/data.html
  4. +3
    -0
      web/app/templates/search.html

+ 27
- 1
web/app/search.py View File

core = 'all' core = 'all'
if sort is None: if sort is None:
sort = 'relevance' sort = 'relevance'
search_results = solr.country_search(core, sort, country_code)
field = 'country'
search_results = solr.term_search(core, sort, field, country_code)
results = search_results[0] results = search_results[0]
num_found = search_results[1] num_found = search_results[1]
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_code=country_code, core=core, sort=sort) return render_template('search.html', results=results, num_found=num_found, total_number=total_number, country_code=country_code, core=core, sort=sort)

# route for year search page
@search.route('/search/year/', methods=['GET', 'POST'])
def year_search():
if request.method == 'POST':
year = request.form.get('year')
core = request.form.get('core')
sort = request.form.get('sort')
else:
year = request.args.get('year')
core = request.args.get('core')
sort = request.args.get('sort')
if year is None:
return redirect(url_for('main.index'))
if core is None:
core = 'all'
if sort is None:
sort = 'relevance'
field = 'year'
search_results = solr.term_search(core, sort, field, year)
results = search_results[0]
num_found = search_results[1]
total_number = solr.get_total_number(core)

return render_template('search.html', results=results, num_found=num_found, total_number=total_number, year=year, core=core, sort=sort)

+ 3
- 3
web/app/solr.py View File

output.append(result_output) output.append(result_output)
return output, num_found return output, num_found


def country_search(core, sort, country_code):
def term_search(core, sort, field, input):


# 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
if (sort == 'relevance'): if (sort == 'relevance'):
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=%7B!term%20f%3Dcountry%7D' + country_code + '&wt=json'
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=%7B!term%20f%3D' + field + '%7D' + input + '&wt=json'
else: else:
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=%7B!term%20f%3Dcountry%7D' + country_code + '&wt=json&sort=' + sort
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=%7B!term%20f%3D' + field + '%7D' + input + '&wt=json&sort=' + sort


# get result # get result
request = requests.get(solrurl) request = requests.get(solrurl)

+ 3
- 1
web/app/templates/data.html View File

{% if i % 2 %} {% if i % 2 %}
{{ year_data[i] }}<br> {{ year_data[i] }}<br>
{% else %} {% else %}
{{ year_data[i] }}:
<a href="{{ url_for('search.year_search', year=year_data[i]) }}">
{{ year_data[i] }}:
</a>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>

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

{% elif country_code is defined %} {% elif country_code is defined %}
<form action="{{ url_for('search.country_search') }}" method="POST"> <form action="{{ url_for('search.country_search') }}" method="POST">
<input type="hidden" name="country_code" value="{{ country_code }}"> <input type="hidden" name="country_code" value="{{ country_code }}">
{% elif year is defined %}
<form action="{{ url_for('search.year_search') }}" method="POST">
<input type="hidden" name="year" value="{{ year }}">
{% endif %} {% endif %}
<input type="hidden" name="searchopt" value="{{ core }}"> <input type="hidden" name="searchopt" value="{{ core }}">
sort by: sort by:

Loading…
Cancel
Save