Skip to content

Commit 9eb573a

Browse files
committed
Split Create out to its own internal endpoint for simplicity
1 parent 6c77f62 commit 9eb573a

File tree

3 files changed

+110
-20
lines changed

3 files changed

+110
-20
lines changed

src/Elasticsearch/Client.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,12 @@ public function create($params)
604604
/** @var callback $endpointBuilder */
605605
$endpointBuilder = $this->endpoints;
606606

607-
/** @var \Elasticsearch\Endpoints\Index $endpoint */
608-
$endpoint = $endpointBuilder('Index');
607+
/** @var \Elasticsearch\Endpoints\Create $endpoint */
608+
$endpoint = $endpointBuilder('Create');
609609
$endpoint->setID($id)
610610
->setIndex($index)
611611
->setType($type)
612-
->setBody($body)
613-
->createIfAbsent();
612+
->setBody($body);
614613
$endpoint->setParams($params);
615614

616615
return $this->performRequest($endpoint);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Elasticsearch\Endpoints;
4+
5+
use Elasticsearch\Common\Exceptions;
6+
7+
/**
8+
* Class Create
9+
*
10+
* @category Elasticsearch
11+
* @package Elasticsearch\Endpoints
12+
* @author Zachary Tong <zach@elastic.co>
13+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
14+
* @link http://elastic.co
15+
*/
16+
class Create extends AbstractEndpoint
17+
{
18+
/**
19+
* @param array $body
20+
*
21+
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
22+
* @return $this
23+
*/
24+
public function setBody($body)
25+
{
26+
if (isset($body) !== true) {
27+
return $this;
28+
}
29+
30+
$this->body = $body;
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
37+
* @return string
38+
*/
39+
public function getURI()
40+
{
41+
if (isset($this->index) !== true) {
42+
throw new Exceptions\RuntimeException(
43+
'index is required for Create'
44+
);
45+
}
46+
47+
if (isset($this->type) !== true) {
48+
throw new Exceptions\RuntimeException(
49+
'type is required for Create'
50+
);
51+
}
52+
53+
if (isset($this->id) !== true) {
54+
throw new Exceptions\RuntimeException(
55+
'id is required for Create'
56+
);
57+
}
58+
59+
$id = $this->id;
60+
$index = $this->index;
61+
$type = $this->type;
62+
return "/$index/$type/$id/_create";
63+
}
64+
65+
/**
66+
* @return string[]
67+
*/
68+
public function getParamWhitelist()
69+
{
70+
return array(
71+
'consistency',
72+
'op_type',
73+
'parent',
74+
'percolate',
75+
'refresh',
76+
'replication',
77+
'routing',
78+
'timeout',
79+
'timestamp',
80+
'ttl',
81+
'version',
82+
'version_type',
83+
'pipeline'
84+
);
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getMethod()
91+
{
92+
return 'PUT';
93+
}
94+
95+
/**
96+
* @return array
97+
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
98+
*/
99+
public function getBody()
100+
{
101+
if (isset($this->body) !== true) {
102+
throw new Exceptions\RuntimeException('Document body must be set for create request');
103+
} else {
104+
return $this->body;
105+
}
106+
}
107+
}

src/Elasticsearch/Endpoints/Index.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ public function getURI()
7171
if (isset($id) === true) {
7272
$uri = "/$index/$type/$id";
7373
}
74-
75-
if ($this->createIfAbsent === true) {
76-
$uri .= $this->addCreateFlag();
77-
}
78-
7974
return $uri;
8075
}
8176

@@ -125,15 +120,4 @@ public function getBody()
125120
return $this->body;
126121
}
127122
}
128-
129-
private function addCreateFlag()
130-
{
131-
if (isset($this->id) === true) {
132-
return '/_create';
133-
} else {
134-
$this->params['op_type'] = 'create';
135-
136-
return "";
137-
}
138-
}
139123
}

0 commit comments

Comments
 (0)