From df7004f08800fc8be11053355e98b619358c37fd Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Wed, 19 Jul 2017 14:42:25 -0400 Subject: [PATCH] Add MsearchTemplate endpoint Closes #599 --- src/Elasticsearch/Client.php | 30 +++++ .../Endpoints/MsearchTemplate.php | 105 ++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/Elasticsearch/Endpoints/MsearchTemplate.php diff --git a/src/Elasticsearch/Client.php b/src/Elasticsearch/Client.php index 3d28b2456..cf1ac43a8 100644 --- a/src/Elasticsearch/Client.php +++ b/src/Elasticsearch/Client.php @@ -646,6 +646,36 @@ public function msearch($params = array()) return $this->performRequest($endpoint); } + /** + * $params['index'] = (list) A comma-separated list of index names to use as default + * ['type'] = (list) A comma-separated list of document types to use as default + * ['search_type'] = (enum) Search operation type + * ['body'] = (array|string) The request definitions (metadata-search request definition pairs), separated by newlines + * ['max_concurrent_searches'] = (number) Controls the maximum number of concurrent searches the multi search api will execute + * + * @param $params array Associative array of parameters + * + * @return array + */ + public function msearchTemplate($params = array()) + { + $index = $this->extractArgument($params, 'index'); + $type = $this->extractArgument($params, 'type'); + $body = $this->extractArgument($params, 'body'); + + /** @var callback $endpointBuilder */ + $endpointBuilder = $this->endpoints; + + /** @var \Elasticsearch\Endpoints\MsearchTemplate $endpoint */ + $endpoint = $endpointBuilder('MsearchTemplate'); + $endpoint->setIndex($index) + ->setType($type) + ->setBody($body); + $endpoint->setParams($params); + + return $this->performRequest($endpoint); + } + /** * $params['index'] = (string) The name of the index (Required) * ['type'] = (string) The type of the document (Required) diff --git a/src/Elasticsearch/Endpoints/MsearchTemplate.php b/src/Elasticsearch/Endpoints/MsearchTemplate.php new file mode 100644 index 000000000..43171741b --- /dev/null +++ b/src/Elasticsearch/Endpoints/MsearchTemplate.php @@ -0,0 +1,105 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 + * @link http://elastic.co + */ +class MsearchTemplate extends AbstractEndpoint +{ + /** + * @param SerializerInterface $serializer + */ + public function __construct(SerializerInterface $serializer) + { + $this->serializer = $serializer; + } + + /** + * @param array|string $body + * + * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException + * @return $this + */ + public function setBody($body) + { + if (isset($body) !== true) { + return $this; + } + + if (is_array($body) === true) { + $bulkBody = ""; + foreach ($body as $item) { + $bulkBody .= $this->serializer->serialize($item)."\n"; + } + $body = $bulkBody; + } + + $this->body = $body; + + return $this; + } + + /** + * @return string + */ + public function getURI() + { + $index = $this->index; + $type = $this->type; + $uri = "/_msearch/template"; + + if (isset($index) === true && isset($type) === true) { + $uri = "/$index/$type/_msearch/template"; + } elseif (isset($index) === true) { + $uri = "/$index/_msearch/template"; + } elseif (isset($type) === true) { + $uri = "/_all/$type/_msearch/template"; + } + + return $uri; + } + + /** + * @return string[] + */ + public function getParamWhitelist() + { + return array( + 'search_type', + 'typed_keys', + 'max_concurrent_searches' + ); + } + + /** + * @return array + * @throws \Elasticsearch\Common\Exceptions\RuntimeException + */ + public function getBody() + { + if (isset($this->body) !== true) { + throw new Exceptions\RuntimeException('Body is required for MSearch'); + } + + return $this->body; + } + + /** + * @return string + */ + public function getMethod() + { + return 'GET'; + } +}