|
|
@ -43,7 +43,11 @@ class Command extends Component |
|
|
|
|
|
|
|
|
|
|
|
public $options = []; |
|
|
|
public $options = []; |
|
|
|
|
|
|
|
|
|
|
|
public function queryAll($options = []) |
|
|
|
/** |
|
|
|
|
|
|
|
* @param array $options |
|
|
|
|
|
|
|
* @return mixed |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function search($options = []) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$query = $this->queryParts; |
|
|
|
$query = $this->queryParts; |
|
|
|
if (empty($query)) { |
|
|
|
if (empty($query)) { |
|
|
@ -57,23 +61,9 @@ class Command extends Component |
|
|
|
$this->type !== null ? $this->type : '_all', |
|
|
|
$this->type !== null ? $this->type : '_all', |
|
|
|
'_search' |
|
|
|
'_search' |
|
|
|
]; |
|
|
|
]; |
|
|
|
try { |
|
|
|
return $this->db->get($url, array_merge($this->options, $options), $query)['hits']; |
|
|
|
$response = $this->db->http()->post($this->createUrl($url, $options), null, $query)->send(); |
|
|
|
|
|
|
|
} catch(ClientErrorResponseException $e) { |
|
|
|
|
|
|
|
throw new Exception("elasticsearch error:\n\n" |
|
|
|
|
|
|
|
. $query . "\n\n" . $e->getMessage() |
|
|
|
|
|
|
|
. print_r(Json::decode($e->getResponse()->getBody(true)), true), [], 0, $e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Json::decode($response->getBody(true))['hits']; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function queryCount($options = []) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$options['search_type'] = 'count'; |
|
|
|
|
|
|
|
return $this->queryAll($options); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Inserts a document into an index |
|
|
|
* Inserts a document into an index |
|
|
|
* @param string $index |
|
|
|
* @param string $index |
|
|
@ -88,18 +78,11 @@ class Command extends Component |
|
|
|
{ |
|
|
|
{ |
|
|
|
$body = is_array($data) ? Json::encode($data) : $data; |
|
|
|
$body = is_array($data) ? Json::encode($data) : $data; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if ($id !== null) { |
|
|
|
if ($id !== null) { |
|
|
|
$response = $this->db->http()->put($this->createUrl([$index, $type, $id], $options), null, $body)->send(); |
|
|
|
return $this->db->put([$index, $type, $id], $options, $body); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$response = $this->db->http()->post($this->createUrl([$index, $type], $options), null, $body)->send(); |
|
|
|
return $this->db->post([$index, $type], $options, $body); |
|
|
|
} |
|
|
|
|
|
|
|
} catch(ClientErrorResponseException $e) { |
|
|
|
|
|
|
|
throw new Exception("elasticsearch error:\n\n" |
|
|
|
|
|
|
|
. $body . "\n\n" . $e->getMessage() |
|
|
|
|
|
|
|
. print_r(Json::decode($e->getResponse()->getBody(true)), true), [], 0, $e); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -113,15 +96,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function get($index, $type, $id, $options = []) |
|
|
|
public function get($index, $type, $id, $options = []) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$httpOptions = [ |
|
|
|
return $this->db->get([$index, $type, $id], $options, null, [200, 404]); |
|
|
|
'exceptions' => false, |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
$response = $this->db->http()->get($this->createUrl([$index, $type, $id], $options), null, $httpOptions)->send(); |
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200 || $response->getStatusCode() == 404) { |
|
|
|
|
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw new Exception('Elasticsearch request failed.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -133,25 +108,12 @@ class Command extends Component |
|
|
|
* @param $id |
|
|
|
* @param $id |
|
|
|
* @param array $options |
|
|
|
* @param array $options |
|
|
|
* @return mixed |
|
|
|
* @return mixed |
|
|
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html |
|
|
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function mget($index, $type, $ids, $options = []) |
|
|
|
public function mget($index, $type, $ids, $options = []) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$httpOptions = [ |
|
|
|
|
|
|
|
'exceptions' => false, |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
$body = Json::encode(['ids' => array_values($ids)]); |
|
|
|
$body = Json::encode(['ids' => array_values($ids)]); |
|
|
|
$response = $this->db->http()->post( // TODO guzzle does not manage to send get request with content |
|
|
|
return $this->db->get([$index, $type, '_mget'], $options, $body); |
|
|
|
$this->createUrl([$index, $type, '_mget'], $options), |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
$body, |
|
|
|
|
|
|
|
$httpOptions |
|
|
|
|
|
|
|
)->send(); |
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) { |
|
|
|
|
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw new Exception('Elasticsearch request failed.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -164,12 +126,9 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getSource($index, $type, $id) |
|
|
|
public function getSource($index, $type, $id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->head($this->createUrl([$index, $type, $id]))->send(); |
|
|
|
return $this->db->get([$index, $type, $id]); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO mget http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* gets a document from the index |
|
|
|
* gets a document from the index |
|
|
|
* @param $index |
|
|
|
* @param $index |
|
|
@ -180,8 +139,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function exists($index, $type, $id) |
|
|
|
public function exists($index, $type, $id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->head($this->createUrl([$index, $type, $id]))->send(); |
|
|
|
return $this->db->head([$index, $type, $id]); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -195,8 +153,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function delete($index, $type, $id, $options = []) |
|
|
|
public function delete($index, $type, $id, $options = []) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->delete($this->createUrl([$index, $type, $id], $options))->send(); |
|
|
|
return $this->db->delete([$index, $type, $id], $options); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -211,21 +168,18 @@ class Command extends Component |
|
|
|
public function update($index, $type, $id, $data, $options = []) |
|
|
|
public function update($index, $type, $id, $data, $options = []) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// TODO |
|
|
|
// TODO |
|
|
|
$response = $this->db->http()->delete($this->createUrl([$index, $type, $id], $options))->send(); |
|
|
|
// return $this->db->delete([$index, $type, $id], $options); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO bulk http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html |
|
|
|
// TODO bulk http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html |
|
|
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function createIndex($index, $configuration = null) |
|
|
|
public function createIndex($index, $configuration = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$body = $configuration !== null ? Json::encode($configuration) : null; |
|
|
|
$body = $configuration !== null ? Json::encode($configuration) : null; |
|
|
|
$response = $this->db->http()->put($this->createUrl([$index]), null, $body)->send(); |
|
|
|
return $this->db->put([$index], $body); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -233,8 +187,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function deleteIndex($index) |
|
|
|
public function deleteIndex($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->delete($this->createUrl([$index]))->send(); |
|
|
|
return $this->db->delete([$index]); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -242,8 +195,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function deleteAllIndexes() |
|
|
|
public function deleteAllIndexes() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->delete($this->createUrl(['_all']))->send(); |
|
|
|
return $this->db->delete(['_all']); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -251,8 +203,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function indexExists($index) |
|
|
|
public function indexExists($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->head($this->createUrl([$index]))->send(); |
|
|
|
return $this->db->head([$index]); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -260,8 +211,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function typeExists($index, $type) |
|
|
|
public function typeExists($index, $type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->head($this->createUrl([$index, $type]))->send(); |
|
|
|
return $this->db->head([$index, $type]); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html |
|
|
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html |
|
|
@ -276,8 +226,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function openIndex($index) |
|
|
|
public function openIndex($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->post($this->createUrl([$index, '_open']))->send(); |
|
|
|
return $this->db->post([$index, '_open']); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -285,8 +234,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function closeIndex($index) |
|
|
|
public function closeIndex($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->post($this->createUrl([$index, '_close']))->send(); |
|
|
|
return $this->db->post([$index, '_close']); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -294,8 +242,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getIndexStatus($index = '_all') |
|
|
|
public function getIndexStatus($index = '_all') |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->get($this->createUrl([$index, '_status']))->send(); |
|
|
|
return $this->db->get([$index, '_status']); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-stats.html |
|
|
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-stats.html |
|
|
@ -306,8 +253,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function clearIndexCache($index) |
|
|
|
public function clearIndexCache($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->post($this->createUrl([$index, '_cache', 'clear']))->send(); |
|
|
|
return $this->db->post([$index, '_cache', 'clear']); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -315,8 +261,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function flushIndex($index = '_all') |
|
|
|
public function flushIndex($index = '_all') |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->post($this->createUrl([$index, '_flush']))->send(); |
|
|
|
return $this->db->post([$index, '_flush']); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -324,8 +269,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function refreshIndex($index) |
|
|
|
public function refreshIndex($index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->post($this->createUrl([$index, '_refresh']))->send(); |
|
|
|
return $this->db->post([$index, '_refresh']); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html |
|
|
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html |
|
|
@ -338,8 +282,7 @@ class Command extends Component |
|
|
|
public function setMapping($index, $type, $mapping) |
|
|
|
public function setMapping($index, $type, $mapping) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$body = $mapping !== null ? Json::encode($mapping) : null; |
|
|
|
$body = $mapping !== null ? Json::encode($mapping) : null; |
|
|
|
$response = $this->db->http()->put($this->createUrl([$index, $type, '_mapping']), null, $body)->send(); |
|
|
|
return $this->db->put([$index, $type, '_mapping'], $body); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -347,8 +290,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getMapping($index = '_all', $type = '_all') |
|
|
|
public function getMapping($index = '_all', $type = '_all') |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->get($this->createUrl([$index, $type, '_mapping']))->send(); |
|
|
|
return $this->db->get([$index, $type, '_mapping']); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -356,8 +298,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function deleteMapping($index, $type) |
|
|
|
public function deleteMapping($index, $type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->delete($this->createUrl([$index, $type]))->send(); |
|
|
|
return $this->db->delete([$index, $type]); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -365,9 +306,7 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getFieldMapping($index, $type = '_all') |
|
|
|
public function getFieldMapping($index, $type = '_all') |
|
|
|
{ |
|
|
|
{ |
|
|
|
// TODO |
|
|
|
return $this->db->put([$index, $type, '_mapping']); |
|
|
|
$response = $this->db->http()->put($this->createUrl([$index, $type, '_mapping']))->send(); |
|
|
|
|
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -375,10 +314,8 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function analyze($options, $index = null) |
|
|
|
public function analyze($options, $index = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// TODO |
|
|
|
// TODO implement |
|
|
|
$response = $this->db->http()->put($this->createUrl([$index, $type, '_mapping']))->send(); |
|
|
|
// return $this->db->put([$index]); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -390,10 +327,10 @@ class Command extends Component |
|
|
|
'template' => $pattern, |
|
|
|
'template' => $pattern, |
|
|
|
'order' => $order, |
|
|
|
'order' => $order, |
|
|
|
'settings' => (object) $settings, |
|
|
|
'settings' => (object) $settings, |
|
|
|
'mappings' => (object) $settings, |
|
|
|
'mappings' => (object) $mappings, |
|
|
|
]); |
|
|
|
]); |
|
|
|
$response = $this->db->http()->put($this->createUrl(['_template', $name]), null, $body)->send(); |
|
|
|
return $this->db->put(['_template', $name], $body); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -401,8 +338,8 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function deleteTemplate($name) |
|
|
|
public function deleteTemplate($name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->delete($this->createUrl(['_template', $name]))->send(); |
|
|
|
return $this->db->delete(['_template', $name]); |
|
|
|
return $response->getStatusCode() == 200; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -410,21 +347,6 @@ class Command extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getTemplate($name) |
|
|
|
public function getTemplate($name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$response = $this->db->http()->get($this->createUrl(['_template', $name]))->send(); |
|
|
|
return $this->db->get(['_template', $name]); |
|
|
|
return Json::decode($response->getBody(true)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function createUrl($path, $options = []) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$url = implode('/', array_map(function($a) { |
|
|
|
|
|
|
|
return urlencode(is_array($a) ? implode(',', $a) : $a); |
|
|
|
|
|
|
|
}, $path)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!empty($options) || !empty($this->options)) { |
|
|
|
|
|
|
|
$options = array_merge($this->options, $options); |
|
|
|
|
|
|
|
$url .= '?' . http_build_query($options); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $url; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |