@@ -25,7 +25,7 @@ def basic_search(): | |||
sort = request.form.get('sort') | |||
else: | |||
sort = 'relevance' | |||
search_results = solr.solr_search(core, sort, search) | |||
search_results = solr.content_search(core, sort, search) | |||
results = search_results[0] | |||
num_found = search_results[1] | |||
total_number = solr.get_total_number(core) | |||
@@ -38,6 +38,8 @@ def basic_search(): | |||
def id_search(): | |||
if request.args.get('id') is None: | |||
return redirect(url_for('main.index')) | |||
else: | |||
id = request.args.get('id') | |||
if request.args.get('core') is not None: | |||
core = request.args.get('core') | |||
else: | |||
@@ -46,8 +48,7 @@ def id_search(): | |||
sort = request.args.get('sort') | |||
else: | |||
sort = 'relevance' | |||
id = request.args.get('id') | |||
search_results = solr.solr_search(core, sort, search, id) | |||
search_results = solr.content_search(core, sort, search, id) | |||
results = search_results[0] | |||
for result in results: | |||
@@ -58,3 +59,27 @@ def id_search(): | |||
result.update(image) | |||
return render_template('record.html', results=results) | |||
# route for country search page | |||
@search.route('/search/country/', methods=['GET', 'POST']) | |||
def country_search(): | |||
if request.method == 'POST': | |||
country_code = request.form.get('country_code') | |||
core = request.form.get('core') | |||
sort = request.form.get('sort') | |||
else: | |||
country_code = request.args.get('country_code') | |||
core = request.args.get('core') | |||
sort = request.args.get('sort') | |||
if country_code is None: | |||
return redirect(url_for('main.index')) | |||
if core is None: | |||
core = 'all' | |||
if sort is None: | |||
sort = 'relevance' | |||
search_results = solr.country_search(core, sort, country_code) | |||
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, country_code=country_code, core=core, sort=sort) |
@@ -17,7 +17,7 @@ from . import ops | |||
solr_hostname = os.environ.get('SOLR_HOSTNAME') | |||
solr_port = os.environ.get('SOLR_PORT') | |||
def solr_search(core, sort, search=None, id=None): | |||
def content_search(core, sort, search=None, id=None): | |||
# 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 id is not None: | |||
@@ -49,6 +49,36 @@ def solr_search(core, sort, search=None, id=None): | |||
output.append(result_output) | |||
return output, num_found | |||
def country_search(core, sort, country_code): | |||
# 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'): | |||
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=%7B!term%20f%3Dcountry%7D' + country_code + '&wt=json' | |||
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 | |||
# get result | |||
request = requests.get(solrurl) | |||
# turn the API response into useful Json | |||
json = request.json() | |||
num_found = json['response']['numFound'] | |||
if (num_found == 0): | |||
output = 'no results found' | |||
else: | |||
output = [] | |||
for result in json['response']['docs']: | |||
# set ID variable | |||
id = result['id'] | |||
# set content variable | |||
content = result['content'] | |||
# parse result | |||
result_output = parse_result(id, content) | |||
output.append(result_output) | |||
return output, num_found | |||
def parse_result(id, input): | |||
output = {} | |||
@@ -179,7 +209,7 @@ def get_total_number(core): | |||
def get_term_data(field, core): | |||
# 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 + '/terms?terms.fl=' + field + '&wt=json&terms.limit=1000' | |||
solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/terms?terms.fl=' + field + '&wt=json&terms.limit=1000&terms.sort=index' | |||
# get result | |||
request = requests.get(solrurl) |
@@ -24,9 +24,14 @@ There are a total of {{ total_number }} patents. | |||
{{ country_data[i] }}<br> | |||
{% else %} | |||
{% if country_data[i].name is defined %} | |||
{{ country_data[i].name }} {{ country_data[i].flag }}: | |||
<a href="{{ url_for('search.country_search', country_code=country_data[i].alpha_2) }}"> | |||
{{ country_data[i].name }} | |||
</a> | |||
{{ country_data[i].flag }}: | |||
{% else %} | |||
{{ country_data[i] }}: | |||
<a href="{{ url_for('search.country_search', country_code=country_data[i]) }}"> | |||
{{ country_data[i] }}: | |||
</a> | |||
{% endif %} | |||
{% endif %} | |||
{% endfor %} |
@@ -11,8 +11,13 @@ | |||
</div> | |||
<div class="row p-3"> | |||
{% if search is defined %} | |||
<form action="{{ url_for('search.basic_search') }}" method="POST"> | |||
<input type="hidden" name="search" value="{{ search }}"> | |||
{% elif country_code is defined %} | |||
<form action="{{ url_for('search.country_search') }}" method="POST"> | |||
<input type="hidden" name="country_code" value="{{ country_code }}"> | |||
{% endif %} | |||
<input type="hidden" name="searchopt" value="{{ core }}"> | |||
sort by: | |||
<select name="sort" id="sort" onchange="this.form.submit()"> | |||
@@ -86,8 +91,10 @@ | |||
{% endif %} | |||
{% if search is defined %} | |||
<script type="text/javascript"> | |||
highlightSearchTerms({{ search|tojson}}) | |||
</script> | |||
{% endif %} | |||
{% endblock %} |