Client.Class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php // vim:set ts=4 sw=4 et:
  2. namespace Mall\Framework\SearchClient;
  3. /**
  4. * This file is part of the ElasticSearch PHP client
  5. *
  6. * (c) Raymond Julin <raymond.julin@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. class Client
  12. {
  13. const DEFAULT_PROTOCOL = 'http';
  14. const DEFAULT_SERVER = '127.0.0.1:9200';
  15. const DEFAULT_INDEX = null;
  16. const DEFAULT_TYPE = null;
  17. protected $_config = array();
  18. protected static $_defaults = array(
  19. 'protocol' => Client::DEFAULT_PROTOCOL,
  20. 'servers' => Client::DEFAULT_SERVER,
  21. 'index' => Client::DEFAULT_INDEX,
  22. 'type' => Client::DEFAULT_TYPE,
  23. 'timeout' => null,
  24. );
  25. protected static $_protocols = array(
  26. 'http' => 'Mall\\Framework\\SearchClient\\Transport\\HTTP'
  27. );
  28. /**
  29. * @var Transport\Base
  30. */
  31. private $transport;
  32. /**
  33. * @var Bulk
  34. */
  35. private $bulk;
  36. /**
  37. * @var string
  38. */
  39. private $index;
  40. /**
  41. * @var string
  42. */
  43. private $type;
  44. /**
  45. * Construct search client
  46. *
  47. * @return \Mall\Framework\SearchClient\Client
  48. * @param \Mall\Framework\SearchClient\Transport\Base $transport
  49. * @param string $index
  50. * @param string $type
  51. */
  52. public function __construct($transport, $index = null, $type = null)
  53. {
  54. $this->transport = $transport;
  55. $this->setIndex($index);
  56. $this->setType($type);
  57. }
  58. /**
  59. * Get a client instance
  60. * Defaults to opening a http transport connection to 127.0.0.1:9200
  61. *
  62. * @param string|array $config Allow overriding only the configuration bits you desire
  63. * - _transport_
  64. * - _host_
  65. * - _port_
  66. * - _index_
  67. * - _type_
  68. * @throws \Exception
  69. * @return \Mall\Framework\SearchClient\Client
  70. */
  71. public static function connection($config = array())
  72. {
  73. if (!$config && ($url = getenv('ELASTICSEARCH_URL'))) {
  74. $config = $url;
  75. }
  76. if (is_string($config)) {
  77. $config = self::parseDsn($config);
  78. }
  79. $config += self::$_defaults;
  80. $protocol = $config['protocol'];
  81. if (!isset(self::$_protocols[$protocol])) {
  82. throw new \Exception("Tried to use unknown protocol: $protocol");
  83. }
  84. $class = self::$_protocols[$protocol];
  85. if (null !== $config['timeout'] && !is_numeric($config['timeout'])) {
  86. throw new \Exception("HTTP timeout should have a numeric value when specified.");
  87. }
  88. $server = is_array($config['servers']) ? $config['servers'][0] : $config['servers'];
  89. list($host, $port) = explode(':', $server);
  90. $transport = new $class($host, $port, $config['timeout']);
  91. $client = new self($transport, $config['index'], $config['type']);
  92. $client->config($config);
  93. return $client;
  94. }
  95. /**
  96. * @param array|null $config
  97. * @return array|void
  98. */
  99. public function config($config = null)
  100. {
  101. if (!$config)
  102. return $this->_config;
  103. if (is_array($config))
  104. $this->_config = $config + $this->_config;
  105. }
  106. /**
  107. * Change what index to go against
  108. * @return \Mall\Framework\SearchClient\Client
  109. * @param mixed $index
  110. */
  111. public function setIndex($index)
  112. {
  113. if (is_array($index)) {
  114. $index = implode(",", array_filter($index));
  115. }
  116. $this->index = $index;
  117. $this->transport->setIndex($index);
  118. return $this;
  119. }
  120. /**
  121. * Get current index
  122. *
  123. * @return string
  124. */
  125. public function getIndex()
  126. {
  127. return $this->index;
  128. }
  129. /**
  130. * Change what types to act against
  131. * @return \Mall\Framework\SearchClient\Client
  132. * @param mixed $type
  133. */
  134. public function setType($type)
  135. {
  136. if (is_array($type))
  137. $type = implode(",", array_filter($type));
  138. $this->type = $type;
  139. $this->transport->setType($type);
  140. return $this;
  141. }
  142. /**
  143. * Get current type
  144. *
  145. * @return string
  146. */
  147. public function getType()
  148. {
  149. return $this->type;
  150. }
  151. /**
  152. * Fetch a document by its id
  153. *
  154. * @return array
  155. * @param mixed $id Optional
  156. * @param bool $verbose
  157. */
  158. public function get($id, $verbose = false)
  159. {
  160. return $this->request($id, "GET", false, $verbose);
  161. }
  162. /**
  163. * Puts a mapping on index
  164. *
  165. * @param array|object $mapping
  166. * @param array $config
  167. * @throws Exception
  168. * @return array
  169. */
  170. public function map($mapping, array $config = array())
  171. {
  172. if (is_array($mapping)) $mapping = new Mapping($mapping);
  173. $mapping->config($config);
  174. try {
  175. $type = $mapping->config('type');
  176. } catch (\Exception $e) {
  177. } // No type is cool
  178. if (isset($type) && !$this->passesTypeConstraint($type)) {
  179. throw new Exception("Cant create mapping due to type constraint mismatch");
  180. }
  181. return $this->request('_mapping', 'PUT', $mapping->export(), true);
  182. }
  183. protected function passesTypeConstraint($constraint)
  184. {
  185. if (is_string($constraint)) $constraint = array($constraint);
  186. $currentType = explode(',', $this->type);
  187. $includeTypes = array_intersect($constraint, $currentType);
  188. return ($constraint && count($includeTypes) === count($constraint));
  189. }
  190. /**
  191. * 更新文档中的字段值
  192. * @param array $data 要更新的字段内容
  193. * @param int $id 要更新的文档id
  194. * @return array
  195. */
  196. public function updateFieldVaule($data, $id)
  197. {
  198. $updateData['doc'] = $data;
  199. $searchUpdateData = json_encode($updateData);
  200. return $this->request($id.'/_update', 'POST', $searchUpdateData);
  201. }
  202. /**
  203. * 批量更新文档中的字段值
  204. * @param array $data 要更新的字段内容
  205. * @param int $id 要更新的文档id
  206. * @return array
  207. */
  208. public function batchUpdateFieldVaule($data, $query)
  209. {
  210. $start = microtime(true);
  211. $result = $this->transport->batchUpdateFieldVaule($data, $query);
  212. $result['time'] = microtime(true) - $start;
  213. return $result;
  214. }
  215. /**
  216. * Perform a raw request
  217. *
  218. * Usage example
  219. *
  220. * $response = $client->request('_status', 'GET');
  221. *
  222. * @return array
  223. * @param mixed $path Request path to use.
  224. * `type` is prepended to this path inside request
  225. * @param string $method HTTP verb to use
  226. * @param mixed $payload Array of data to be json-encoded
  227. * @param bool $verbose Controls response data, if `false`
  228. * only `_source` of response is returned
  229. */
  230. public function request($path, $method = 'GET', $payload = false, $verbose = false, $buildPath = true)
  231. {
  232. $path = $buildPath ? $this->expandPath($path) : $path;
  233. $response = $this->transport->request($path, $method, $payload, $buildPath);
  234. return ($verbose || !isset($response['_source']))
  235. ? $response
  236. : $response['_source'];
  237. }
  238. /**
  239. * Index a new document or update it if existing
  240. *
  241. * @return array
  242. * @param array $document
  243. * @param mixed $id Optional
  244. * @param array $options Allow sending query parameters to control indexing further
  245. * _refresh_ *bool* If set to true, immediately refresh the shard after indexing
  246. */
  247. public function index($document, $id = false, array $options = array())
  248. {
  249. if ($this->bulk) {
  250. return $this->bulk->index($document, $id, $this->index, $this->type, $options);
  251. }
  252. return $this->transport->index($document, $id, $options);
  253. }
  254. /**
  255. * Perform search, this is the sweet spot
  256. *
  257. * @return array
  258. * @param $query
  259. * @param array $options
  260. */
  261. public function search($query, array $options = array())
  262. {
  263. $start = microtime(true);
  264. $result = $this->transport->search($query, $options);
  265. $result['time'] = microtime(true) - $start;
  266. return $result;
  267. }
  268. /**
  269. * Perform scrollSearch
  270. * @param $query
  271. * @param string $scroll
  272. * @param int $size
  273. * @param array $options
  274. * @return mixed
  275. */
  276. public function scrollSearch($query, $scroll = '10s', $size = 2000, array $options = array())
  277. {
  278. $start = microtime(true);
  279. $result = $this->transport->scrollSearch($query, $scroll, $size, $options);
  280. $result['time'] = microtime(true) - $start;
  281. return $result;
  282. }
  283. /**
  284. * Flush this index/type combination
  285. *
  286. * @return array
  287. * @param mixed $id If id is supplied, delete that id for this index
  288. * if not wipe the entire index
  289. * @param array $options Parameters to pass to delete action
  290. */
  291. public function delete($id = false, array $options = array())
  292. {
  293. if ($this->bulk) {
  294. return $this->bulk->delete($id, $this->index, $this->type, $options);
  295. }
  296. return $this->transport->delete($id, $options);
  297. }
  298. /**
  299. * Flush this index/type combination
  300. *
  301. * @return array
  302. * @param mixed $query Text or array based query to delete everything that matches
  303. * @param array $options Parameters to pass to delete action
  304. */
  305. public function deleteByQuery($query, array $options = array())
  306. {
  307. return $this->transport->deleteByQuery($query, $options);
  308. }
  309. /**
  310. * Perform refresh of current indexes
  311. *
  312. * @return array
  313. */
  314. public function refresh()
  315. {
  316. return $this->transport->request(array('_refresh'), 'GET');
  317. }
  318. /**
  319. * Expand a given path (array or string)
  320. * If this is not an absolute path index + type will be prepended
  321. * If it is an absolute path it will be used as is
  322. *
  323. * @param mixed $path
  324. * @return array
  325. */
  326. protected function expandPath($path)
  327. {
  328. $path = (array)$path;
  329. $isAbsolute = $path[0][0] === '/';
  330. return $isAbsolute
  331. ? $path
  332. : array_merge((array)$this->type, $path);
  333. }
  334. /**
  335. * Parse a DSN string into an associative array
  336. *
  337. * @param string $dsn
  338. * @return array
  339. */
  340. protected static function parseDsn($dsn)
  341. {
  342. $parts = parse_url($dsn);
  343. $protocol = $parts['scheme'];
  344. $servers = $parts['host'] . ':' . $parts['port'];
  345. if (isset($parts['path'])) {
  346. $path = explode('/', $parts['path']);
  347. list($index, $type) = array_values(array_filter($path));
  348. }
  349. return compact('protocol', 'servers', 'index', 'type');
  350. }
  351. /**
  352. * Create a bulk-transaction
  353. *
  354. * @return Bulk
  355. */
  356. public function createBulk()
  357. {
  358. return new Bulk($this);
  359. }
  360. /**
  361. * 增加Index
  362. *
  363. * @param $index
  364. * @param $mappings
  365. * @return mixed
  366. */
  367. public function createBase($index, $mappings)
  368. {
  369. return $this->transport->createBase($index, $mappings);
  370. }
  371. /**
  372. * 删除Index
  373. *
  374. * @param $index
  375. * @return mixed
  376. */
  377. public function deleteBase($index)
  378. {
  379. return $this->transport->deleteBase($index);
  380. }
  381. /**
  382. * Begin a transparent bulk-transaction
  383. * if one is already running, return its handle
  384. * @return Bulk
  385. */
  386. public function beginBulk()
  387. {
  388. if (!$this->bulk) {
  389. $this->bulk = $this->createBulk();
  390. }
  391. return $this->bulk;
  392. }
  393. /**
  394. * @see beginBulk
  395. */
  396. public function begin()
  397. {
  398. return $this->beginBulk();
  399. }
  400. /**
  401. * commit a bulk-transaction
  402. * @return array
  403. */
  404. public function commitBulk()
  405. {
  406. if ($this->bulk) {
  407. $result = $this->bulk->commit();
  408. $this->bulk = null;
  409. return $result;
  410. }
  411. }
  412. /**
  413. * @see commitBulk
  414. */
  415. public function commit()
  416. {
  417. return $this->commitBulk();
  418. }
  419. }