.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
meilisearch.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2""".. sidebar:: info
3
4 - :origin:`meilisearch.py <searx/engines/meilisearch.py>`
5 - `MeiliSearch <https://www.meilisearch.com>`_
6 - `MeiliSearch Documentation <https://docs.meilisearch.com/>`_
7 - `Install MeiliSearch
8 <https://docs.meilisearch.com/learn/getting_started/installation.html>`_
9
10MeiliSearch_ is aimed at individuals and small companies. It is designed for
11small-scale (less than 10 million documents) data collections. E.g. it is great
12for storing web pages you have visited and searching in the contents later.
13
14The engine supports faceted search, so you can search in a subset of documents
15of the collection. Furthermore, you can search in MeiliSearch_ instances that
16require authentication by setting ``auth_token``.
17
18Example
19=======
20
21Here is a simple example to query a Meilisearch instance:
22
23.. code:: yaml
24
25 - name: meilisearch
26 engine: meilisearch
27 shortcut: mes
28 base_url: http://localhost:7700
29 index: my-index
30 enable_http: true
31
32"""
33
34# pylint: disable=global-statement
35
36from json import loads, dumps
37
38
39base_url = 'http://localhost:7700'
40index = ''
41auth_key = ''
42facet_filters = []
43_search_url = ''
44result_template = 'key-value.html'
45categories = ['general']
46paging = True
47
48
49def init(_):
50 if index == '':
51 raise ValueError('index cannot be empty')
52
53 global _search_url
54 _search_url = base_url + '/indexes/' + index + '/search'
55
56
57def request(query, params):
58 if auth_key != '':
59 params['headers']['X-Meili-API-Key'] = auth_key
60
61 params['headers']['Content-Type'] = 'application/json'
62 params['url'] = _search_url
63 params['method'] = 'POST'
64
65 data = {
66 'q': query,
67 'offset': 10 * (params['pageno'] - 1),
68 'limit': 10,
69 }
70 if len(facet_filters) > 0:
71 data['facetFilters'] = facet_filters
72
73 params['data'] = dumps(data)
74
75 return params
76
77
78def response(resp):
79 results = []
80
81 resp_json = loads(resp.text)
82 for result in resp_json['hits']:
83 r = {key: str(value) for key, value in result.items()}
84 r['template'] = result_template
85 results.append(r)
86
87 return results