meilisearch.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_key`_.
  14. .. _auth_key: https://www.meilisearch.com/docs/reference/api/overview#authorization
  15. Example
  16. =======
  17. Here is a simple example to query a Meilisearch instance:
  18. .. code:: yaml
  19. - name: meilisearch
  20. engine: meilisearch
  21. shortcut: mes
  22. base_url: http://localhost:7700
  23. index: my-index
  24. enable_http: true
  25. # auth_key: Bearer XXXXX
  26. """
  27. # pylint: disable=global-statement
  28. from json import dumps
  29. from searx.result_types import EngineResults
  30. from searx.extended_types import SXNG_Response
  31. base_url = 'http://localhost:7700'
  32. index = ''
  33. auth_key = ''
  34. facet_filters = []
  35. _search_url = ''
  36. categories = ['general']
  37. paging = True
  38. def init(_):
  39. if index == '':
  40. raise ValueError('index cannot be empty')
  41. global _search_url
  42. _search_url = base_url + '/indexes/' + index + '/search'
  43. def request(query, params):
  44. if auth_key != '':
  45. params['headers']['Authorization'] = auth_key
  46. params['headers']['Content-Type'] = 'application/json'
  47. params['url'] = _search_url
  48. params['method'] = 'POST'
  49. data = {
  50. 'q': query,
  51. 'offset': 10 * (params['pageno'] - 1),
  52. 'limit': 10,
  53. }
  54. if len(facet_filters) > 0:
  55. data['facetFilters'] = facet_filters
  56. params['data'] = dumps(data)
  57. return params
  58. def response(resp: SXNG_Response) -> EngineResults:
  59. res = EngineResults()
  60. resp_json = resp.json()
  61. for row in resp_json['hits']:
  62. kvmap = {key: str(value) for key, value in row.items()}
  63. res.add(res.types.KeyValue(kvmap=kvmap))
  64. return res