|
| 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