| sort = request.form.get('sort') | sort = request.form.get('sort') | ||||
| else: | else: | ||||
| sort = 'relevance' | sort = 'relevance' | ||||
| search_results = solr.solr_search(core, sort, search) | |||||
| search_results = solr.content_search(core, sort, search) | |||||
| 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) | ||||
| def id_search(): | def id_search(): | ||||
| if request.args.get('id') is None: | if request.args.get('id') is None: | ||||
| return redirect(url_for('main.index')) | return redirect(url_for('main.index')) | ||||
| else: | |||||
| id = request.args.get('id') | |||||
| if request.args.get('core') is not None: | if request.args.get('core') is not None: | ||||
| core = request.args.get('core') | core = request.args.get('core') | ||||
| else: | else: | ||||
| sort = request.args.get('sort') | sort = request.args.get('sort') | ||||
| else: | else: | ||||
| sort = 'relevance' | 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] | results = search_results[0] | ||||
| for result in results: | for result in results: | ||||
| result.update(image) | result.update(image) | ||||
| return render_template('record.html', results=results) | 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) |
| solr_hostname = os.environ.get('SOLR_HOSTNAME') | solr_hostname = os.environ.get('SOLR_HOSTNAME') | ||||
| solr_port = os.environ.get('SOLR_PORT') | 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 | # 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: | if id is not None: | ||||
| output.append(result_output) | output.append(result_output) | ||||
| return output, num_found | 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): | def parse_result(id, input): | ||||
| output = {} | output = {} | ||||
| def get_term_data(field, 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 | # 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 | # get result | ||||
| request = requests.get(solrurl) | request = requests.get(solrurl) |
| {{ country_data[i] }}<br> | {{ country_data[i] }}<br> | ||||
| {% else %} | {% else %} | ||||
| {% if country_data[i].name is defined %} | {% 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 %} | {% else %} | ||||
| {{ country_data[i] }}: | |||||
| <a href="{{ url_for('search.country_search', country_code=country_data[i]) }}"> | |||||
| {{ country_data[i] }}: | |||||
| </a> | |||||
| {% endif %} | {% endif %} | ||||
| {% endif %} | {% endif %} | ||||
| {% endfor %} | {% endfor %} |
| </div> | </div> | ||||
| <div class="row p-3"> | <div class="row p-3"> | ||||
| {% if search is defined %} | |||||
| <form action="{{ url_for('search.basic_search') }}" method="POST"> | <form action="{{ url_for('search.basic_search') }}" method="POST"> | ||||
| <input type="hidden" name="search" value="{{ search }}"> | <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 }}"> | <input type="hidden" name="searchopt" value="{{ core }}"> | ||||
| sort by: | sort by: | ||||
| <select name="sort" id="sort" onchange="this.form.submit()"> | <select name="sort" id="sort" onchange="this.form.submit()"> | ||||
| {% endif %} | {% endif %} | ||||
| {% if search is defined %} | |||||
| <script type="text/javascript"> | <script type="text/javascript"> | ||||
| highlightSearchTerms({{ search|tojson}}) | highlightSearchTerms({{ search|tojson}}) | ||||
| </script> | </script> | ||||
| {% endif %} | |||||
| {% endblock %} | {% endblock %} |