summaryrefslogtreecommitdiff
path: root/searx/engines/meilisearch.py
blob: 86cd7512cd48104669aee84433044811fa212076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""
 Meilisearch
"""

# pylint: disable=global-statement, missing-function-docstring

from json import loads, dumps


base_url = 'http://localhost:7700'
index = ''
auth_key = ''
facet_filters = list()
_search_url = ''
result_template = 'key-value.html'
categories = ['general']
paging = True


def init(_):
    if index == '':
        raise ValueError('index cannot be empty')

    global _search_url
    _search_url = base_url + '/indexes/' + index + '/search'


def request(query, params):
    if auth_key != '':
        params['headers']['X-Meili-API-Key'] = auth_key

    params['headers']['Content-Type'] = 'application/json'
    params['url'] = _search_url
    params['method'] = 'POST'

    data = {
        'q': query,
        'offset': 10 * (params['pageno'] - 1),
        'limit': 10,
    }
    if len(facet_filters) > 0:
        data['facetFilters'] = facet_filters

    params['data'] = dumps(data)

    return params


def response(resp):
    results = []

    resp_json = loads(resp.text)
    for result in resp_json['hits']:
        r = {key: str(value) for key, value in result.items()}
        r['template'] = result_template
        results.append(r)

    return results