meilisearch.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Meilisearch
  4. """
  5. # pylint: disable=global-statement, missing-function-docstring
  6. from json import loads, dumps
  7. base_url = 'http://localhost:7700'
  8. index = ''
  9. auth_key = ''
  10. facet_filters = []
  11. _search_url = ''
  12. result_template = 'key-value.html'
  13. categories = ['general']
  14. paging = True
  15. def init(_):
  16. if index == '':
  17. raise ValueError('index cannot be empty')
  18. global _search_url
  19. _search_url = base_url + '/indexes/' + index + '/search'
  20. def request(query, params):
  21. if auth_key != '':
  22. params['headers']['X-Meili-API-Key'] = auth_key
  23. params['headers']['Content-Type'] = 'application/json'
  24. params['url'] = _search_url
  25. params['method'] = 'POST'
  26. data = {
  27. 'q': query,
  28. 'offset': 10 * (params['pageno'] - 1),
  29. 'limit': 10,
  30. }
  31. if len(facet_filters) > 0:
  32. data['facetFilters'] = facet_filters
  33. params['data'] = dumps(data)
  34. return params
  35. def response(resp):
  36. results = []
  37. resp_json = loads(resp.text)
  38. for result in resp_json['hits']:
  39. r = {key: str(value) for key, value in result.items()}
  40. r['template'] = result_template
  41. results.append(r)
  42. return results