Skip to content

Commit a51b4d4

Browse files
committed
SynonymAnalyzer and czech implementation
1 parent 89d9e5c commit a51b4d4

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Mapping\Analyzer\Custom\Synonym;
4+
5+
abstract class AbstractSynonym
6+
implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface,
7+
\Spameri\ElasticQuery\Collection\Item
8+
{
9+
10+
/**
11+
* @var \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
12+
*/
13+
protected $filter;
14+
15+
/**
16+
* @var \Spameri\ElasticQuery\Mapping\Filter\Stop
17+
*/
18+
protected $stopFilter;
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $synonyms;
24+
25+
26+
public function __construct(
27+
?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL,
28+
array $synonyms
29+
)
30+
{
31+
$this->stopFilter = $stopFilter;
32+
$this->synonyms = $synonyms;
33+
}
34+
35+
36+
public function key() : string
37+
{
38+
return $this->name();
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 toArray() : array
55+
{
56+
$filterArray = [];
57+
/** @var \Spameri\ElasticQuery\Mapping\FilterInterface $filter */
58+
foreach ($this->filter() as $filter) {
59+
if ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\Synonym) {
60+
$filterArray[] = $filter->getName();
61+
62+
} elseif ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\Stop) {
63+
$filterArray[] = $filter->getName();
64+
65+
} else {
66+
$filterArray[] = $filter->getType();
67+
}
68+
}
69+
70+
return [
71+
$this->name() => [
72+
'type' => $this->getType(),
73+
'tokenizer' => $this->tokenizer(),
74+
'filter' => $filterArray,
75+
],
76+
];
77+
}
78+
79+
80+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Mapping\Analyzer\Custom\Synonym;
4+
5+
class CzechSynonym extends \Spameri\ElasticQuery\Mapping\Analyzer\Custom\Synonym\AbstractSynonym
6+
{
7+
8+
public function name(): string
9+
{
10+
return 'czechSynonym';
11+
}
12+
13+
14+
public function filter(): \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection
15+
{
16+
if ( ! $this->filter instanceof \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection) {
17+
$this->filter = new \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection();
18+
$this->filter->add(
19+
new \Spameri\ElasticQuery\Mapping\Filter\Lowercase()
20+
);
21+
if ($this->stopFilter) {
22+
$this->filter->add($this->stopFilter);
23+
24+
} else {
25+
$this->filter->add(
26+
new \Spameri\ElasticQuery\Mapping\Filter\Stop\Czech()
27+
);
28+
}
29+
$this->filter->add(
30+
new \Spameri\ElasticQuery\Mapping\Filter\Synonym(
31+
$this->synonyms
32+
)
33+
);
34+
$this->filter->add(
35+
new \Spameri\ElasticQuery\Mapping\Filter\Lowercase()
36+
);
37+
if ($this->stopFilter) {
38+
$this->filter->add($this->stopFilter);
39+
40+
} else {
41+
$this->filter->add(
42+
new \Spameri\ElasticQuery\Mapping\Filter\Stop\Czech()
43+
);
44+
}
45+
$this->filter->add(
46+
new \Spameri\ElasticQuery\Mapping\Filter\Unique()
47+
);
48+
$this->filter->add(
49+
new \Spameri\ElasticQuery\Mapping\Filter\ASCIIFolding()
50+
);
51+
}
52+
53+
return $this->filter;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)