meilisearch.py 2.1 KB

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