123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php // vim:set ts=4 sw=4 et:
- namespace Mall\Framework\SearchClient;
- /**
- * This file is part of the ElasticSearch PHP client
- *
- * (c) Raymond Julin <raymond.julin@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- 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'
- );
- /**
- * @var Transport\Base
- */
- private $transport;
- /**
- * @var Bulk
- */
- private $bulk;
- /**
- * @var string
- */
- private $index;
- /**
- * @var string
- */
- private $type;
- /**
- * Construct search client
- *
- * @return \Mall\Framework\SearchClient\Client
- * @param \Mall\Framework\SearchClient\Transport\Base $transport
- * @param string $index
- * @param string $type
- */
- public function __construct($transport, $index = null, $type = null)
- {
- $this->transport = $transport;
- $this->setIndex($index);
- $this->setType($type);
- }
- /**
- * Get a client instance
- * Defaults to opening a http transport connection to 127.0.0.1:9200
- *
- * @param string|array $config Allow overriding only the configuration bits you desire
- * - _transport_
- * - _host_
- * - _port_
- * - _index_
- * - _type_
- * @throws \Exception
- * @return \Mall\Framework\SearchClient\Client
- */
- 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;
- }
- /**
- * @param array|null $config
- * @return array|void
- */
- public function config($config = null)
- {
- if (!$config)
- return $this->_config;
- if (is_array($config))
- $this->_config = $config + $this->_config;
- }
- /**
- * Change what index to go against
- * @return \Mall\Framework\SearchClient\Client
- * @param mixed $index
- */
- public function setIndex($index)
- {
- if (is_array($index)) {
- $index = implode(",", array_filter($index));
- }
- $this->index = $index;
- $this->transport->setIndex($index);
- return $this;
- }
- /**
- * Get current index
- *
- * @return string
- */
- public function getIndex()
- {
- return $this->index;
- }
- /**
- * Change what types to act against
- * @return \Mall\Framework\SearchClient\Client
- * @param mixed $type
- */
- public function setType($type)
- {
- if (is_array($type))
- $type = implode(",", array_filter($type));
- $this->type = $type;
- $this->transport->setType($type);
- return $this;
- }
- /**
- * Get current type
- *
- * @return string
- */
- public function getType()
- {
- return $this->type;
- }
- /**
- * Fetch a document by its id
- *
- * @return array
- * @param mixed $id Optional
- * @param bool $verbose
- */
- public function get($id, $verbose = false)
- {
- return $this->request($id, "GET", false, $verbose);
- }
- /**
- * Puts a mapping on index
- *
- * @param array|object $mapping
- * @param array $config
- * @throws Exception
- * @return array
- */
- 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) {
- } // No type is cool
- 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));
- }
- /**
- * 更新文档中的字段值
- * @param array $data 要更新的字段内容
- * @param int $id 要更新的文档id
- * @return array
- */
- public function updateFieldVaule($data, $id)
- {
- $updateData['doc'] = $data;
- $searchUpdateData = json_encode($updateData);
- return $this->request($id.'/_update', 'POST', $searchUpdateData);
- }
- /**
- * 批量更新文档中的字段值
- * @param array $data 要更新的字段内容
- * @param int $id 要更新的文档id
- * @return array
- */
- public function batchUpdateFieldVaule($data, $query)
- {
- $start = microtime(true);
- $result = $this->transport->batchUpdateFieldVaule($data, $query);
- $result['time'] = microtime(true) - $start;
- return $result;
- }
- /**
- * Perform a raw request
- *
- * Usage example
- *
- * $response = $client->request('_status', 'GET');
- *
- * @return array
- * @param mixed $path Request path to use.
- * `type` is prepended to this path inside request
- * @param string $method HTTP verb to use
- * @param mixed $payload Array of data to be json-encoded
- * @param bool $verbose Controls response data, if `false`
- * only `_source` of response is returned
- */
- 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'];
- }
- /**
- * Index a new document or update it if existing
- *
- * @return array
- * @param array $document
- * @param mixed $id Optional
- * @param array $options Allow sending query parameters to control indexing further
- * _refresh_ *bool* If set to true, immediately refresh the shard after indexing
- */
- 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);
- }
- /**
- * Perform search, this is the sweet spot
- *
- * @return array
- * @param $query
- * @param array $options
- */
- public function search($query, array $options = array())
- {
- $start = microtime(true);
- $result = $this->transport->search($query, $options);
- $result['time'] = microtime(true) - $start;
- return $result;
- }
- /**
- * Perform scrollSearch
- * @param $query
- * @param string $scroll
- * @param int $size
- * @param array $options
- * @return mixed
- */
- 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;
- }
- /**
- * Flush this index/type combination
- *
- * @return array
- * @param mixed $id If id is supplied, delete that id for this index
- * if not wipe the entire index
- * @param array $options Parameters to pass to delete action
- */
- 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);
- }
- /**
- * Flush this index/type combination
- *
- * @return array
- * @param mixed $query Text or array based query to delete everything that matches
- * @param array $options Parameters to pass to delete action
- */
- public function deleteByQuery($query, array $options = array())
- {
- return $this->transport->deleteByQuery($query, $options);
- }
- /**
- * Perform refresh of current indexes
- *
- * @return array
- */
- public function refresh()
- {
- return $this->transport->request(array('_refresh'), 'GET');
- }
- /**
- * Expand a given path (array or string)
- * If this is not an absolute path index + type will be prepended
- * If it is an absolute path it will be used as is
- *
- * @param mixed $path
- * @return array
- */
- protected function expandPath($path)
- {
- $path = (array)$path;
- $isAbsolute = $path[0][0] === '/';
- return $isAbsolute
- ? $path
- : array_merge((array)$this->type, $path);
- }
- /**
- * Parse a DSN string into an associative array
- *
- * @param string $dsn
- * @return array
- */
- 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');
- }
- /**
- * Create a bulk-transaction
- *
- * @return Bulk
- */
- public function createBulk()
- {
- return new Bulk($this);
- }
- /**
- * 增加Index
- *
- * @param $index
- * @param $mappings
- * @return mixed
- */
- public function createBase($index, $mappings)
- {
- return $this->transport->createBase($index, $mappings);
- }
- /**
- * 删除Index
- *
- * @param $index
- * @return mixed
- */
- public function deleteBase($index)
- {
- return $this->transport->deleteBase($index);
- }
- /**
- * Begin a transparent bulk-transaction
- * if one is already running, return its handle
- * @return Bulk
- */
- public function beginBulk()
- {
- if (!$this->bulk) {
- $this->bulk = $this->createBulk();
- }
- return $this->bulk;
- }
- /**
- * @see beginBulk
- */
- public function begin()
- {
- return $this->beginBulk();
- }
- /**
- * commit a bulk-transaction
- * @return array
- */
- public function commitBulk()
- {
- if ($this->bulk) {
- $result = $this->bulk->commit();
- $this->bulk = null;
- return $result;
- }
- }
- /**
- * @see commitBulk
- */
- public function commit()
- {
- return $this->commitBulk();
- }
- }
|