Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace IQDEV\ElasticSearchTests\Seed;
use IQDEV\ElasticSearch\Config\BaseConfiguration;
use IQDEV\ElasticSearch\Configuration;
use IQDEV\ElasticSearch\Document\ProductDocument;
use IQDEV\ElasticSearch\Facet\FacetCategory;
use IQDEV\ElasticSearch\Facet\FacetKeyword;
use IQDEV\ElasticSearch\Facet\FacetNumber;
use IQDEV\ElasticSearch\Indexer\AddIndex;
use IQDEV\ElasticSearch\Indexer\IndexProvider;
use IQDEV\ElasticSearch\Indexer\IndexRunner;
use IQDEV\ElasticSearchTests\Factory\ClientFactory;
use Psr\Log\Test\TestLogger;
class DefaultSeed
{
private IndexRunner $indexRunner;
private Configuration $configuration;
public function __construct()
{
$this->configuration = new BaseConfiguration();
$this->indexRunner = new IndexRunner(
ClientFactory::create(),
$this->configuration,
new TestLogger()
);
}
public function start()
{
$provider = new class implements IndexProvider {
public function get(): \Generator
{
$products = [
[
'id' => 's1',
'name' => 'Кроссовки NMD_R1 Boba Fett Spectoo',
'category' => 'shoes',
'properties' => ['brand' => 'adidas', 'color' => 'green', 'size' => 46,'price' => 100]
],
[
'id' => 's2',
'name' => 'КРОССОВКИ ULTRABOOST 5.0 DNA',
'category' => 'shoes',
'properties' => ['brand' => 'adidas', 'color' => 'red', 'size' => 47,'price' => 101]
],
[
'id' => 's3',
'name' => 'Кроссовки Reebok Royal Techque',
'category' => 'shoes',
'properties' => ['brand' => 'rebook', 'color' => 'blue', 'size' => 47,'price' => 102]
],
[
'id' => 's4',
'name' => 'Nike Air Zoom Pegasus 39',
'category' => 'shoes',
'properties' => ['brand' => 'nike', 'color' => 'green', 'size' => 43,'price' => 103]
],
[
'id' => 'h1',
'name' => 'Nike Dri-FIT Strike',
'category' => 't-short',
'properties' => ['brand' => 'nike', 'color' => 'red', 'size' => 'xl','price' => 104]
],
[
'id' => 'h2',
'name' => 'Nike Dri-FIT Rise 365',
'category' => 't-short',
'properties' => ['brand' => 'nike', 'color' => 'white', 'size' => 'xxl','price' => 105]
],
[
'id' => 'h3',
'name' => 'Компрессионная Футболка ACTIVCHILL Graphic Move',
'category' => 't-short',
'properties' => ['brand' => 'rebook', 'color' => 'white', 'size' => 'xl','price' => 106]
],
[
'id' => 'p1',
'name' => 'Товар с ценой',
'category' => 'prices',
'properties' => ['brand' => 'rebook', 'color' => 'white', 'size' => 'xl','price' => 107]
],
];
foreach ($products as $product) {
$document = new ProductDocument(new FacetCategory($product['category']));
//todo по-хорошему нужны базовые классы, которые будут описывать свойства
// и формировать структуру для последующей обработки
$document->setAdditionData([
'id' => $product['id'],
'title' => $product['name'],
]);
foreach ($product['properties'] as $key => $prop) {
if ($key === 'price') {
$document->getNumberFacets()->add(new FacetNumber($key, $prop));
} else {
$document->getKeywordFacets()->add(new FacetKeyword($key, $prop));
}
}
$document->setSearchContent($product['name']);
yield new AddIndex(
$_ENV['IQ_ES_PRODUCT_SEARCH_INDEX'],
$document,
$product['id']
);
}
}
public function setBatchSize(int $size): void
{
}
public function getBatchSize(): ?int
{
return null;
}
public function setLimit(int $limit): void
{
}
public function getLimit(): ?int
{
return null;
}
};
$this->indexRunner->run($provider);
}
}