Skip to content

Commit 5163d52

Browse files
committed
CommonGrams analyzer
1 parent fa6f392 commit 5163d52

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)