.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
9
Solr_ is a popular search engine based on Lucene, just like Elasticsearch_. But
10
instead of searching in indices, you can search in collections.
11
12
Example
13
=======
14
15
This 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
32
from
json
import
loads
33
from
urllib.parse
import
urlencode
34
from
searx.exceptions
import
SearxEngineAPIException
35
36
37
base_url =
'http://localhost:8983'
38
collection =
''
39
rows = 10
40
sort =
''
# sorting: asc or desc
41
field_list =
'name'
# list of field names to display on the UI
42
default_fields =
''
# default field to query
43
query_fields =
''
# query fields
44
_search_url =
''
45
paging =
True
46
47
48
def
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
56
def
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
75
def
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
89
def
__get_response
(resp):
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
searx.exceptions.SearxEngineAPIException
Definition
exceptions.py:54
searx.engines.solr.request
request(query, params)
Definition
solr.py:56
searx.engines.solr.__get_response
__get_response(resp)
Definition
solr.py:89
searx.engines.solr.response
response(resp)
Definition
solr.py:75
searx.engines.solr.init
init(_)
Definition
solr.py:48
searx.exceptions
Definition
exceptions.py:1
searxng
searx
engines
solr.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0