From 9be27e6bbf605f1d1f1303398e4d9ea04edcbb0b Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Sat, 22 Feb 2020 23:47:02 +0100 Subject: [PATCH] TASK: Automatically set NEOS_TYPE_FIELD on getData --- Classes/Domain/Model/Document.php | 62 +++++++++++++++------------- Classes/Domain/Model/GenericType.php | 8 ---- Classes/Domain/Model/Mapping.php | 6 +-- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/Classes/Domain/Model/Document.php b/Classes/Domain/Model/Document.php index 4e0401b..5c23da4 100644 --- a/Classes/Domain/Model/Document.php +++ b/Classes/Domain/Model/Document.php @@ -50,7 +50,7 @@ class Document * With a fresh instance of this document, or a conducted change, this flag gets set to TRUE again. * When retrieved from the storage, or successfully set to the storage, it's FALSE. * - * @var boolean + * @var bool */ protected $dirty = true; @@ -60,7 +60,7 @@ class Document * @param string $id * @param int $version */ - public function __construct(AbstractType $type, array $data = null, $id = null, $version = null) + public function __construct(AbstractType $type, ?array $data = null, ?string $id = null, ?int $version = null) { $this->type = $type; $this->data = $data; @@ -84,10 +84,11 @@ public function __clone() /** * Stores this document. If ID is given, PUT will be used; else POST * - * @throws ElasticSearchException * @return void + * @throws \Neos\Flow\Http\Exception + * @throws ElasticSearchException */ - public function store() + public function store(): void { if ($this->id !== null) { $method = 'PUT'; @@ -96,7 +97,8 @@ public function store() $method = 'POST'; $path = ''; } - $response = $this->request($method, $path, [], json_encode($this->data)); + + $response = $this->request($method, $path, [], json_encode($this->getData())); $treatedContent = $response->getTreatedContent(); $this->id = $treatedContent['_id']; @@ -104,20 +106,6 @@ public function store() $this->dirty = false; } - /** - * @param string $method - * @param string $path - * @param array $arguments - * @param string $content - * @return Response - * @throws ElasticSearchException - * @throws \Neos\Flow\Http\Exception - */ - protected function request(string $method, ?string $path = null, array $arguments = [], ?string $content = null): Response - { - return $this->type->request($method, $path, $arguments, $content); - } - /** * @return boolean */ @@ -126,15 +114,6 @@ public function isDirty(): bool return $this->dirty; } - /** - * @param boolean $dirty - * @return void - */ - protected function setDirty(bool $dirty = true): void - { - $this->dirty = $dirty; - } - /** * @return int */ @@ -150,6 +129,7 @@ public function getVersion(): int */ public function getData(): array { + $this->data[Mapping::NEOS_TYPE_FIELD] = $this->type->getName(); return $this->data; } @@ -179,7 +159,7 @@ public function getId(): string * @return mixed * @throws ElasticSearchException */ - public function getField(string $fieldName, $silent = false) + public function getField(string $fieldName, bool $silent = false) { if (!array_key_exists($fieldName, $this->data) && $silent === false) { throw new ElasticSearchException(sprintf('The field %s was not present in data of document in %s/%s.', $fieldName, $this->type->getIndex()->getName(), $this->type->getName()), 1340274696); @@ -195,4 +175,28 @@ public function getType(): AbstractType { return $this->type; } + + /** + * @param string $method + * @param string $path + * @param array $arguments + * @param string $content + * @return Response + * @throws ElasticSearchException + * @throws \Neos\Flow\Http\Exception + */ + protected function request(string $method, ?string $path = null, array $arguments = [], ?string $content = null): Response + { + return $this->type->request($method, $path, $arguments, $content); + } + + /** + * @param boolean $dirty + * @return void + */ + protected function setDirty(bool $dirty = true): void + { + $this->dirty = $dirty; + } + } diff --git a/Classes/Domain/Model/GenericType.php b/Classes/Domain/Model/GenericType.php index b09e36b..0d20643 100644 --- a/Classes/Domain/Model/GenericType.php +++ b/Classes/Domain/Model/GenericType.php @@ -18,12 +18,4 @@ */ class GenericType extends AbstractType { - /** - * @param Index $index - * @param string $name - */ - public function __construct(Index $index, $name = null) - { - parent::__construct($index, $name); - } } diff --git a/Classes/Domain/Model/Mapping.php b/Classes/Domain/Model/Mapping.php index 95f51e4..8db25d1 100644 --- a/Classes/Domain/Model/Mapping.php +++ b/Classes/Domain/Model/Mapping.php @@ -22,7 +22,7 @@ class Mapping { - protected const TYPE_PROPERTY_NAME = '_neos_type'; + public const NEOS_TYPE_FIELD = '_neosType'; /** * @var AbstractType @@ -55,7 +55,7 @@ class Mapping public function __construct(AbstractType $type) { $this->type = $type; - $this->properties[static::TYPE_PROPERTY_NAME] = ['type' => 'keyword']; + $this->properties[static::NEOS_TYPE_FIELD] = ['type' => 'keyword']; } /** @@ -100,7 +100,7 @@ public function apply(): Response { $content = json_encode($this->asArray()); - return $this->type->request('PUT', '/_mapping', [], $content); + return $this->type->request('PUT', '/_mapping', ['include_type_name' => 'false'], $content); } /**