|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ensi\LaravelElasticQuery\Suggesting\Request; |
| 4 | + |
| 5 | +use Webmozart\Assert\Assert; |
| 6 | + |
| 7 | +class PhraseSuggester implements Suggester |
| 8 | +{ |
| 9 | + // basic phrase suggest api parameters |
| 10 | + protected ?string $text = null; |
| 11 | + protected ?int $gramSize = null; |
| 12 | + protected ?float $realWordErrorLikelihood = null; |
| 13 | + protected ?float $confidence = null; |
| 14 | + protected ?float $maxErrors = null; |
| 15 | + protected ?string $separator = null; |
| 16 | + protected ?int $size = null; |
| 17 | + protected ?string $analyzer = null; |
| 18 | + protected ?int $shardSize = null; |
| 19 | + protected ?string $highlightPreTag = null; |
| 20 | + protected ?string $highlightPostTag = null; |
| 21 | + |
| 22 | + public function __construct(protected string $name, protected string $field) |
| 23 | + { |
| 24 | + Assert::stringNotEmpty(trim($name)); |
| 25 | + Assert::stringNotEmpty(trim($field)); |
| 26 | + } |
| 27 | + |
| 28 | + public function toDSL(): array |
| 29 | + { |
| 30 | + return [ |
| 31 | + "text" => $this->text, |
| 32 | + "phrase" => array_filter([ |
| 33 | + "field" => $this->field, |
| 34 | + |
| 35 | + "gram_size" => $this->gramSize, |
| 36 | + "real_word_error_likelihood" => $this->realWordErrorLikelihood, |
| 37 | + "confidence" => $this->confidence, |
| 38 | + "max_errors" => $this->maxErrors, |
| 39 | + "separator" => $this->separator, |
| 40 | + "size" => $this->size, |
| 41 | + "analyzer" => $this->analyzer, |
| 42 | + "shard_size" => $this->shardSize, |
| 43 | + "highlight" => array_filter([ |
| 44 | + "pre_tag" => $this->highlightPreTag, |
| 45 | + "post_tag" => $this->highlightPostTag, |
| 46 | + ]) ?: null, |
| 47 | + // todo collate |
| 48 | + // todo smoothing models |
| 49 | + // todo direct_generator |
| 50 | + ]), |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + public function name(): string |
| 55 | + { |
| 56 | + return $this->name; |
| 57 | + } |
| 58 | + |
| 59 | + public function text(string $text): self |
| 60 | + { |
| 61 | + Assert::stringNotEmpty(trim($text)); |
| 62 | + |
| 63 | + $this->text = $text; |
| 64 | + |
| 65 | + return $this; |
| 66 | + } |
| 67 | + |
| 68 | + public function gramSize(int $gramSize): self |
| 69 | + { |
| 70 | + $this->gramSize = $gramSize; |
| 71 | + |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + public function realWordErrorLikelihood(float $realWordErrorLikelihood): self |
| 76 | + { |
| 77 | + $this->realWordErrorLikelihood = $realWordErrorLikelihood; |
| 78 | + |
| 79 | + return $this; |
| 80 | + } |
| 81 | + |
| 82 | + public function confidence(float $confidence): self |
| 83 | + { |
| 84 | + $this->confidence = $confidence; |
| 85 | + |
| 86 | + return $this; |
| 87 | + } |
| 88 | + |
| 89 | + public function maxErrors(float $maxErrors): self |
| 90 | + { |
| 91 | + $this->maxErrors = $maxErrors; |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + public function separator(string $separator): self |
| 97 | + { |
| 98 | + $this->separator = $separator; |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + public function size(int $size): self |
| 104 | + { |
| 105 | + $this->size = $size; |
| 106 | + |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + public function analyzer(string $analyzer): self |
| 111 | + { |
| 112 | + $this->analyzer = $analyzer; |
| 113 | + |
| 114 | + return $this; |
| 115 | + } |
| 116 | + |
| 117 | + public function shardSize(int $shardSize): self |
| 118 | + { |
| 119 | + $this->shardSize = $shardSize; |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | + |
| 124 | + public function highlight(string $highlightPreTag, string $highlightPostTag): self |
| 125 | + { |
| 126 | + $this->highlightPreTag = $highlightPreTag; |
| 127 | + $this->highlightPostTag = $highlightPostTag; |
| 128 | + |
| 129 | + return $this; |
| 130 | + } |
| 131 | +} |
0 commit comments