solr.py 1.8 KB

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