meilisearch.py 1.2 KB

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