Skip to content

Commit d37c14e

Browse files
committed
#107572
1 parent 4ac2aea commit d37c14e

File tree

11 files changed

+236
-108
lines changed

11 files changed

+236
-108
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
trait WithMaxEdits
8+
{
9+
protected ?int $maxEdits = null;
10+
11+
public function maxEdits(int $maxEdits): static
12+
{
13+
Assert::range($maxEdits, 1, 2);
14+
15+
$this->maxEdits = $maxEdits;
16+
17+
return $this;
18+
}
19+
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
trait WithMaxInspections
6+
{
7+
protected ?int $maxInspections = null;
8+
9+
public function maxInspections(int $maxInspections): static
10+
{
11+
$this->maxInspections = $maxInspections;
12+
13+
return $this;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
trait WithMaxTermFreq
6+
{
7+
protected ?int $maxTermFreq = null;
8+
9+
public function maxTermFreq(int $maxTermFreq): static
10+
{
11+
$this->maxTermFreq = $maxTermFreq;
12+
13+
return $this;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
trait WithMinDocFreq
6+
{
7+
protected ?int $minDocFreq = null;
8+
9+
public function minDocFreq(int $minDocFreq): static
10+
{
11+
$this->minDocFreq = $minDocFreq;
12+
13+
return $this;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
trait WithMinWordLength
6+
{
7+
protected ?int $minWordLength = null;
8+
9+
public function minWordLength(int $minWordLength): static
10+
{
11+
$this->minWordLength = $minWordLength;
12+
13+
return $this;
14+
}
15+
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
trait WithPrefixLength
6+
{
7+
protected ?int $prefixLength = null;
8+
9+
public function prefixLength(int $prefixLength): static
10+
{
11+
$this->prefixLength = $prefixLength;
12+
13+
return $this;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
trait WithSize
6+
{
7+
protected ?int $size = null;
8+
9+
public function size(int $size): static
10+
{
11+
$this->size = $size;
12+
13+
return $this;
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request\Concerns;
4+
5+
use Ensi\LaravelElasticQuery\Suggesting\Enums\SuggestMode;
6+
7+
trait WithSuggestMode
8+
{
9+
protected ?string $suggestMode = null;
10+
11+
public function suggestModeMissing(): static
12+
{
13+
$this->suggestMode = SuggestMode::MISSING;
14+
15+
return $this;
16+
}
17+
18+
public function suggestModePopular(): static
19+
{
20+
$this->suggestMode = SuggestMode::POPULAR;
21+
22+
return $this;
23+
}
24+
25+
public function suggestModeAlways(): static
26+
{
27+
$this->suggestMode = SuggestMode::ALWAYS;
28+
29+
return $this;
30+
}
31+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Suggesting\Request;
4+
5+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithMaxEdits;
6+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithMaxInspections;
7+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithMaxTermFreq;
8+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithMinDocFreq;
9+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithMinWordLength;
10+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithPrefixLength;
11+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithSize;
12+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithSuggestMode;
13+
14+
class DirectGenerator
15+
{
16+
use WithSuggestMode, WithSize, WithMaxEdits, WithPrefixLength, WithMinWordLength, WithMaxInspections, WithMinDocFreq, WithMaxTermFreq;
17+
protected ?string $preFilter = null;
18+
protected ?string $postFilter = null;
19+
20+
public function __construct(protected string $field)
21+
{
22+
}
23+
24+
public function toDSL(): array
25+
{
26+
return array_filter([
27+
"field" => $this->field,
28+
"size" => $this->size,
29+
"suggest_mode" => $this->suggestMode,
30+
"max_edits" => $this->maxEdits,
31+
"prefix_length" => $this->prefixLength,
32+
"min_word_length" => $this->minWordLength,
33+
"max_inspections" => $this->maxInspections,
34+
"min_doc_freq" => $this->minDocFreq,
35+
"max_term_freq" => $this->maxTermFreq,
36+
"pre_filter" => $this->preFilter,
37+
"post_filter" => $this->postFilter,
38+
]);
39+
}
40+
41+
public function preFilter(string $preFilter): static
42+
{
43+
$this->preFilter = $preFilter;
44+
45+
return $this;
46+
}
47+
48+
public function postFilter(string $postFilter): static
49+
{
50+
$this->postFilter = $postFilter;
51+
52+
return $this;
53+
}
54+
}

src/Suggesting/Request/PhraseSuggester.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
namespace Ensi\LaravelElasticQuery\Suggesting\Request;
44

5+
use Ensi\LaravelElasticQuery\Suggesting\Request\Concerns\WithSize;
56
use Webmozart\Assert\Assert;
67

78
class PhraseSuggester implements Suggester
89
{
10+
use WithSize;
11+
912
// basic phrase suggest api parameters
1013
protected ?string $text = null;
1114
protected ?int $gramSize = null;
1215
protected ?float $realWordErrorLikelihood = null;
1316
protected ?float $confidence = null;
1417
protected ?float $maxErrors = null;
1518
protected ?string $separator = null;
16-
protected ?int $size = null;
1719
protected ?string $analyzer = null;
1820
protected ?int $shardSize = null;
1921
protected ?string $highlightPreTag = null;
2022
protected ?string $highlightPostTag = null;
23+
protected array $directGenerators = [];
2124

2225
public function __construct(protected string $name, protected string $field)
2326
{
@@ -44,9 +47,9 @@ public function toDSL(): array
4447
"pre_tag" => $this->highlightPreTag,
4548
"post_tag" => $this->highlightPostTag,
4649
]) ?: null,
50+
"direct_generator" => array_map(fn(DirectGenerator $g) => $g->toDSL(), $this->directGenerators),
4751
// todo collate
4852
// todo smoothing models
49-
// todo direct_generator
5053
]),
5154
];
5255
}
@@ -56,7 +59,7 @@ public function name(): string
5659
return $this->name;
5760
}
5861

59-
public function text(string $text): self
62+
public function text(string $text): static
6063
{
6164
Assert::stringNotEmpty(trim($text));
6265

@@ -65,66 +68,66 @@ public function text(string $text): self
6568
return $this;
6669
}
6770

68-
public function gramSize(int $gramSize): self
71+
public function gramSize(int $gramSize): static
6972
{
7073
$this->gramSize = $gramSize;
7174

7275
return $this;
7376
}
7477

75-
public function realWordErrorLikelihood(float $realWordErrorLikelihood): self
78+
public function realWordErrorLikelihood(float $realWordErrorLikelihood): static
7679
{
7780
$this->realWordErrorLikelihood = $realWordErrorLikelihood;
7881

7982
return $this;
8083
}
8184

82-
public function confidence(float $confidence): self
85+
public function confidence(float $confidence): static
8386
{
8487
$this->confidence = $confidence;
8588

8689
return $this;
8790
}
8891

89-
public function maxErrors(float $maxErrors): self
92+
public function maxErrors(float $maxErrors): static
9093
{
9194
$this->maxErrors = $maxErrors;
9295

9396
return $this;
9497
}
9598

96-
public function separator(string $separator): self
99+
public function separator(string $separator): static
97100
{
98101
$this->separator = $separator;
99102

100103
return $this;
101104
}
102105

103-
public function size(int $size): self
106+
public function analyzer(string $analyzer): static
104107
{
105-
$this->size = $size;
108+
$this->analyzer = $analyzer;
106109

107110
return $this;
108111
}
109112

110-
public function analyzer(string $analyzer): self
113+
public function shardSize(int $shardSize): static
111114
{
112-
$this->analyzer = $analyzer;
115+
$this->shardSize = $shardSize;
113116

114117
return $this;
115118
}
116119

117-
public function shardSize(int $shardSize): self
120+
public function highlight(string $highlightPreTag, string $highlightPostTag): static
118121
{
119-
$this->shardSize = $shardSize;
122+
$this->highlightPreTag = $highlightPreTag;
123+
$this->highlightPostTag = $highlightPostTag;
120124

121125
return $this;
122126
}
123127

124-
public function highlight(string $highlightPreTag, string $highlightPostTag): self
128+
public function addDirectGenerator(DirectGenerator $directGenerator): static
125129
{
126-
$this->highlightPreTag = $highlightPreTag;
127-
$this->highlightPostTag = $highlightPostTag;
130+
$this->directGenerators[] = $directGenerator;
128131

129132
return $this;
130133
}

0 commit comments

Comments
 (0)