|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Erichard\ElasticQueryBuilder\Query; |
| 4 | + |
| 5 | +use Erichard\ElasticQueryBuilder\Contracts\QueryInterface; |
| 6 | +use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch; |
| 7 | + |
| 8 | +class SimpleQueryStringQuery implements QueryInterface |
| 9 | +{ |
| 10 | + use HasMinimumShouldMatch; |
| 11 | + |
| 12 | + /** |
| 13 | + * @param mixed[]|string[] $fields |
| 14 | + */ |
| 15 | + public function __construct( |
| 16 | + protected array $fields, |
| 17 | + protected string $query, |
| 18 | + |
| 19 | + private ?string $flags = null, |
| 20 | + private ?bool $fuzzyTranspositions = null, |
| 21 | + private ?int $fuzzyMaxExpansions = null, |
| 22 | + private ?int $fuzzyPrefixLength = null, |
| 23 | + ?string $minimumShouldMatch = null, |
| 24 | + private ?string $defaultOperator = null, |
| 25 | + private ?string $analyzer = null, |
| 26 | + private ?bool $lenient = null, |
| 27 | + private ?string $quoteFieldSuffix = null, |
| 28 | + private ?bool $analyzeWildCard = null, |
| 29 | + private ?bool $autoGenerateSynonymsPhraseQuery = null, |
| 30 | + |
| 31 | + protected array $params = [], |
| 32 | + ) { |
| 33 | + $this->minimumShouldMatch = $minimumShouldMatch; |
| 34 | + } |
| 35 | + |
| 36 | + public function setFlags(string|null $flags): self |
| 37 | + { |
| 38 | + $this->flags = $flags; |
| 39 | + return $this; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + public function setFuzzyTranspositions(bool|null $fuzzyTranspositions): self |
| 44 | + { |
| 45 | + $this->fuzzyTranspositions = $fuzzyTranspositions; |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public function setFuzzyMaxExpansions(int|null $fuzzyMaxExpansions): self |
| 51 | + { |
| 52 | + $this->fuzzyMaxExpansions = $fuzzyMaxExpansions; |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public function setFuzzyPrefixLength(int|null $fuzzyPrefixLength): self |
| 58 | + { |
| 59 | + $this->fuzzyPrefixLength = $fuzzyPrefixLength; |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + public function setDefaultOperator(string|null $defaultOperator): self |
| 65 | + { |
| 66 | + $this->defaultOperator = $defaultOperator; |
| 67 | + return $this; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public function setAnalyzer(string|null $analyzer): self |
| 72 | + { |
| 73 | + $this->analyzer = $analyzer; |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public function setLenient(bool|null $lenient): self |
| 79 | + { |
| 80 | + $this->lenient = $lenient; |
| 81 | + return $this; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public function setQuoteFieldSuffix(string|null $quoteFieldSuffix): self |
| 86 | + { |
| 87 | + $this->quoteFieldSuffix = $quoteFieldSuffix; |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + public function setAnalyzeWildCard(bool|null $analyzeWildCard): self |
| 93 | + { |
| 94 | + $this->analyzeWildCard = $analyzeWildCard; |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + public function setAutoGenerateSynonymsPhraseQuery(bool|null $autoGenerateSynonymsPhraseQuery): self |
| 100 | + { |
| 101 | + $this->autoGenerateSynonymsPhraseQuery = $autoGenerateSynonymsPhraseQuery; |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + public function setFields(array $fields): self |
| 106 | + { |
| 107 | + $this->fields = $fields; |
| 108 | + |
| 109 | + return $this; |
| 110 | + } |
| 111 | + |
| 112 | + public function setQuery(string $query): self |
| 113 | + { |
| 114 | + $this->query = $query; |
| 115 | + |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + public function setParams(array $params): self |
| 120 | + { |
| 121 | + $this->params = $params; |
| 122 | + |
| 123 | + return $this; |
| 124 | + } |
| 125 | + |
| 126 | + public function build(): array |
| 127 | + { |
| 128 | + $data = [ |
| 129 | + 'query' => $this->query, |
| 130 | + 'fields' => $this->fields, |
| 131 | + ]; |
| 132 | + if (null !== $this->flags) { |
| 133 | + $data['flags'] = $this->flags; |
| 134 | + } |
| 135 | + if (null !== $this->fuzzyTranspositions) { |
| 136 | + $data['fuzzy_transpositions'] = $this->fuzzyTranspositions; |
| 137 | + } |
| 138 | + |
| 139 | + if (null !== $this->fuzzyMaxExpansions) { |
| 140 | + $data['fuzzy_max_expansions'] = $this->fuzzyMaxExpansions; |
| 141 | + } |
| 142 | + |
| 143 | + if (null !== $this->fuzzyPrefixLength) { |
| 144 | + $data['fuzzy_prefix_length'] = $this->fuzzyPrefixLength; |
| 145 | + } |
| 146 | + |
| 147 | + if (null !== $this->defaultOperator) { |
| 148 | + $data['default_operator'] = $this->defaultOperator; |
| 149 | + } |
| 150 | + |
| 151 | + if (null !== $this->analyzer) { |
| 152 | + $data['analyzer'] = $this->analyzer; |
| 153 | + } |
| 154 | + |
| 155 | + if (null !== $this->lenient) { |
| 156 | + $data['lenient'] = $this->lenient; |
| 157 | + } |
| 158 | + |
| 159 | + if (null !== $this->quoteFieldSuffix) { |
| 160 | + $data['quote_field_suffix'] = $this->quoteFieldSuffix; |
| 161 | + } |
| 162 | + |
| 163 | + if (null !== $this->analyzeWildCard) { |
| 164 | + $data['analyze_wildcard'] = $this->analyzeWildCard; |
| 165 | + } |
| 166 | + if (null !== $this->autoGenerateSynonymsPhraseQuery) { |
| 167 | + $data['auto_generate_synonyms_phrase_query'] = $this->autoGenerateSynonymsPhraseQuery; |
| 168 | + } |
| 169 | + |
| 170 | + $this->buildMinimumShouldMatchTo($data); |
| 171 | + |
| 172 | + $build = $this->params; |
| 173 | + $build['simple_query_string'] = $data; |
| 174 | + |
| 175 | + return $build; |
| 176 | + } |
| 177 | +} |
0 commit comments