Skip to content

Commit e3e07fb

Browse files
committed
WordDelimiter analyzer
1 parent 4bd58a8 commit e3e07fb

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)