Search.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace Elastica\Multi;
  3. use Elastica\Client;
  4. use Elastica\JSON;
  5. use Elastica\Request;
  6. use Elastica\Search as BaseSearch;
  7. /**
  8. * Elastica multi search.
  9. *
  10. * @author munkie
  11. *
  12. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html
  13. */
  14. class Search
  15. {
  16. /**
  17. * @const string[] valid header options
  18. */
  19. private static $HEADER_OPTIONS = ['index', 'types', 'search_type',
  20. 'routing', 'preference', ];
  21. /**
  22. * @var MultiBuilderInterface
  23. */
  24. private $_builder;
  25. /**
  26. * @var \Elastica\Client
  27. */
  28. protected $_client;
  29. /**
  30. * @var array
  31. */
  32. protected $_options = [];
  33. /**
  34. * @var array|\Elastica\Search[]
  35. */
  36. protected $_searches = [];
  37. /**
  38. * Constructs search object.
  39. *
  40. * @param \Elastica\Client $client Client object
  41. * @param MultiBuilderInterface $builder
  42. */
  43. public function __construct(Client $client, MultiBuilderInterface $builder = null)
  44. {
  45. $this->_builder = $builder ?: new MultiBuilder();
  46. $this->_client = $client;
  47. }
  48. /**
  49. * @return \Elastica\Client
  50. */
  51. public function getClient()
  52. {
  53. return $this->_client;
  54. }
  55. /**
  56. * @return $this
  57. */
  58. public function clearSearches()
  59. {
  60. $this->_searches = [];
  61. return $this;
  62. }
  63. /**
  64. * @param \Elastica\Search $search
  65. * @param string $key Optional key
  66. *
  67. * @return $this
  68. */
  69. public function addSearch(BaseSearch $search, $key = null)
  70. {
  71. if ($key) {
  72. $this->_searches[$key] = $search;
  73. } else {
  74. $this->_searches[] = $search;
  75. }
  76. return $this;
  77. }
  78. /**
  79. * @param array|\Elastica\Search[] $searches
  80. *
  81. * @return $this
  82. */
  83. public function addSearches(array $searches)
  84. {
  85. foreach ($searches as $key => $search) {
  86. $this->addSearch($search, $key);
  87. }
  88. return $this;
  89. }
  90. /**
  91. * @param array|\Elastica\Search[] $searches
  92. *
  93. * @return $this
  94. */
  95. public function setSearches(array $searches)
  96. {
  97. $this->clearSearches();
  98. $this->addSearches($searches);
  99. return $this;
  100. }
  101. /**
  102. * @return array|\Elastica\Search[]
  103. */
  104. public function getSearches()
  105. {
  106. return $this->_searches;
  107. }
  108. /**
  109. * @param string $searchType
  110. *
  111. * @return $this
  112. */
  113. public function setSearchType($searchType)
  114. {
  115. $this->_options[BaseSearch::OPTION_SEARCH_TYPE] = $searchType;
  116. return $this;
  117. }
  118. /**
  119. * @return \Elastica\Multi\ResultSet
  120. */
  121. public function search()
  122. {
  123. $data = $this->_getData();
  124. $response = $this->getClient()->request(
  125. '_msearch',
  126. Request::POST,
  127. $data,
  128. $this->_options,
  129. Request::NDJSON_CONTENT_TYPE
  130. );
  131. return $this->_builder->buildMultiResultSet($response, $this->getSearches());
  132. }
  133. /**
  134. * @return string
  135. */
  136. protected function _getData()
  137. {
  138. $data = '';
  139. foreach ($this->getSearches() as $search) {
  140. $data .= $this->_getSearchData($search);
  141. }
  142. return $data;
  143. }
  144. /**
  145. * @param \Elastica\Search $search
  146. *
  147. * @return string
  148. */
  149. protected function _getSearchData(BaseSearch $search)
  150. {
  151. $header = $this->_getSearchDataHeader($search);
  152. $header = (empty($header)) ? new \stdClass() : $header;
  153. $query = $search->getQuery();
  154. // Keep other query options as part of the search body
  155. $queryOptions = array_diff_key($search->getOptions(), array_flip(self::$HEADER_OPTIONS));
  156. $data = JSON::stringify($header)."\n";
  157. $data .= JSON::stringify($query->toArray() + $queryOptions)."\n";
  158. return $data;
  159. }
  160. /**
  161. * @param \Elastica\Search $search
  162. *
  163. * @return array
  164. */
  165. protected function _getSearchDataHeader(BaseSearch $search)
  166. {
  167. $header = $search->getOptions();
  168. if ($search->hasIndices()) {
  169. $header['index'] = $search->getIndices();
  170. }
  171. if ($search->hasTypes()) {
  172. $header['types'] = $search->getTypes();
  173. }
  174. // Filter options accepted in the "header"
  175. return array_intersect_key($header, array_flip(self::$HEADER_OPTIONS));
  176. }
  177. }