diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Type')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Type/AbstractType.php | 200 | ||||
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Type/Mapping.php | 280 |
2 files changed, 480 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Type/AbstractType.php b/vendor/ruflin/elastica/lib/Elastica/Type/AbstractType.php new file mode 100644 index 00000000..ba2e0430 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Type/AbstractType.php @@ -0,0 +1,200 @@ +<?php + +namespace Elastica\Type; + +use Elastica\Client; +use Elastica\Exception\InvalidException; +use Elastica\Index; +use Elastica\SearchableInterface; +use Elastica\Type as BaseType; +use Elastica\Util; + +/** + * Abstract helper class to implement search indices based on models. + * + * This abstract model should help creating search index and a subtype + * with some easy config entries that are overloaded. + * + * The following variables have to be set: + * - $_indexName + * - $_typeName + * + * The following variables can be set for additional configuration + * - $_mapping: Value type mapping for the given type + * - $_indexParams: Parameters for the index + * + * @todo Add some settings examples to code + * @category Xodoa + * @package Elastica + * @author Nicolas Ruflin <spam@ruflin.com> + */ +abstract class AbstractType implements SearchableInterface +{ + const MAX_DOCS_PER_REQUEST = 1000; + + /** + * Index name + * + * @var string Index name + */ + protected $_indexName = ''; + + /** + * Index name + * + * @var string Index name + */ + protected $_typeName = ''; + + /** + * Client + * + * @var \Elastica\Client Client object + */ + protected $_client = null; + + /** + * Index + * + * @var \Elastica\Index Index object + */ + protected $_index = null; + + /** + * Type + * + * @var \Elastica\Type Type object + */ + protected $_type = null; + + /** + * Mapping + * + * @var array Mapping + */ + protected $_mapping = array(); + + /** + * Index params + * + * @var array Index params + */ + protected $_indexParams = array(); + + /** + * Source + * + * @var boolean Source + */ + protected $_source = true; + + /** + * Creates index object with client connection + * + * Reads index and type name from protected vars _indexName and _typeName. + * Has to be set in child class + * + * @param \Elastica\Client $client OPTIONAL Client object + * @throws \Elastica\Exception\InvalidException + */ + public function __construct(Client $client = null) + { + if (!$client) { + $client = new Client(); + } + + if (empty($this->_indexName)) { + throw new InvalidException('Index name has to be set'); + } + + if (empty($this->_typeName)) { + throw new InvalidException('Type name has to be set'); + } + + $this->_client = $client; + $this->_index = new Index($this->_client, $this->_indexName); + $this->_type = new BaseType($this->_index, $this->_typeName); + } + + /** + * Creates the index and sets the mapping for this type + * + * @param bool $recreate OPTIONAL Recreates the index if true (default = false) + */ + public function create($recreate = false) + { + $this->getIndex()->create($this->_indexParams, $recreate); + + $mapping = new Mapping($this->getType()); + $mapping->setProperties($this->_mapping); + $mapping->setSource(array('enabled' => $this->_source)); + $mapping->send(); + } + + /** + * @param \Elastica\Query $query + * @param array|int $options + * @return \Elastica\Search + */ + public function createSearch($query = '', $options = null) + { + return $this->getType()->createSearch($query, $options); + } + + /** + * Search on the type + * + * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object + * @return \Elastica\ResultSet ResultSet with all results inside + * @see \Elastica\SearchableInterface::search + */ + public function search($query = '', $options = null) + { + return $this->getType()->search($query, $options = null); + } + + /** + * Count docs in the type based on query + * + * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object + * @return int number of documents matching the query + * @see \Elastica\SearchableInterface::count + */ + public function count($query = '') + { + return $this->getType()->count($query); + } + + /** + * Returns the search index + * + * @return \Elastica\Index Index object + */ + public function getIndex() + { + return $this->_index; + } + + /** + * Returns type object + * + * @return \Elastica\Type Type object + */ + public function getType() + { + return $this->_type; + } + + /** + * Converts given time to format: 1995-12-31T23:59:59Z + * + * This is the lucene date format + * + * @param int $date Date input (could be string etc.) -> must be supported by strtotime + * @return string Converted date string + */ + public function convertDate($date) + { + return Util::convertDate($date); + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Type/Mapping.php b/vendor/ruflin/elastica/lib/Elastica/Type/Mapping.php new file mode 100644 index 00000000..920093fb --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Type/Mapping.php @@ -0,0 +1,280 @@ +<?php + +namespace Elastica\Type; + +use Elastica\Exception\InvalidException; +use Elastica\Request; +use Elastica\Type; + +/** + * Elastica Mapping object + * + * @category Xodoa + * @package Elastica + * @author Nicolas Ruflin <spam@ruflin.com> + * @link http://www.elasticsearch.org/guide/reference/mapping/ + */ +class Mapping +{ + /** + * Mapping + * + * @var array Mapping + */ + protected $_mapping = array(); + + /** + * Type + * + * @var \Elastica\Type Type object + */ + protected $_type = null; + + /** + * Construct Mapping + * + * @param \Elastica\Type $type OPTIONAL Type object + * @param array $properties OPTIONAL Properties + */ + public function __construct(Type $type = null, array $properties = array()) + { + if ($type) { + $this->setType($type); + } + + if (!empty($properties)) { + $this->setProperties($properties); + } + } + + /** + * Sets the mapping type + * Enter description here ... + * @param \Elastica\Type $type Type object + * @return \Elastica\Type\Mapping Current object + */ + public function setType(Type $type) + { + $this->_type = $type; + + return $this; + } + + /** + * Sets the mapping properties + * + * @param array $properties Properties + * @return \Elastica\Type\Mapping Mapping object + */ + public function setProperties(array $properties) + { + return $this->setParam('properties', $properties); + } + + /** + * Gets the mapping properties + * + * @return array $properties Properties + */ + public function getProperties() + { + return $this->getParam('properties'); + } + + /** + * Sets the mapping _meta + * @param array $meta metadata + * @return \Elastica\Type\Mapping Mapping object + * @link http://www.elasticsearch.org/guide/reference/mapping/meta.html + */ + public function setMeta(array $meta) + { + return $this->setParam('_meta', $meta); + } + + /** + * Returns mapping type + * + * @return \Elastica\Type Type + */ + public function getType() + { + return $this->_type; + } + + /** + * Sets source values + * + * To disable source, argument is + * array('enabled' => false) + * + * @param array $source Source array + * @return \Elastica\Type\Mapping Current object + * @link http://www.elasticsearch.org/guide/reference/mapping/source-field.html + */ + public function setSource(array $source) + { + return $this->setParam('_source', $source); + } + + /** + * Disables the source in the index + * + * Param can be set to true to enable again + * + * @param bool $enabled OPTIONAL (default = false) + * @return \Elastica\Type\Mapping Current object + */ + public function disableSource($enabled = false) + { + return $this->setSource(array('enabled' => $enabled)); + } + + /** + * Sets raw parameters + * + * Possible options: + * _uid + * _id + * _type + * _source + * _all + * _analyzer + * _boost + * _parent + * _routing + * _index + * _size + * properties + * + * @param string $key Key name + * @param mixed $value Key value + * @return \Elastica\Type\Mapping Current object + */ + public function setParam($key, $value) + { + $this->_mapping[$key] = $value; + + return $this; + } + + /** + * Get raw parameters + * + * @see setParam + * @param string $key Key name + * @return mixed $value Key value + */ + public function getParam($key) + { + return isset($this->_mapping[$key])?$this->_mapping[$key]:null; + } + + /** + * Sets params for the "_all" field + * + * @param array $params _all Params (enabled, store, term_vector, analyzer) + * @return \Elastica\Type\Mapping + */ + public function setAllField(array $params) + { + return $this->setParam('_all', $params); + } + + /** + * Enables the "_all" field + * + * @param bool $enabled OPTIONAL (default = true) + * @return \Elastica\Type\Mapping + */ + public function enableAllField($enabled = true) + { + return $this->setAllField(array('enabled' => $enabled)); + } + + /** + * Set TTL + * + * @param array $params TTL Params (enabled, default, ...) + * @return \Elastica\Type\Mapping + */ + public function setTtl(array $params) + { + return $this->setParam('_ttl', $params); + + } + + /** + * Enables TTL for all documents in this type + * + * @param bool $enabled OPTIONAL (default = true) + * @return \Elastica\Type\Mapping + */ + public function enableTtl($enabled = true) + { + return $this->setTTL(array('enabled' => $enabled)); + } + + /** + * Set parent type + * + * @param string $type Parent type + * @return \Elastica\Type\Mapping + */ + public function setParent($type) + { + return $this->setParam('_parent', array('type' => $type)); + } + + /** + * Converts the mapping to an array + * + * @throws \Elastica\Exception\InvalidException + * @return array Mapping as array + */ + public function toArray() + { + $type = $this->getType(); + + if (empty($type)) { + throw new InvalidException('Type has to be set'); + } + + return array($type->getName() => $this->_mapping); + } + + /** + * Submits the mapping and sends it to the server + * + * @return \Elastica\Response Response object + */ + public function send() + { + $path = '_mapping'; + + return $this->getType()->request($path, Request::PUT, $this->toArray()); + } + + /** + * Creates a mapping object + * + * @param array|\Elastica\Type\Mapping $mapping Mapping object or properties array + * @return \Elastica\Type\Mapping Mapping object + * @throws \Elastica\Exception\InvalidException If invalid type + */ + public static function create($mapping) + { + if (is_array($mapping)) { + $mappingObject = new Mapping(); + $mappingObject->setProperties($mapping); + } else { + $mappingObject = $mapping; + } + + if (!$mappingObject instanceof Mapping) { + throw new InvalidException('Invalid object type'); + } + + return $mappingObject; + } +} |