meilisearch.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """.. sidebar:: info
  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. MeiliSearch_ is aimed at individuals and small companies. It is designed for
  10. small-scale (less than 10 million documents) data collections. E.g. it is great
  11. for storing web pages you have visited and searching in the contents later.
  12. The engine supports faceted search, so you can search in a subset of documents
  13. of the collection. Furthermore, you can search in MeiliSearch_ instances that
  14. require authentication by setting ``auth_token``.
  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. """
  26. # pylint: disable=global-statement
  27. from json import loads, dumps
  28. base_url = 'http://localhost:7700'
  29. index = ''
  30. auth_key = ''
  31. facet_filters = []
  32. _search_url = ''
  33. result_template = 'key-value.html'
  34. categories = ['general']
  35. paging = True
  36. def init(_):
  37. if index == '':
  38. raise ValueError('index cannot be empty')
  39. global _search_url
  40. _search_url = base_url + '/indexes/' + index + '/search'
  41. def request(query, params):
  42. if auth_key != '':
  43. params['headers']['X-Meili-API-Key'] = auth_key
  44. params['headers']['Content-Type'] = 'application/json'
  45. params['url'] = _search_url
  46. params['method'] = 'POST'
  47. data = {
  48. 'q': query,
  49. 'offset': 10 * (params['pageno'] - 1),
  50. 'limit': 10,
  51. }
  52. if len(facet_filters) > 0:
  53. data['facetFilters'] = facet_filters
  54. params['data'] = dumps(data)
  55. return params
  56. def response(resp):
  57. results = []
  58. resp_json = loads(resp.text)
  59. for result in resp_json['hits']:
  60. r = {key: str(value) for key, value in result.items()}
  61. r['template'] = result_template
  62. results.append(r)
  63. return results