Skip to content

Commit 4bd58a8

Browse files
committed
EdgeNgram analyzer
1 parent 5163d52 commit 4bd58a8

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Mapping\Analyzer\Custom;
4+
5+
class EdgeNgram implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface, \Spameri\ElasticQuery\Collection\Item
6+
{
7+
8+
/**
9+
* @var \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
10+
*/
11+
private $filter;
12+
13+
/**
14+
* @var int
15+
*/
16+
private $minGram;
17+
18+
/**
19+
* @var int
20+
*/
21+
private $maxGram;
22+
23+
/**
24+
* @var \Spameri\ElasticQuery\Mapping\Filter\Stop
25+
*/
26+
private $stopFilter;
27+
28+
29+
public function __construct(
30+
int $minGram = 2,
31+
int $maxGram = 6,
32+
?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL
33+
)
34+
{
35+
$this->minGram = $minGram;
36+
$this->maxGram = $maxGram;
37+
if ($stopFilter === NULL) {
38+
$stopFilter = new \Spameri\ElasticQuery\Mapping\Filter\Stop\Czech();
39+
}
40+
$this->stopFilter = $stopFilter;
41+
}
42+
43+
44+
public function key(): string
45+
{
46+
return $this->name();
47+
}
48+
49+
50+
public function name(): string
51+
{
52+
return 'customEdgeNgram';
53+
}
54+
55+
56+
public function getType(): string
57+
{
58+
return 'custom';
59+
}
60+
61+
62+
public function tokenizer(): string
63+
{
64+
return 'standard';
65+
}
66+
67+
68+
public function filter(): \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
69+
{
70+
if ( ! $this->filter instanceof \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection) {
71+
$this->filter = new \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection();
72+
$this->filter->add(
73+
new \Spameri\ElasticQuery\Mapping\Filter\ASCIIFolding()
74+
);
75+
$this->filter->add(
76+
new \Spameri\ElasticQuery\Mapping\Filter\Lowercase()
77+
);
78+
$this->filter->add($this->stopFilter);
79+
$this->filter->add(
80+
new \Spameri\ElasticQuery\Mapping\Filter\EdgeNgram($this->minGram, $this->maxGram)
81+
);
82+
$this->filter->add(
83+
new \Spameri\ElasticQuery\Mapping\Filter\Unique()
84+
);
85+
}
86+
87+
return $this->filter;
88+
}
89+
90+
91+
public function toArray(): array
92+
{
93+
$filterArray = [];
94+
/** @var \Spameri\ElasticQuery\Mapping\FilterInterface $filter */
95+
foreach ($this->filter() as $filter) {
96+
$filterArray[] = $filter->getType();
97+
}
98+
99+
return [
100+
$this->name() => [
101+
'type' => $this->getType(),
102+
'tokenizer' => $this->tokenizer(),
103+
'filter' => $filterArray,
104+
],
105+
];
106+
}
107+
108+
}

0 commit comments

Comments
 (0)