Skip to content

Commit df7004f

Browse files
committed
Add MsearchTemplate endpoint
Closes #599
1 parent d857ae1 commit df7004f

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

src/Elasticsearch/Client.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,36 @@ public function msearch($params = array())
646646
return $this->performRequest($endpoint);
647647
}
648648

649+
/**
650+
* $params['index'] = (list) A comma-separated list of index names to use as default
651+
* ['type'] = (list) A comma-separated list of document types to use as default
652+
* ['search_type'] = (enum) Search operation type
653+
* ['body'] = (array|string) The request definitions (metadata-search request definition pairs), separated by newlines
654+
* ['max_concurrent_searches'] = (number) Controls the maximum number of concurrent searches the multi search api will execute
655+
*
656+
* @param $params array Associative array of parameters
657+
*
658+
* @return array
659+
*/
660+
public function msearchTemplate($params = array())
661+
{
662+
$index = $this->extractArgument($params, 'index');
663+
$type = $this->extractArgument($params, 'type');
664+
$body = $this->extractArgument($params, 'body');
665+
666+
/** @var callback $endpointBuilder */
667+
$endpointBuilder = $this->endpoints;
668+
669+
/** @var \Elasticsearch\Endpoints\MsearchTemplate $endpoint */
670+
$endpoint = $endpointBuilder('MsearchTemplate');
671+
$endpoint->setIndex($index)
672+
->setType($type)
673+
->setBody($body);
674+
$endpoint->setParams($params);
675+
676+
return $this->performRequest($endpoint);
677+
}
678+
649679
/**
650680
* $params['index'] = (string) The name of the index (Required)
651681
* ['type'] = (string) The type of the document (Required)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Elasticsearch\Endpoints;
4+
5+
use Elasticsearch\Common\Exceptions;
6+
use Elasticsearch\Serializers\SerializerInterface;
7+
use Elasticsearch\Transport;
8+
9+
/**
10+
* Class MsearchTemplate
11+
*
12+
* @category Elasticsearch
13+
* @package Elasticsearch\Endpoints
14+
* @author Zachary Tong <[email protected]>
15+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
16+
* @link http://elastic.co
17+
*/
18+
class MsearchTemplate extends AbstractEndpoint
19+
{
20+
/**
21+
* @param SerializerInterface $serializer
22+
*/
23+
public function __construct(SerializerInterface $serializer)
24+
{
25+
$this->serializer = $serializer;
26+
}
27+
28+
/**
29+
* @param array|string $body
30+
*
31+
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
32+
* @return $this
33+
*/
34+
public function setBody($body)
35+
{
36+
if (isset($body) !== true) {
37+
return $this;
38+
}
39+
40+
if (is_array($body) === true) {
41+
$bulkBody = "";
42+
foreach ($body as $item) {
43+
$bulkBody .= $this->serializer->serialize($item)."\n";
44+
}
45+
$body = $bulkBody;
46+
}
47+
48+
$this->body = $body;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getURI()
57+
{
58+
$index = $this->index;
59+
$type = $this->type;
60+
$uri = "/_msearch/template";
61+
62+
if (isset($index) === true && isset($type) === true) {
63+
$uri = "/$index/$type/_msearch/template";
64+
} elseif (isset($index) === true) {
65+
$uri = "/$index/_msearch/template";
66+
} elseif (isset($type) === true) {
67+
$uri = "/_all/$type/_msearch/template";
68+
}
69+
70+
return $uri;
71+
}
72+
73+
/**
74+
* @return string[]
75+
*/
76+
public function getParamWhitelist()
77+
{
78+
return array(
79+
'search_type',
80+
'typed_keys',
81+
'max_concurrent_searches'
82+
);
83+
}
84+
85+
/**
86+
* @return array
87+
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
88+
*/
89+
public function getBody()
90+
{
91+
if (isset($this->body) !== true) {
92+
throw new Exceptions\RuntimeException('Body is required for MSearch');
93+
}
94+
95+
return $this->body;
96+
}
97+
98+
/**
99+
* @return string
100+
*/
101+
public function getMethod()
102+
{
103+
return 'GET';
104+
}
105+
}

0 commit comments

Comments
 (0)