-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_index.php
112 lines (99 loc) · 2.46 KB
/
prepare_index.php
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
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()
->setHosts(['elasticsearch:9200'])
->build();
create_index($client);
insert_one($client);
insert_many($client);
function create_index(Client $client): void
{
$params = [
'index' => 'airports_php',
'body' => [
'mappings' => [
'properties' => [
'code' => [
'type' => 'keyword'
],
'name' => [
'type' => 'text'
],
'city' => [
'type' => 'text'
],
'country' => [
'type' => 'text'
]
]
]
]
];
$client->indices()->create($params);
}
function insert_one(Client $client): void
{
$params = [
'index' => 'airports_php',
'body' => [
'code' => 'KTW',
'name' => 'Pyrzowice',
'city' => 'Katowice',
'country' => 'Polska'
]
];
$client->index($params);
}
function insert_many(Client $client): void
{
$params = [];
$airports = [
[
'code' => 'KRK',
'name' => 'Balice',
'city' => 'Kraków',
'country' => 'Polska'
],
[
'code' => 'WMI',
'name' => 'Modlin',
'city' => 'Warszawa',
'country' => 'Polska'
],
[
'code' => 'WAW',
'name' => 'Okęcie',
'city' => 'Warszawa',
'country' => 'Polska'
],
[
'code' => 'WRO',
'name' => 'Strachowice',
'city' => 'Wrocław',
'country' => 'Polska'
],
[
'code' => 'IEG',
'name' => 'Babimost',
'city' => 'Zielona Góra',
'country' => 'Polska'
]
];
foreach ($airports as $airport) {
$params['body'][] = [
'index' => [
'_index' => 'airports_php',
]
];
$params['body'][] = [
'code' => $airport['code'],
'name' => $airport['name'],
'city' => $airport['city'],
'country' => $airport['country']
];
}
$client->bulk($params);
}