Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add MsearchTemplate endpoint
Closes #599
  • Loading branch information
polyfractal committed Jul 19, 2017
1 parent d857ae1 commit df7004f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Elasticsearch/Client.php
Expand Up @@ -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)
Expand Down
105 changes: 105 additions & 0 deletions src/Elasticsearch/Endpoints/MsearchTemplate.php
@@ -0,0 +1,105 @@
<?php

namespace Elasticsearch\Endpoints;

use Elasticsearch\Common\Exceptions;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\Transport;

/**
* Class MsearchTemplate
*
* @category Elasticsearch
* @package Elasticsearch\Endpoints
* @author Zachary Tong <zach@elastic.co>
* @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';
}
}

0 comments on commit df7004f

Please sign in to comment.