MsearchTemplate.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Elasticsearch\Endpoints;
  3. use Elasticsearch\Common\Exceptions;
  4. use Elasticsearch\Serializers\SerializerInterface;
  5. use Elasticsearch\Transport;
  6. /**
  7. * Class MsearchTemplate
  8. *
  9. * @category Elasticsearch
  10. * @package Elasticsearch\Endpoints
  11. * @author Zachary Tong <zach@elastic.co>
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  13. * @link http://elastic.co
  14. */
  15. class MsearchTemplate extends AbstractEndpoint
  16. {
  17. /**
  18. * @param SerializerInterface $serializer
  19. */
  20. public function __construct(SerializerInterface $serializer)
  21. {
  22. $this->serializer = $serializer;
  23. }
  24. /**
  25. * @param array|string $body
  26. *
  27. * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
  28. * @return $this
  29. */
  30. public function setBody($body)
  31. {
  32. if (isset($body) !== true) {
  33. return $this;
  34. }
  35. if (is_array($body) === true) {
  36. $bulkBody = "";
  37. foreach ($body as $item) {
  38. $bulkBody .= $this->serializer->serialize($item)."\n";
  39. }
  40. $body = $bulkBody;
  41. }
  42. $this->body = $body;
  43. return $this;
  44. }
  45. /**
  46. * @return string
  47. */
  48. public function getURI()
  49. {
  50. $index = $this->index;
  51. $type = $this->type;
  52. $uri = "/_msearch/template";
  53. if (isset($index) === true && isset($type) === true) {
  54. $uri = "/$index/$type/_msearch/template";
  55. } elseif (isset($index) === true) {
  56. $uri = "/$index/_msearch/template";
  57. } elseif (isset($type) === true) {
  58. $uri = "/_all/$type/_msearch/template";
  59. }
  60. return $uri;
  61. }
  62. /**
  63. * @return string[]
  64. */
  65. public function getParamWhitelist()
  66. {
  67. return array(
  68. 'search_type',
  69. 'typed_keys',
  70. 'max_concurrent_searches'
  71. );
  72. }
  73. /**
  74. * @return array
  75. * @throws \Elasticsearch\Common\Exceptions\RuntimeException
  76. */
  77. public function getBody()
  78. {
  79. if (isset($this->body) !== true) {
  80. throw new Exceptions\RuntimeException('Body is required for MSearch');
  81. }
  82. return $this->body;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getMethod()
  88. {
  89. return 'GET';
  90. }
  91. }