.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
solr.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2""".. sidebar:: info
3
4 - :origin:`solr.py <searx/engines/solr.py>`
5 - `Solr <https://solr.apache.org>`_
6 - `Solr Resources <https://solr.apache.org/resources.html>`_
7 - `Install Solr <https://solr.apache.org/guide/installing-solr.html>`_
8
9Solr_ is a popular search engine based on Lucene, just like Elasticsearch_. But
10instead of searching in indices, you can search in collections.
11
12Example
13=======
14
15This is an example configuration for searching in the collection
16``my-collection`` and get the results in ascending order.
17
18.. code:: yaml
19
20 - name: solr
21 engine: solr
22 shortcut: slr
23 base_url: http://localhost:8983
24 collection: my-collection
25 sort: asc
26 enable_http: true
27
28"""
29
30# pylint: disable=global-statement
31
32from json import loads
33from urllib.parse import urlencode
34from searx.exceptions import SearxEngineAPIException
35
36
37base_url = 'http://localhost:8983'
38collection = ''
39rows = 10
40sort = '' # sorting: asc or desc
41field_list = 'name' # list of field names to display on the UI
42default_fields = '' # default field to query
43query_fields = '' # query fields
44_search_url = ''
45paging = True
46
47
48def init(_):
49 if collection == '':
50 raise ValueError('collection cannot be empty')
51
52 global _search_url
53 _search_url = base_url + '/solr/' + collection + '/select?{params}'
54
55
56def request(query, params):
57 query_params = {'q': query, 'rows': rows}
58 if field_list != '':
59 query_params['fl'] = field_list
60 if query_fields != '':
61 query_params['qf'] = query_fields
62 if default_fields != '':
63 query_params['df'] = default_fields
64 if sort != '':
65 query_params['sort'] = sort
66
67 if 'pageno' in params:
68 query_params['start'] = rows * (params['pageno'] - 1)
69
70 params['url'] = _search_url.format(params=urlencode(query_params))
71
72 return params
73
74
75def response(resp):
76 resp_json = __get_response(resp)
77
78 results = []
79 for result in resp_json['response']['docs']:
80 r = {key: str(value) for key, value in result.items()}
81 if len(r) == 0:
82 continue
83 r['template'] = 'key-value.html'
84 results.append(r)
85
86 return results
87
88
90 try:
91 resp_json = loads(resp.text)
92 except Exception as e:
93 raise SearxEngineAPIException("failed to parse response") from e
94
95 if 'error' in resp_json:
96 raise SearxEngineAPIException(resp_json['error']['msg'])
97
98 return resp_json
request(query, params)
Definition solr.py:56
__get_response(resp)
Definition solr.py:89
response(resp)
Definition solr.py:75