Browse Source

adding error handling for no Solr core

main
Simon Bowie 2 years ago
parent
commit
30fd404c82
2 changed files with 36 additions and 28 deletions
  1. +35
    -27
      web/app/solr.py
  2. +1
    -1
      web/app/templates/theme.html

+ 35
- 27
web/app/solr.py View File

@@ -21,31 +21,34 @@ solr_port = os.environ.get('SOLR_PORT')
def solr_search(solrurl):
# get result
request = requests.get(solrurl)
# turn the API response into useful Json
json = request.json()
if request.status_code != 200:
error = "Solr error: " + str(request.text)
return error
else:
# turn the API response into useful Json
json = request.json()

num_found = json['response']['numFound']
facets = []
num_found = json['response']['numFound']
facets = []

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)
try:
json['facet_counts']
facets = json['facet_counts']['facet_fields']
except KeyError:
pass

return output, num_found, facets
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)
try:
json['facet_counts']
facets = json['facet_counts']['facet_fields']
except KeyError:
pass
return output, num_found, facets

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

@@ -183,12 +186,17 @@ def get_number_random_records(core, number):
i = 0
while i <= number-1:
search_results = random_search(core)
results = search_results[0]
for result in results:
results_list.append(result)
i += 1
if "error" not in search_results:
results = search_results[0]
for result in results:
results_list.append(result)
i += 1
else:
results_list = search_results
break
return results_list


def get_ten_random_elements(field):
core = 'all'
output = []

+ 1
- 1
web/app/templates/theme.html View File

@@ -6,7 +6,7 @@

<hr>

{% if results == 'no results found' %}
{% if 'error' in results %}

{{ results }}


Loading…
Cancel
Save