123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- namespace Mall\Framework\SearchClient;
- class Client
- {
- const DEFAULT_PROTOCOL = 'http';
- const DEFAULT_SERVER = '127.0.0.1:9200';
- const DEFAULT_INDEX = null;
- const DEFAULT_TYPE = null;
- protected $_config = array();
- protected static $_defaults = array(
- 'protocol' => Client::DEFAULT_PROTOCOL,
- 'servers' => Client::DEFAULT_SERVER,
- 'index' => Client::DEFAULT_INDEX,
- 'type' => Client::DEFAULT_TYPE,
- 'timeout' => null,
- );
- protected static $_protocols = array(
- 'http' => 'Mall\\Framework\\SearchClient\\Transport\\HTTP'
- );
-
- private $transport;
-
- private $bulk;
-
- private $index;
-
- private $type;
-
- public function __construct($transport, $index = null, $type = null)
- {
- $this->transport = $transport;
- $this->setIndex($index);
- $this->setType($type);
- }
-
- public static function connection($config = array())
- {
- if (!$config && ($url = getenv('ELASTICSEARCH_URL'))) {
- $config = $url;
- }
- if (is_string($config)) {
- $config = self::parseDsn($config);
- }
- $config += self::$_defaults;
- $protocol = $config['protocol'];
- if (!isset(self::$_protocols[$protocol])) {
- throw new \Exception("Tried to use unknown protocol: $protocol");
- }
- $class = self::$_protocols[$protocol];
- if (null !== $config['timeout'] && !is_numeric($config['timeout'])) {
- throw new \Exception("HTTP timeout should have a numeric value when specified.");
- }
- $server = is_array($config['servers']) ? $config['servers'][0] : $config['servers'];
- list($host, $port) = explode(':', $server);
- $transport = new $class($host, $port, $config['timeout']);
- $client = new self($transport, $config['index'], $config['type']);
- $client->config($config);
- return $client;
- }
-
- public function config($config = null)
- {
- if (!$config)
- return $this->_config;
- if (is_array($config))
- $this->_config = $config + $this->_config;
- }
-
- public function setIndex($index)
- {
- if (is_array($index)) {
- $index = implode(",", array_filter($index));
- }
- $this->index = $index;
- $this->transport->setIndex($index);
- return $this;
- }
-
- public function getIndex()
- {
- return $this->index;
- }
-
- public function setType($type)
- {
- if (is_array($type))
- $type = implode(",", array_filter($type));
- $this->type = $type;
- $this->transport->setType($type);
- return $this;
- }
-
- public function getType()
- {
- return $this->type;
- }
-
- public function get($id, $verbose = false)
- {
- return $this->request($id, "GET", false, $verbose);
- }
-
- public function map($mapping, array $config = array())
- {
- if (is_array($mapping)) $mapping = new Mapping($mapping);
- $mapping->config($config);
- try {
- $type = $mapping->config('type');
- } catch (\Exception $e) {
- }
- if (isset($type) && !$this->passesTypeConstraint($type)) {
- throw new Exception("Cant create mapping due to type constraint mismatch");
- }
- return $this->request('_mapping', 'PUT', $mapping->export(), true);
- }
- protected function passesTypeConstraint($constraint)
- {
- if (is_string($constraint)) $constraint = array($constraint);
- $currentType = explode(',', $this->type);
- $includeTypes = array_intersect($constraint, $currentType);
- return ($constraint && count($includeTypes) === count($constraint));
- }
-
- public function updateFieldVaule($data, $id)
- {
- $updateData['doc'] = $data;
- $searchUpdateData = json_encode($updateData);
- return $this->request($id.'/_update', 'POST', $searchUpdateData);
- }
-
- public function batchUpdateFieldVaule($data, $query)
- {
- $start = microtime(true);
- $result = $this->transport->batchUpdateFieldVaule($data, $query);
- $result['time'] = microtime(true) - $start;
- return $result;
- }
-
- public function request($path, $method = 'GET', $payload = false, $verbose = false, $buildPath = true)
- {
- $path = $buildPath ? $this->expandPath($path) : $path;
- $response = $this->transport->request($path, $method, $payload, $buildPath);
- return ($verbose || !isset($response['_source']))
- ? $response
- : $response['_source'];
- }
-
- public function index($document, $id = false, array $options = array())
- {
- if ($this->bulk) {
- return $this->bulk->index($document, $id, $this->index, $this->type, $options);
- }
- return $this->transport->index($document, $id, $options);
- }
-
- public function search($query, array $options = array())
- {
- $start = microtime(true);
- $result = $this->transport->search($query, $options);
- $result['time'] = microtime(true) - $start;
- return $result;
- }
-
- public function scrollSearch($query, $scroll = '10s', $size = 2000, array $options = array())
- {
- $start = microtime(true);
- $result = $this->transport->scrollSearch($query, $scroll, $size, $options);
- $result['time'] = microtime(true) - $start;
- return $result;
- }
-
- public function delete($id = false, array $options = array())
- {
- if ($this->bulk) {
- return $this->bulk->delete($id, $this->index, $this->type, $options);
- }
- return $this->transport->delete($id, $options);
- }
-
- public function deleteByQuery($query, array $options = array())
- {
- return $this->transport->deleteByQuery($query, $options);
- }
-
- public function refresh()
- {
- return $this->transport->request(array('_refresh'), 'GET');
- }
-
- protected function expandPath($path)
- {
- $path = (array)$path;
- $isAbsolute = $path[0][0] === '/';
- return $isAbsolute
- ? $path
- : array_merge((array)$this->type, $path);
- }
-
- protected static function parseDsn($dsn)
- {
- $parts = parse_url($dsn);
- $protocol = $parts['scheme'];
- $servers = $parts['host'] . ':' . $parts['port'];
- if (isset($parts['path'])) {
- $path = explode('/', $parts['path']);
- list($index, $type) = array_values(array_filter($path));
- }
- return compact('protocol', 'servers', 'index', 'type');
- }
-
- public function createBulk()
- {
- return new Bulk($this);
- }
-
- public function createBase($index, $mappings)
- {
- return $this->transport->createBase($index, $mappings);
- }
-
- public function deleteBase($index)
- {
- return $this->transport->deleteBase($index);
- }
-
- public function beginBulk()
- {
- if (!$this->bulk) {
- $this->bulk = $this->createBulk();
- }
- return $this->bulk;
- }
-
- public function begin()
- {
- return $this->beginBulk();
- }
-
- public function commitBulk()
- {
- if ($this->bulk) {
- $result = $this->bulk->commit();
- $this->bulk = null;
- return $result;
- }
- }
-
- public function commit()
- {
- return $this->commitBulk();
- }
- }
|