solr.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Solr
  4. """
  5. # pylint: disable=global-statement, missing-function-docstring
  6. from json import loads
  7. from urllib.parse import urlencode
  8. from searx.exceptions import SearxEngineAPIException
  9. base_url = 'http://localhost:8983'
  10. collection = ''
  11. rows = 10
  12. sort = '' # sorting: asc or desc
  13. field_list = 'name' # list of field names to display on the UI
  14. default_fields = '' # default field to query
  15. query_fields = '' # query fields
  16. _search_url = ''
  17. paging = True
  18. def init(_):
  19. if collection == '':
  20. raise ValueError('collection cannot be empty')
  21. global _search_url
  22. _search_url = base_url + '/solr/' + collection + '/select?{params}'
  23. def request(query, params):
  24. query_params = {'q': query, 'rows': rows}
  25. if field_list != '':
  26. query_params['fl'] = field_list
  27. if query_fields != '':
  28. query_params['qf'] = query_fields
  29. if default_fields != '':
  30. query_params['df'] = default_fields
  31. if sort != '':
  32. query_params['sort'] = sort
  33. if 'pageno' in params:
  34. query_params['start'] = rows * (params['pageno'] - 1)
  35. params['url'] = _search_url.format(params=urlencode(query_params))
  36. return params
  37. def response(resp):
  38. resp_json = __get_response(resp)
  39. results = []
  40. for result in resp_json['response']['docs']:
  41. r = {key: str(value) for key, value in result.items()}
  42. if len(r) == 0:
  43. continue
  44. r['template'] = 'key-value.html'
  45. results.append(r)
  46. return results
  47. def __get_response(resp):
  48. try:
  49. resp_json = loads(resp.text)
  50. except Exception as e:
  51. raise SearxEngineAPIException("failed to parse response") from e
  52. if 'error' in resp_json:
  53. raise SearxEngineAPIException(resp_json['error']['msg'])
  54. return resp_json