CreateArguments.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) 2009-2020 Daniele Alessandri
  6. * (c) 2021-2025 Till Krüss
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Predis\Command\Argument\Search;
  12. use InvalidArgumentException;
  13. class CreateArguments extends CommonArguments
  14. {
  15. /**
  16. * @var string[]
  17. */
  18. private $supportedDataTypesEnum = [
  19. 'hash' => 'HASH',
  20. 'json' => 'JSON',
  21. ];
  22. /**
  23. * Specify data type for given index. To index JSON you must have the RedisJSON module to be installed.
  24. *
  25. * @param string $modifier
  26. * @return $this
  27. */
  28. public function on(string $modifier = 'HASH'): self
  29. {
  30. if (in_array(strtoupper($modifier), $this->supportedDataTypesEnum)) {
  31. $this->arguments[] = 'ON';
  32. $this->arguments[] = $this->supportedDataTypesEnum[strtolower($modifier)];
  33. return $this;
  34. }
  35. $enumValues = implode(', ', array_values($this->supportedDataTypesEnum));
  36. throw new InvalidArgumentException("Wrong modifier value given. Currently supports: {$enumValues}");
  37. }
  38. /**
  39. * Adds one or more prefixes into index.
  40. *
  41. * @param array $prefixes
  42. * @return $this
  43. */
  44. public function prefix(array $prefixes): self
  45. {
  46. $this->arguments[] = 'PREFIX';
  47. $this->arguments[] = count($prefixes);
  48. $this->arguments = array_merge($this->arguments, $prefixes);
  49. return $this;
  50. }
  51. /**
  52. * Document attribute set as document language.
  53. *
  54. * @param string $languageAttribute
  55. * @return $this
  56. */
  57. public function languageField(string $languageAttribute): self
  58. {
  59. $this->arguments[] = 'LANGUAGE_FIELD';
  60. $this->arguments[] = $languageAttribute;
  61. return $this;
  62. }
  63. /**
  64. * Default score for documents in the index.
  65. *
  66. * @param float $defaultScore
  67. * @return $this
  68. */
  69. public function score(float $defaultScore = 1.0): self
  70. {
  71. $this->arguments[] = 'SCORE';
  72. $this->arguments[] = $defaultScore;
  73. return $this;
  74. }
  75. /**
  76. * Document attribute that used as the document rank based on the user ranking.
  77. *
  78. * @param string $scoreAttribute
  79. * @return $this
  80. */
  81. public function scoreField(string $scoreAttribute): self
  82. {
  83. $this->arguments[] = 'SCORE_FIELD';
  84. $this->arguments[] = $scoreAttribute;
  85. return $this;
  86. }
  87. /**
  88. * Forces RediSearch to encode indexes as if there were more than 32 text attributes.
  89. *
  90. * @return $this
  91. */
  92. public function maxTextFields(): self
  93. {
  94. $this->arguments[] = 'MAXTEXTFIELDS';
  95. return $this;
  96. }
  97. /**
  98. * Does not store term offsets for documents.
  99. *
  100. * @return $this
  101. */
  102. public function noOffsets(): self
  103. {
  104. $this->arguments[] = 'NOOFFSETS';
  105. return $this;
  106. }
  107. /**
  108. * Creates a lightweight temporary index that expires after a specified period of inactivity, in seconds.
  109. *
  110. * @param int $seconds
  111. * @return $this
  112. */
  113. public function temporary(int $seconds): self
  114. {
  115. $this->arguments[] = 'TEMPORARY';
  116. $this->arguments[] = $seconds;
  117. return $this;
  118. }
  119. /**
  120. * Conserves storage space and memory by disabling highlighting support.
  121. *
  122. * @return $this
  123. */
  124. public function noHl(): self
  125. {
  126. $this->arguments[] = 'NOHL';
  127. return $this;
  128. }
  129. /**
  130. * Does not store attribute bits for each term.
  131. *
  132. * @return $this
  133. */
  134. public function noFields(): self
  135. {
  136. $this->arguments[] = 'NOFIELDS';
  137. return $this;
  138. }
  139. /**
  140. * Avoids saving the term frequencies in the index.
  141. *
  142. * @return $this
  143. */
  144. public function noFreqs(): self
  145. {
  146. $this->arguments[] = 'NOFREQS';
  147. return $this;
  148. }
  149. /**
  150. * Sets the index with a custom stopword list, to be ignored during indexing and search time.
  151. *
  152. * @param array $stopWords
  153. * @return $this
  154. */
  155. public function stopWords(array $stopWords): self
  156. {
  157. $this->arguments[] = 'STOPWORDS';
  158. $this->arguments[] = count($stopWords);
  159. $this->arguments = array_merge($this->arguments, $stopWords);
  160. return $this;
  161. }
  162. }