query.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. from typing import List, Optional, Union
  2. class Query:
  3. """
  4. Query is used to build complex queries that have more parameters than just
  5. the query string. The query string is set in the constructor, and other
  6. options have setter functions.
  7. The setter functions return the query object, so they can be chained,
  8. i.e. `Query("foo").verbatim().filter(...)` etc.
  9. """
  10. def __init__(self, query_string: str) -> None:
  11. """
  12. Create a new query object.
  13. The query string is set in the constructor, and other options have
  14. setter functions.
  15. """
  16. self._query_string: str = query_string
  17. self._offset: int = 0
  18. self._num: int = 10
  19. self._no_content: bool = False
  20. self._no_stopwords: bool = False
  21. self._fields: Optional[List[str]] = None
  22. self._verbatim: bool = False
  23. self._with_payloads: bool = False
  24. self._with_scores: bool = False
  25. self._scorer: Optional[str] = None
  26. self._filters: List = list()
  27. self._ids: Optional[List[str]] = None
  28. self._slop: int = -1
  29. self._timeout: Optional[float] = None
  30. self._in_order: bool = False
  31. self._sortby: Optional[SortbyField] = None
  32. self._return_fields: List = []
  33. self._summarize_fields: List = []
  34. self._highlight_fields: List = []
  35. self._language: Optional[str] = None
  36. self._expander: Optional[str] = None
  37. self._dialect: Optional[int] = None
  38. def query_string(self) -> str:
  39. """Return the query string of this query only."""
  40. return self._query_string
  41. def limit_ids(self, *ids) -> "Query":
  42. """Limit the results to a specific set of pre-known document
  43. ids of any length."""
  44. self._ids = ids
  45. return self
  46. def return_fields(self, *fields) -> "Query":
  47. """Add fields to return fields."""
  48. self._return_fields += fields
  49. return self
  50. def return_field(self, field: str, as_field: Optional[str] = None) -> "Query":
  51. """Add field to return fields (Optional: add 'AS' name
  52. to the field)."""
  53. self._return_fields.append(field)
  54. if as_field is not None:
  55. self._return_fields += ("AS", as_field)
  56. return self
  57. def _mk_field_list(self, fields: List[str]) -> List:
  58. if not fields:
  59. return []
  60. return [fields] if isinstance(fields, str) else list(fields)
  61. def summarize(
  62. self,
  63. fields: Optional[List] = None,
  64. context_len: Optional[int] = None,
  65. num_frags: Optional[int] = None,
  66. sep: Optional[str] = None,
  67. ) -> "Query":
  68. """
  69. Return an abridged format of the field, containing only the segments of
  70. the field which contain the matching term(s).
  71. If `fields` is specified, then only the mentioned fields are
  72. summarized; otherwise all results are summarized.
  73. Server side defaults are used for each option (except `fields`)
  74. if not specified
  75. - **fields** List of fields to summarize. All fields are summarized
  76. if not specified
  77. - **context_len** Amount of context to include with each fragment
  78. - **num_frags** Number of fragments per document
  79. - **sep** Separator string to separate fragments
  80. """
  81. args = ["SUMMARIZE"]
  82. fields = self._mk_field_list(fields)
  83. if fields:
  84. args += ["FIELDS", str(len(fields))] + fields
  85. if context_len is not None:
  86. args += ["LEN", str(context_len)]
  87. if num_frags is not None:
  88. args += ["FRAGS", str(num_frags)]
  89. if sep is not None:
  90. args += ["SEPARATOR", sep]
  91. self._summarize_fields = args
  92. return self
  93. def highlight(
  94. self, fields: Optional[List[str]] = None, tags: Optional[List[str]] = None
  95. ) -> None:
  96. """
  97. Apply specified markup to matched term(s) within the returned field(s).
  98. - **fields** If specified then only those mentioned fields are
  99. highlighted, otherwise all fields are highlighted
  100. - **tags** A list of two strings to surround the match.
  101. """
  102. args = ["HIGHLIGHT"]
  103. fields = self._mk_field_list(fields)
  104. if fields:
  105. args += ["FIELDS", str(len(fields))] + fields
  106. if tags:
  107. args += ["TAGS"] + list(tags)
  108. self._highlight_fields = args
  109. return self
  110. def language(self, language: str) -> "Query":
  111. """
  112. Analyze the query as being in the specified language.
  113. :param language: The language (e.g. `chinese` or `english`)
  114. """
  115. self._language = language
  116. return self
  117. def slop(self, slop: int) -> "Query":
  118. """Allow a maximum of N intervening non matched terms between
  119. phrase terms (0 means exact phrase).
  120. """
  121. self._slop = slop
  122. return self
  123. def timeout(self, timeout: float) -> "Query":
  124. """overrides the timeout parameter of the module"""
  125. self._timeout = timeout
  126. return self
  127. def in_order(self) -> "Query":
  128. """
  129. Match only documents where the query terms appear in
  130. the same order in the document.
  131. i.e. for the query "hello world", we do not match "world hello"
  132. """
  133. self._in_order = True
  134. return self
  135. def scorer(self, scorer: str) -> "Query":
  136. """
  137. Use a different scoring function to evaluate document relevance.
  138. Default is `TFIDF`.
  139. :param scorer: The scoring function to use
  140. (e.g. `TFIDF.DOCNORM` or `BM25`)
  141. """
  142. self._scorer = scorer
  143. return self
  144. def get_args(self) -> List[str]:
  145. """Format the redis arguments for this query and return them."""
  146. args = [self._query_string]
  147. args += self._get_args_tags()
  148. args += self._summarize_fields + self._highlight_fields
  149. args += ["LIMIT", self._offset, self._num]
  150. return args
  151. def _get_args_tags(self) -> List[str]:
  152. args = []
  153. if self._no_content:
  154. args.append("NOCONTENT")
  155. if self._fields:
  156. args.append("INFIELDS")
  157. args.append(len(self._fields))
  158. args += self._fields
  159. if self._verbatim:
  160. args.append("VERBATIM")
  161. if self._no_stopwords:
  162. args.append("NOSTOPWORDS")
  163. if self._filters:
  164. for flt in self._filters:
  165. if not isinstance(flt, Filter):
  166. raise AttributeError("Did not receive a Filter object.")
  167. args += flt.args
  168. if self._with_payloads:
  169. args.append("WITHPAYLOADS")
  170. if self._scorer:
  171. args += ["SCORER", self._scorer]
  172. if self._with_scores:
  173. args.append("WITHSCORES")
  174. if self._ids:
  175. args.append("INKEYS")
  176. args.append(len(self._ids))
  177. args += self._ids
  178. if self._slop >= 0:
  179. args += ["SLOP", self._slop]
  180. if self._timeout is not None:
  181. args += ["TIMEOUT", self._timeout]
  182. if self._in_order:
  183. args.append("INORDER")
  184. if self._return_fields:
  185. args.append("RETURN")
  186. args.append(len(self._return_fields))
  187. args += self._return_fields
  188. if self._sortby:
  189. if not isinstance(self._sortby, SortbyField):
  190. raise AttributeError("Did not receive a SortByField.")
  191. args.append("SORTBY")
  192. args += self._sortby.args
  193. if self._language:
  194. args += ["LANGUAGE", self._language]
  195. if self._expander:
  196. args += ["EXPANDER", self._expander]
  197. if self._dialect:
  198. args += ["DIALECT", self._dialect]
  199. return args
  200. def paging(self, offset: int, num: int) -> "Query":
  201. """
  202. Set the paging for the query (defaults to 0..10).
  203. - **offset**: Paging offset for the results. Defaults to 0
  204. - **num**: How many results do we want
  205. """
  206. self._offset = offset
  207. self._num = num
  208. return self
  209. def verbatim(self) -> "Query":
  210. """Set the query to be verbatim, i.e. use no query expansion
  211. or stemming.
  212. """
  213. self._verbatim = True
  214. return self
  215. def no_content(self) -> "Query":
  216. """Set the query to only return ids and not the document content."""
  217. self._no_content = True
  218. return self
  219. def no_stopwords(self) -> "Query":
  220. """
  221. Prevent the query from being filtered for stopwords.
  222. Only useful in very big queries that you are certain contain
  223. no stopwords.
  224. """
  225. self._no_stopwords = True
  226. return self
  227. def with_payloads(self) -> "Query":
  228. """Ask the engine to return document payloads."""
  229. self._with_payloads = True
  230. return self
  231. def with_scores(self) -> "Query":
  232. """Ask the engine to return document search scores."""
  233. self._with_scores = True
  234. return self
  235. def limit_fields(self, *fields: List[str]) -> "Query":
  236. """
  237. Limit the search to specific TEXT fields only.
  238. - **fields**: A list of strings, case sensitive field names
  239. from the defined schema.
  240. """
  241. self._fields = fields
  242. return self
  243. def add_filter(self, flt: "Filter") -> "Query":
  244. """
  245. Add a numeric or geo filter to the query.
  246. **Currently only one of each filter is supported by the engine**
  247. - **flt**: A NumericFilter or GeoFilter object, used on a
  248. corresponding field
  249. """
  250. self._filters.append(flt)
  251. return self
  252. def sort_by(self, field: str, asc: bool = True) -> "Query":
  253. """
  254. Add a sortby field to the query.
  255. - **field** - the name of the field to sort by
  256. - **asc** - when `True`, sorting will be done in asceding order
  257. """
  258. self._sortby = SortbyField(field, asc)
  259. return self
  260. def expander(self, expander: str) -> "Query":
  261. """
  262. Add a expander field to the query.
  263. - **expander** - the name of the expander
  264. """
  265. self._expander = expander
  266. return self
  267. def dialect(self, dialect: int) -> "Query":
  268. """
  269. Add a dialect field to the query.
  270. - **dialect** - dialect version to execute the query under
  271. """
  272. self._dialect = dialect
  273. return self
  274. class Filter:
  275. def __init__(self, keyword: str, field: str, *args: List[str]) -> None:
  276. self.args = [keyword, field] + list(args)
  277. class NumericFilter(Filter):
  278. INF = "+inf"
  279. NEG_INF = "-inf"
  280. def __init__(
  281. self,
  282. field: str,
  283. minval: Union[int, str],
  284. maxval: Union[int, str],
  285. minExclusive: bool = False,
  286. maxExclusive: bool = False,
  287. ) -> None:
  288. args = [
  289. minval if not minExclusive else f"({minval}",
  290. maxval if not maxExclusive else f"({maxval}",
  291. ]
  292. Filter.__init__(self, "FILTER", field, *args)
  293. class GeoFilter(Filter):
  294. METERS = "m"
  295. KILOMETERS = "km"
  296. FEET = "ft"
  297. MILES = "mi"
  298. def __init__(
  299. self, field: str, lon: float, lat: float, radius: float, unit: str = KILOMETERS
  300. ) -> None:
  301. Filter.__init__(self, "GEOFILTER", field, lon, lat, radius, unit)
  302. class SortbyField:
  303. def __init__(self, field: str, asc=True) -> None:
  304. self.args = [field, "ASC" if asc else "DESC"]