| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | # SPDX-License-Identifier: AGPL-3.0-or-later""".. sidebar:: info   - :origin:`solr.py <searx/engines/solr.py>`   - `Solr <https://solr.apache.org>`_   - `Solr Resources <https://solr.apache.org/resources.html>`_   - `Install Solr <https://solr.apache.org/guide/installing-solr.html>`_Solr_ is a popular search engine based on Lucene, just like Elasticsearch_.  Butinstead of searching in indices, you can search in collections.Example=======This is an example configuration for searching in the collection``my-collection`` and get the results in ascending order... code:: yaml  - name: solr    engine: solr    shortcut: slr    base_url: http://localhost:8983    collection: my-collection    sort: asc    enable_http: true"""# pylint: disable=global-statementfrom json import loadsfrom urllib.parse import urlencodefrom searx.exceptions import SearxEngineAPIExceptionbase_url = 'http://localhost:8983'collection = ''rows = 10sort = ''  # sorting: asc or descfield_list = 'name'  # list of field names to display on the UIdefault_fields = ''  # default field to queryquery_fields = ''  # query fields_search_url = ''paging = Truedef init(_):    if collection == '':        raise ValueError('collection cannot be empty')    global _search_url    _search_url = base_url + '/solr/' + collection + '/select?{params}'def request(query, params):    query_params = {'q': query, 'rows': rows}    if field_list != '':        query_params['fl'] = field_list    if query_fields != '':        query_params['qf'] = query_fields    if default_fields != '':        query_params['df'] = default_fields    if sort != '':        query_params['sort'] = sort    if 'pageno' in params:        query_params['start'] = rows * (params['pageno'] - 1)    params['url'] = _search_url.format(params=urlencode(query_params))    return paramsdef response(resp):    resp_json = __get_response(resp)    results = []    for result in resp_json['response']['docs']:        r = {key: str(value) for key, value in result.items()}        if len(r) == 0:            continue        r['template'] = 'key-value.html'        results.append(r)    return resultsdef __get_response(resp):    try:        resp_json = loads(resp.text)    except Exception as e:        raise SearxEngineAPIException("failed to parse response") from e    if 'error' in resp_json:        raise SearxEngineAPIException(resp_json['error']['msg'])    return resp_json
 |