PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Solarium/QueryType/Select/Query/Component/DistributedSearch.php

http://github.com/basdenooijer/solarium
PHP | 331 lines | 111 code | 33 blank | 187 comment | 3 complexity | 60447f035a0d1bf3b34afc87c663fd4d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright 2011 Bas de Nooijer. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this listof conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * The views and conclusions contained in the software and documentation are
  28. * those of the authors and should not be interpreted as representing official
  29. * policies, either expressed or implied, of the copyright holder.
  30. *
  31. * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
  32. * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
  33. * @link http://www.solarium-project.org/
  34. */
  35. /**
  36. * @namespace
  37. */
  38. namespace Solarium\QueryType\Select\Query\Component;
  39. use Solarium\QueryType\Select\Query\Query as SelectQuery;
  40. use Solarium\QueryType\Select\RequestBuilder\Component\DistributedSearch as RequestBuilder;
  41. /**
  42. * Distributed Search (sharding) component
  43. *
  44. * @link http://wiki.apache.org/solr/DistributedSearch
  45. * @link http://wiki.apache.org/solr/SolrCloud/
  46. */
  47. class DistributedSearch extends Component
  48. {
  49. /**
  50. * Request to be distributed across all shards in the list
  51. *
  52. * @var array
  53. */
  54. protected $shards = array();
  55. /**
  56. * Requests will be distributed across collections in this list
  57. *
  58. * @var array
  59. */
  60. protected $collections = array();
  61. /**
  62. * Get component type
  63. *
  64. * @return string
  65. */
  66. public function getType()
  67. {
  68. return SelectQuery::COMPONENT_DISTRIBUTEDSEARCH;
  69. }
  70. /**
  71. * Get a requestbuilder for this query
  72. *
  73. * @return RequestBuilder
  74. */
  75. public function getRequestBuilder()
  76. {
  77. return new RequestBuilder;
  78. }
  79. /**
  80. * This component has no response parser...
  81. *
  82. * @return null
  83. */
  84. public function getResponseParser()
  85. {
  86. return null;
  87. }
  88. /**
  89. * Initialize options
  90. *
  91. * Several options need some extra checks or setup work, for these options
  92. * the setters are called.
  93. *
  94. * @return void
  95. */
  96. protected function init()
  97. {
  98. foreach ($this->options as $name => $value) {
  99. switch ($name) {
  100. case 'shards':
  101. $this->setShards($value);
  102. break;
  103. case 'collections':
  104. $this->setCollections($value);
  105. break;
  106. }
  107. }
  108. }
  109. /**
  110. * Add a shard
  111. *
  112. * @param string $key unique string
  113. * @param string $shard The syntax is host:port/base_url
  114. * @return self Provides fluent interface
  115. * @link http://wiki.apache.org/solr/DistributedSearch
  116. */
  117. public function addShard($key, $shard)
  118. {
  119. $this->shards[$key] = $shard;
  120. return $this;
  121. }
  122. /**
  123. * Add multiple shards
  124. *
  125. * Example usage:
  126. * <code>
  127. * $client = new Solarium\Client;
  128. * $query = $client->createSelect();
  129. * $distributedSearch = $query->getDistributedSearch();
  130. * $distributedSearch->addShards(array(
  131. * 'core0' => 'localhost:8983/solr/core0',
  132. * 'core1' => 'localhost:8983/solr/core1'
  133. * ));
  134. * $result = $client->select($query);
  135. * </code>
  136. * @param array $shards
  137. * @return self Provides fluent interface
  138. */
  139. public function addShards(array $shards)
  140. {
  141. foreach ($shards as $key => $shard) {
  142. $this->addShard($key, $shard);
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Remove a shard
  148. *
  149. * @param string $key
  150. * @return self Provides fluent interface
  151. */
  152. public function removeShard($key)
  153. {
  154. if (isset($this->shards[$key])) {
  155. unset($this->shards[$key]);
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Remove all shards
  161. *
  162. * @return self Provides fluent interface
  163. */
  164. public function clearShards()
  165. {
  166. $this->shards = array();
  167. return $this;
  168. }
  169. /**
  170. * Set multiple shards
  171. *
  172. * This overwrites any existing shards
  173. *
  174. * Example usage:
  175. * <code>
  176. * $client = new Solarium\Client;
  177. * $query = $client->createSelect();
  178. * $distributedSearch = $query->getDistributedSearch();
  179. * $distributedSearch->setShards(array(
  180. * 'core0' => 'localhost:8983/solr/core0',
  181. * 'core1' => 'localhost:8983/solr/core1'
  182. * ));
  183. * $result = $client->select($query);
  184. * </code>
  185. *
  186. * @param array $shards Associative array of shards
  187. * @return self Provides fluent interface
  188. */
  189. public function setShards(array $shards)
  190. {
  191. $this->clearShards();
  192. $this->addShards($shards);
  193. return $this;
  194. }
  195. /**
  196. * Get a list of the shards
  197. *
  198. * @return array
  199. */
  200. public function getShards()
  201. {
  202. return $this->shards;
  203. }
  204. /**
  205. * A sharded request will go to the standard request handler
  206. * (not necessarily the original); this can be overridden via shards.qt
  207. *
  208. * @param string
  209. * @return self Provides fluent interface
  210. */
  211. public function setShardRequestHandler($handler)
  212. {
  213. $this->setOption('shardhandler', $handler);
  214. return $this;
  215. }
  216. /**
  217. * Get a shard request handler (shards.qt)
  218. *
  219. * @param string
  220. * @return self Provides fluent interface
  221. */
  222. public function getShardRequestHandler()
  223. {
  224. return $this->getOption('shardhandler');
  225. }
  226. /**
  227. * Add a collection
  228. *
  229. * @param string $key unique string
  230. * @param string $collection The syntax is host:port/base_url
  231. * @return self Provides fluent interface
  232. * @link http://wiki.apache.org/solr/SolrCloud/
  233. */
  234. public function addCollection($key, $collection)
  235. {
  236. $this->collections[$key] = $collection;
  237. return $this;
  238. }
  239. /**
  240. * Add multiple collections
  241. *
  242. * @param array $collections
  243. * @return self Provides fluent interface
  244. */
  245. public function addCollections(array $collections)
  246. {
  247. foreach ($collections as $key => $collection) {
  248. $this->addCollection($key, $collection);
  249. }
  250. return $this;
  251. }
  252. /**
  253. * Remove a collection
  254. *
  255. * @param string $key
  256. * @return self Provides fluent interface
  257. */
  258. public function removeCollection($key)
  259. {
  260. if (isset($this->collections[$key])) {
  261. unset($this->collections[$key]);
  262. }
  263. return $this;
  264. }
  265. /**
  266. * Remove all collections
  267. *
  268. * @return self Provides fluent interface
  269. */
  270. public function clearCollections()
  271. {
  272. $this->collections = array();
  273. return $this;
  274. }
  275. /**
  276. * Set multiple collections
  277. *
  278. * This overwrites any existing collections
  279. *
  280. * @param array $collections Associative array of collections
  281. * @return self Provides fluent interface
  282. */
  283. public function setCollections(array $collections)
  284. {
  285. $this->clearCollections();
  286. $this->addCollections($collections);
  287. return $this;
  288. }
  289. /**
  290. * Get a list of the collections
  291. *
  292. * @return array
  293. */
  294. public function getCollections()
  295. {
  296. return $this->collections;
  297. }
  298. }