图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.1 KiB

  1. from typing import Union
  2. from .aggregation import Asc, Desc, Reducer, SortDirection
  3. class FieldOnlyReducer(Reducer):
  4. """See https://redis.io/docs/interact/search-and-query/search/aggregations/"""
  5. def __init__(self, field: str) -> None:
  6. super().__init__(field)
  7. self._field = field
  8. class count(Reducer):
  9. """
  10. Counts the number of results in the group
  11. """
  12. NAME = "COUNT"
  13. def __init__(self) -> None:
  14. super().__init__()
  15. class sum(FieldOnlyReducer):
  16. """
  17. Calculates the sum of all the values in the given fields within the group
  18. """
  19. NAME = "SUM"
  20. def __init__(self, field: str) -> None:
  21. super().__init__(field)
  22. class min(FieldOnlyReducer):
  23. """
  24. Calculates the smallest value in the given field within the group
  25. """
  26. NAME = "MIN"
  27. def __init__(self, field: str) -> None:
  28. super().__init__(field)
  29. class max(FieldOnlyReducer):
  30. """
  31. Calculates the largest value in the given field within the group
  32. """
  33. NAME = "MAX"
  34. def __init__(self, field: str) -> None:
  35. super().__init__(field)
  36. class avg(FieldOnlyReducer):
  37. """
  38. Calculates the mean value in the given field within the group
  39. """
  40. NAME = "AVG"
  41. def __init__(self, field: str) -> None:
  42. super().__init__(field)
  43. class tolist(FieldOnlyReducer):
  44. """
  45. Returns all the matched properties in a list
  46. """
  47. NAME = "TOLIST"
  48. def __init__(self, field: str) -> None:
  49. super().__init__(field)
  50. class count_distinct(FieldOnlyReducer):
  51. """
  52. Calculate the number of distinct values contained in all the results in
  53. the group for the given field
  54. """
  55. NAME = "COUNT_DISTINCT"
  56. def __init__(self, field: str) -> None:
  57. super().__init__(field)
  58. class count_distinctish(FieldOnlyReducer):
  59. """
  60. Calculate the number of distinct values contained in all the results in the
  61. group for the given field. This uses a faster algorithm than
  62. `count_distinct` but is less accurate
  63. """
  64. NAME = "COUNT_DISTINCTISH"
  65. class quantile(Reducer):
  66. """
  67. Return the value for the nth percentile within the range of values for the
  68. field within the group.
  69. """
  70. NAME = "QUANTILE"
  71. def __init__(self, field: str, pct: float) -> None:
  72. super().__init__(field, str(pct))
  73. self._field = field
  74. class stddev(FieldOnlyReducer):
  75. """
  76. Return the standard deviation for the values within the group
  77. """
  78. NAME = "STDDEV"
  79. def __init__(self, field: str) -> None:
  80. super().__init__(field)
  81. class first_value(Reducer):
  82. """
  83. Selects the first value within the group according to sorting parameters
  84. """
  85. NAME = "FIRST_VALUE"
  86. def __init__(self, field: str, *byfields: Union[Asc, Desc]) -> None:
  87. """
  88. Selects the first value of the given field within the group.
  89. ### Parameter
  90. - **field**: Source field used for the value
  91. - **byfields**: How to sort the results. This can be either the
  92. *class* of `aggregation.Asc` or `aggregation.Desc` in which
  93. case the field `field` is also used as the sort input.
  94. `byfields` can also be one or more *instances* of `Asc` or `Desc`
  95. indicating the sort order for these fields
  96. """
  97. fieldstrs = []
  98. if (
  99. len(byfields) == 1
  100. and isinstance(byfields[0], type)
  101. and issubclass(byfields[0], SortDirection)
  102. ):
  103. byfields = [byfields[0](field)]
  104. for f in byfields:
  105. fieldstrs += [f.field, f.DIRSTRING]
  106. args = [field]
  107. if fieldstrs:
  108. args += ["BY"] + fieldstrs
  109. super().__init__(*args)
  110. self._field = field
  111. class random_sample(Reducer):
  112. """
  113. Returns a random sample of items from the dataset, from the given property
  114. """
  115. NAME = "RANDOM_SAMPLE"
  116. def __init__(self, field: str, size: int) -> None:
  117. """
  118. ### Parameter
  119. **field**: Field to sample from
  120. **size**: Return this many items (can be less)
  121. """
  122. args = [field, str(size)]
  123. super().__init__(*args)
  124. self._field = field