1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Client; |
4
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Client\IndexClientInterface; |
6
|
|
|
use AmaTeam\ElasticSearch\API\IndexingInterface; |
7
|
|
|
use AmaTeam\ElasticSearch\API\MappingInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\Mapping\Operations as Mappings; |
9
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
10
|
|
|
use Elasticsearch\Client; |
11
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
12
|
|
|
|
13
|
|
|
class IndexClient implements IndexClientInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Client |
17
|
|
|
*/ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Client $client |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Client $client) |
24
|
|
|
{ |
25
|
|
|
$this->client = $client; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function create(string $index, IndexingInterface $indexing = null, array $mappings = []): void |
29
|
|
|
{ |
30
|
|
|
$payload = ['settings' => $indexing->getOptions(), 'mappings' => []]; |
|
|
|
|
31
|
|
|
foreach ($mappings as $type => $mapping) { |
32
|
|
|
$normalized = Mappings::toStdObject($mapping); |
33
|
|
|
unset($normalized->type); |
34
|
|
|
$payload['mappings'][$type] = $normalized; |
35
|
|
|
} |
36
|
|
|
foreach ($payload as $key => $value) { |
37
|
|
|
if (empty($value)) { |
38
|
|
|
unset($payload[$key]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
$parameters = ['index' => $index, 'body' => $payload]; |
42
|
|
|
$this->client->indices()->create($parameters); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function exists(string $index): bool |
46
|
|
|
{ |
47
|
|
|
return $this->client->indices()->exists(['index' => $index]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function delete(string $index): void |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
$this->client->delete(['index' => $index]); |
54
|
|
|
} catch (Missing404Exception $e) { |
55
|
|
|
// noop |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setMapping(string $index, string $type, MappingInterface $mapping): void |
60
|
|
|
{ |
61
|
|
|
$payload = Mappings::toStdObject($mapping); |
62
|
|
|
unset($payload->type); |
63
|
|
|
$parameters = ['index' => $index, 'type' => $type, 'body' => $payload]; |
64
|
|
|
$this->client->indices()->putMapping($parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getMapping(string $index, string $type): ?MappingInterface |
68
|
|
|
{ |
69
|
|
|
$data = $this->client->indices()->getMapping(['index' => $index, 'type' => $type]); |
70
|
|
|
$normalized = $data[$index]['mappings'][$type]; |
71
|
|
|
$mapping = Mappings::fromArray($normalized); |
72
|
|
|
$mapping->setType(RootType::ID); |
73
|
|
|
return $mapping; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setIndexing(string $index, IndexingInterface $indexing): void |
77
|
|
|
{ |
78
|
|
|
$parameters = ['index' => $index, 'body' => $indexing->getOptions()]; |
79
|
|
|
$this->client->indices()->putSettings($parameters); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.