Skip to content

Commit

Permalink
Remove references to _type field (deprecated in ES 7.0).
Browse files Browse the repository at this point in the history
For this change to work correctly a new serializer is needed (now should use the _class field inside the payload instead of _type)
  • Loading branch information
ninodafonte committed Dec 23, 2020
1 parent 77f9990 commit 2383965
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions src/ElasticSearchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,25 @@ public function __construct(
string $index,
string $class,
array $notAnalyzedFields = []
) {
)
{
$this->client = $client;
$this->serializer = $serializer;
$this->index = $index;
$this->class = $class;
$this->notAnalyzedFields = $notAnalyzedFields;
}

/**
* {@inheritdoc}
*/
public function save(Identifiable $data): void
{
Assertion::isInstanceOf($data, $this->class);

$serializedReadModel = $this->serializer->serialize($data);

$params = [
'index' => $this->index,
'type' => $serializedReadModel['class'],
'id' => $data->getId(),
'body' => $serializedReadModel['payload'],
'index' => $this->index,
'id' => $data->getId(),
'body' => $serializedReadModel['payload'],
'refresh' => true,
];

Expand All @@ -81,8 +78,7 @@ public function find($id): ?Identifiable
{
$params = [
'index' => $this->index,
'type' => $this->class,
'id' => (string) $id,
'id' => (string)$id,
];

try {
Expand Down Expand Up @@ -121,9 +117,8 @@ public function remove($id): void
{
try {
$this->client->delete([
'id' => (string) $id,
'index' => $this->index,
'type' => $this->class,
'id' => (string)$id,
'index' => $this->index,
'refresh' => true,
]);
} catch (Missing404Exception $e) { // It was already deleted or never existed, fine by us!
Expand All @@ -150,12 +145,11 @@ protected function search(array $query, array $facets = [], int $size = 500): ar
try {
return $this->client->search([
'index' => $this->index,
'type' => $this->class,
'body' => [
'query' => $query,
'body' => [
'query' => $query,
'facets' => $facets,
],
'size' => $size,
'size' => $size,
]);
} catch (Missing404Exception $e) {
return [];
Expand All @@ -167,11 +161,10 @@ protected function query(array $query): array
return $this->searchAndDeserializeHits(
[
'index' => $this->index,
'type' => $this->class,
'body' => [
'body' => [
'query' => $query,
],
'size' => 500,
'size' => 500,
]
);
}
Expand All @@ -196,7 +189,6 @@ private function deserializeHit(array $hit): Identifiable
{
return $this->serializer->deserialize(
[
'class' => $hit['_type'],
'payload' => $hit['_source'],
]
);
Expand Down Expand Up @@ -225,8 +217,6 @@ private function buildFilter(array $filter): array
*/
public function createIndex(): bool
{
$class = $this->class;

$indexParams = [
'index' => $this->index,
];
Expand All @@ -244,9 +234,9 @@ public function createIndex(): bool

$this->client->indices()->create($indexParams);
$response = $this->client->cluster()->health([
'index' => $this->index,
'index' => $this->index,
'wait_for_status' => 'yellow',
'timeout' => '5s',
'timeout' => '5s',
]);

return isset($response['status']) && 'red' !== $response['status'];
Expand All @@ -258,16 +248,16 @@ public function createIndex(): bool
public function deleteIndex(): bool
{
$indexParams = [
'index' => $this->index,
'index' => $this->index,
'timeout' => '5s',
];

$this->client->indices()->delete($indexParams);

$response = $this->client->cluster()->health([
'index' => $this->index,
'index' => $this->index,
'wait_for_status' => 'yellow',
'timeout' => '5s',
'timeout' => '5s',
]);

return isset($response['status']) && 'red' !== $response['status'];
Expand Down

0 comments on commit 2383965

Please sign in to comment.