solr.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """.. sidebar:: info
  3. - :origin:`solr.py <searx/engines/solr.py>`
  4. - `Solr <https://solr.apache.org>`_
  5. - `Solr Resources <https://solr.apache.org/resources.html>`_
  6. - `Install Solr <https://solr.apache.org/guide/installing-solr.html>`_
  7. Solr_ is a popular search engine based on Lucene, just like Elasticsearch_. But
  8. instead of searching in indices, you can search in collections.
  9. Example
  10. =======
  11. This is an example configuration for searching in the collection
  12. ``my-collection`` and get the results in ascending order.
  13. .. code:: yaml
  14. - name: solr
  15. engine: solr
  16. shortcut: slr
  17. base_url: http://localhost:8983
  18. collection: my-collection
  19. sort: asc
  20. enable_http: true
  21. """
  22. # pylint: disable=global-statement
  23. from urllib.parse import urlencode
  24. from searx.exceptions import SearxEngineAPIException
  25. from searx.result_types import EngineResults
  26. from searx.extended_types import SXNG_Response
  27. base_url = 'http://localhost:8983'
  28. collection = ''
  29. rows = 10
  30. sort = '' # sorting: asc or desc
  31. field_list = 'name' # list of field names to display on the UI
  32. default_fields = '' # default field to query
  33. query_fields = '' # query fields
  34. _search_url = ''
  35. paging = True
  36. def init(_):
  37. if collection == '':
  38. raise ValueError('collection cannot be empty')
  39. global _search_url
  40. _search_url = base_url + '/solr/' + collection + '/select?{params}'
  41. def request(query, params):
  42. query_params = {'q': query, 'rows': rows}
  43. if field_list != '':
  44. query_params['fl'] = field_list
  45. if query_fields != '':
  46. query_params['qf'] = query_fields
  47. if default_fields != '':
  48. query_params['df'] = default_fields
  49. if sort != '':
  50. query_params['sort'] = sort
  51. if 'pageno' in params:
  52. query_params['start'] = rows * (params['pageno'] - 1)
  53. params['url'] = _search_url.format(params=urlencode(query_params))
  54. return params
  55. def response(resp: SXNG_Response) -> EngineResults:
  56. try:
  57. resp_json = resp.json()
  58. except Exception as e:
  59. raise SearxEngineAPIException("failed to parse response") from e
  60. if "error" in resp_json:
  61. raise SearxEngineAPIException(resp_json["error"]["msg"])
  62. res = EngineResults()
  63. for result in resp_json["response"]["docs"]:
  64. kvmap = {key: str(value) for key, value in result.items()}
  65. if not kvmap:
  66. continue
  67. res.add(res.types.KeyValue(kvmap=kvmap))
  68. return res