From 9eb573a7275adce880cc6a1c3e998459c174443b Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Tue, 25 Oct 2016 13:40:29 -0400 Subject: [PATCH] Split Create out to its own internal endpoint for simplicity --- src/Elasticsearch/Client.php | 7 +- src/Elasticsearch/Endpoints/Create.php | 107 +++++++++++++++++++++++++ src/Elasticsearch/Endpoints/Index.php | 16 ---- 3 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 src/Elasticsearch/Endpoints/Create.php diff --git a/src/Elasticsearch/Client.php b/src/Elasticsearch/Client.php index 31c0ad5cd..03993aab6 100644 --- a/src/Elasticsearch/Client.php +++ b/src/Elasticsearch/Client.php @@ -604,13 +604,12 @@ public function create($params) /** @var callback $endpointBuilder */ $endpointBuilder = $this->endpoints; - /** @var \Elasticsearch\Endpoints\Index $endpoint */ - $endpoint = $endpointBuilder('Index'); + /** @var \Elasticsearch\Endpoints\Create $endpoint */ + $endpoint = $endpointBuilder('Create'); $endpoint->setID($id) ->setIndex($index) ->setType($type) - ->setBody($body) - ->createIfAbsent(); + ->setBody($body); $endpoint->setParams($params); return $this->performRequest($endpoint); diff --git a/src/Elasticsearch/Endpoints/Create.php b/src/Elasticsearch/Endpoints/Create.php new file mode 100644 index 000000000..bbecabeb5 --- /dev/null +++ b/src/Elasticsearch/Endpoints/Create.php @@ -0,0 +1,107 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 + * @link http://elastic.co + */ +class Create extends AbstractEndpoint +{ + /** + * @param array $body + * + * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException + * @return $this + */ + public function setBody($body) + { + if (isset($body) !== true) { + return $this; + } + + $this->body = $body; + + return $this; + } + + /** + * @throws \Elasticsearch\Common\Exceptions\RuntimeException + * @return string + */ + public function getURI() + { + if (isset($this->index) !== true) { + throw new Exceptions\RuntimeException( + 'index is required for Create' + ); + } + + if (isset($this->type) !== true) { + throw new Exceptions\RuntimeException( + 'type is required for Create' + ); + } + + if (isset($this->id) !== true) { + throw new Exceptions\RuntimeException( + 'id is required for Create' + ); + } + + $id = $this->id; + $index = $this->index; + $type = $this->type; + return "/$index/$type/$id/_create"; + } + + /** + * @return string[] + */ + public function getParamWhitelist() + { + return array( + 'consistency', + 'op_type', + 'parent', + 'percolate', + 'refresh', + 'replication', + 'routing', + 'timeout', + 'timestamp', + 'ttl', + 'version', + 'version_type', + 'pipeline' + ); + } + + /** + * @return string + */ + public function getMethod() + { + return 'PUT'; + } + + /** + * @return array + * @throws \Elasticsearch\Common\Exceptions\RuntimeException + */ + public function getBody() + { + if (isset($this->body) !== true) { + throw new Exceptions\RuntimeException('Document body must be set for create request'); + } else { + return $this->body; + } + } +} diff --git a/src/Elasticsearch/Endpoints/Index.php b/src/Elasticsearch/Endpoints/Index.php index e800efb0a..ba3c82a0b 100644 --- a/src/Elasticsearch/Endpoints/Index.php +++ b/src/Elasticsearch/Endpoints/Index.php @@ -71,11 +71,6 @@ public function getURI() if (isset($id) === true) { $uri = "/$index/$type/$id"; } - - if ($this->createIfAbsent === true) { - $uri .= $this->addCreateFlag(); - } - return $uri; } @@ -125,15 +120,4 @@ public function getBody() return $this->body; } } - - private function addCreateFlag() - { - if (isset($this->id) === true) { - return '/_create'; - } else { - $this->params['op_type'] = 'create'; - - return ""; - } - } }