diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Type.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Type.php | 217 |
1 files changed, 126 insertions, 91 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Type.php b/vendor/ruflin/elastica/lib/Elastica/Type.php index 1ed952db..8abec330 100644 --- a/vendor/ruflin/elastica/lib/Elastica/Type.php +++ b/vendor/ruflin/elastica/lib/Elastica/Type.php @@ -1,36 +1,31 @@ <?php - namespace Elastica; -use Elastica\Document; -use Elastica\Exception\RuntimeException; use Elastica\Exception\InvalidException; use Elastica\Exception\NotFoundException; -use Elastica\Exception\ResponseException; +use Elastica\Exception\RuntimeException; use Elastica\Type\Mapping; /** - * Elastica type object + * Elastica type object. * * elasticsearch has for every types as a substructure. This object * represents a type inside a context * The hierarchy is as following: client -> index -> type -> document * - * @category Xodoa - * @package Elastica * @author Nicolas Ruflin <spam@ruflin.com> */ class Type implements SearchableInterface { /** - * Index + * Index. * * @var \Elastica\Index Index object */ protected $_index = null; /** - * Type name + * Type name. * * @var string Type name */ @@ -42,10 +37,10 @@ class Type implements SearchableInterface protected $_serializer; /** - * Creates a new type object inside the given index + * Creates a new type object inside the given index. * * @param \Elastica\Index $index Index Object - * @param string $name Type name + * @param string $name Type name */ public function __construct(Index $index, $name) { @@ -54,9 +49,10 @@ class Type implements SearchableInterface } /** - * Adds the given document to the search index + * Adds the given document to the search index. + * + * @param \Elastica\Document $doc Document with data * - * @param \Elastica\Document $doc Document with data * @return \Elastica\Response */ public function addDocument(Document $doc) @@ -111,8 +107,10 @@ class Type implements SearchableInterface /** * @param $object * @param Document $doc - * @return Response + * * @throws Exception\RuntimeException + * + * @return Response */ public function addObject($object, Document $doc = null) { @@ -130,37 +128,46 @@ class Type implements SearchableInterface } /** - * Update document, using update script. Requires elasticsearch >= 0.19.0 + * Update document, using update script. Requires elasticsearch >= 0.19.0. + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html + * + * @param \Elastica\Document|\Elastica\Script $data Document with update data + * @param array $options array of query params to use for query. For possible options check es api * - * @param \Elastica\Document|\Elastica\Script $data Document with update data * @throws \Elastica\Exception\InvalidException + * * @return \Elastica\Response - * @link http://www.elasticsearch.org/guide/reference/api/update.html */ - public function updateDocument($data) + public function updateDocument($data, array $options = array()) { if (!($data instanceof Document) && !($data instanceof Script)) { - throw new \InvalidArgumentException("Data should be a Document or Script"); + throw new \InvalidArgumentException('Data should be a Document or Script'); } if (!$data->hasId()) { throw new InvalidException('Document or Script id is not set'); } + $id = urlencode($data->getId()); + return $this->getIndex()->getClient()->updateDocument( - $data->getId(), + $id, $data, $this->getIndex()->getName(), - $this->getName() + $this->getName(), + $options ); } /** - * Uses _bulk to send documents to the server + * Uses _bulk to send documents to the server. + * + * @param array|\Elastica\Document[] $docs Array of Elastica\Document * - * @param array|\Elastica\Document[] $docs Array of Elastica\Document * @return \Elastica\Bulk\ResponseSet - * @link http://www.elasticsearch.org/guide/reference/api/bulk.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html */ public function updateDocuments(array $docs) { @@ -172,11 +179,13 @@ class Type implements SearchableInterface } /** - * Uses _bulk to send documents to the server + * Uses _bulk to send documents to the server. + * + * @param array|\Elastica\Document[] $docs Array of Elastica\Document * - * @param array|\Elastica\Document[] $docs Array of Elastica\Document * @return \Elastica\Bulk\ResponseSet - * @link http://www.elasticsearch.org/guide/reference/api/bulk.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html */ public function addDocuments(array $docs) { @@ -188,11 +197,13 @@ class Type implements SearchableInterface } /** - * Uses _bulk to send documents to the server + * Uses _bulk to send documents to the server. * * @param objects[] $objects + * * @return \Elastica\Bulk\ResponseSet - * @link http://www.elasticsearch.org/guide/reference/api/bulk.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html */ public function addObjects(array $objects) { @@ -213,27 +224,25 @@ class Type implements SearchableInterface } /** - * Get the document from search index + * Get the document from search index. + * + * @param string $id Document id + * @param array $options Options for the get request. * - * @param string $id Document id - * @param array $options Options for the get request. * @throws \Elastica\Exception\NotFoundException + * @throws \Elastica\Exception\ResponseException + * * @return \Elastica\Document */ public function getDocument($id, $options = array()) { $path = urlencode($id); - try { - $response = $this->request($path, Request::GET, array(), $options); - $result = $response->getData(); - } catch (ResponseException $e) { - throw new NotFoundException('doc id ' . $id . ' not found'); - } + $response = $this->request($path, Request::GET, array(), $options); + $result = $response->getData(); - $info = $response->getTransferInfo(); - if ($info['http_code'] !== 200) { - throw new NotFoundException('doc id ' . $id . ' not found'); + if (!isset($result['found']) || $result['found'] === false) { + throw new NotFoundException('doc id '.$id.' not found'); } if (isset($result['fields'])) { @@ -251,8 +260,9 @@ class Type implements SearchableInterface } /** - * @param string $id + * @param string $id * @param array|string $data + * * @return Document */ public function createDocument($id = '', $data = array()) @@ -264,7 +274,7 @@ class Type implements SearchableInterface } /** - * Returns the type name + * Returns the type name. * * @return string Type name */ @@ -274,9 +284,10 @@ class Type implements SearchableInterface } /** - * Sets value type mapping for this type + * Sets value type mapping for this type. + * + * @param \Elastica\Type\Mapping|array $mapping Elastica\Type\MappingType object or property array with all mappings * - * @param \Elastica\Type\Mapping|array $mapping Elastica\Type\MappingType object or property array with all mappings * @return \Elastica\Response */ public function setMapping($mapping) @@ -288,7 +299,7 @@ class Type implements SearchableInterface } /** - * Returns current mapping for the given type + * Returns current mapping for the given type. * * @return array Current mapping */ @@ -308,10 +319,11 @@ class Type implements SearchableInterface } /** - * Create search object + * Create search object. + * + * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object + * @param int|array $options OPTIONAL Limit or associative array of options (option=>value) * - * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object - * @param int|array $options OPTIONAL Limit or associative array of options (option=>value) * @return \Elastica\Search */ public function createSearch($query = '', $options = null) @@ -325,11 +337,13 @@ class Type implements SearchableInterface } /** - * Do a search on this type + * Do a search on this type. + * + * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object + * @param int|array $options OPTIONAL Limit or associative array of options (option=>value) + * + * @return \Elastica\ResultSet ResultSet with all results inside * - * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object - * @param int|array $options OPTIONAL Limit or associative array of options (option=>value) - * @return \Elastica\ResultSet ResultSet with all results inside * @see \Elastica\SearchableInterface::search */ public function search($query = '', $options = null) @@ -340,10 +354,12 @@ class Type implements SearchableInterface } /** - * Count docs by query + * Count docs by 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 * - * @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 = '') @@ -354,7 +370,7 @@ class Type implements SearchableInterface } /** - * Returns index client + * Returns index client. * * @return \Elastica\Index Index object */ @@ -365,6 +381,7 @@ class Type implements SearchableInterface /** * @param \Elastica\Document $document + * * @return \Elastica\Response */ public function deleteDocument(Document $document) @@ -378,18 +395,21 @@ class Type implements SearchableInterface 'replication', 'consistency', 'refresh', - 'timeout' + 'timeout', ) ); + return $this->deleteById($document->getId(), $options); } /** - * Uses _bulk to delete documents from the server + * Uses _bulk to delete documents from the server. + * + * @param array|\Elastica\Document[] $docs Array of Elastica\Document * - * @param array|\Elastica\Document[] $docs Array of Elastica\Document * @return \Elastica\Bulk\ResponseSet - * @link http://www.elasticsearch.org/guide/reference/api/bulk.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html */ public function deleteDocuments(array $docs) { @@ -401,14 +421,17 @@ class Type implements SearchableInterface } /** - * Deletes an entry by its unique identifier + * Deletes an entry by its unique identifier. + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html + * + * @param int|string $id Document id + * @param array $options * - * @param int|string $id Document id - * @param array $options * @throws \InvalidArgumentException * @throws \Elastica\Exception\NotFoundException - * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/delete.html + * + * @return \Elastica\Response Response object */ public function deleteById($id, array $options = array()) { @@ -423,17 +446,18 @@ class Type implements SearchableInterface $responseData = $response->getData(); if (isset($responseData['found']) && false == $responseData['found']) { - throw new NotFoundException('Doc id ' . $id . ' not found and can not be deleted'); + throw new NotFoundException('Doc id '.$id.' not found and can not be deleted'); } return $response; } /** - * Deletes the given list of ids from this type + * Deletes the given list of ids from this type. + * + * @param array $ids + * @param string|false $routing Optional routing key for all ids * - * @param array $ids - * @param string|false $routing Optional routing key for all ids * @return \Elastica\Response Response object */ public function deleteIds(array $ids, $routing = false) @@ -442,18 +466,21 @@ class Type implements SearchableInterface } /** - * Deletes entries in the db based on a query + * Deletes entries in the db based on a query. + * + * @param \Elastica\Query|string $query Query object + * @param array $options Optional params * - * @param \Elastica\Query|string $query Query object - * @param array $options Optional params * @return \Elastica\Response - * @link http://www.elasticsearch.org/guide/reference/api/delete-by-query.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html */ public function deleteByQuery($query, array $options = array()) { if (is_string($query)) { // query_string queries are not supported for delete by query operations $options['q'] = $query; + return $this->request('_query', Request::DELETE, array(), $options); } $query = Query::create($query); @@ -474,56 +501,64 @@ class Type implements SearchableInterface } /** - * More like this query based on the given object + * More like this query based on the given object. * * The id in the given object has to be set * - * @param \Elastica\Document $doc Document to query for similar objects - * @param array $params OPTIONAL Additional arguments for the query - * @param string|array|\Elastica\Query $query OPTIONAL Query to filter the moreLikeThis results - * @return \Elastica\ResultSet ResultSet with all results inside - * @link http://www.elasticsearch.org/guide/reference/api/more-like-this.html + * @param \Elastica\Document $doc Document to query for similar objects + * @param array $params OPTIONAL Additional arguments for the query + * @param string|array|\Elastica\Query $query OPTIONAL Query to filter the moreLikeThis results + * + * @return \Elastica\ResultSet ResultSet with all results inside + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-more-like-this.html */ public function moreLikeThis(Document $doc, $params = array(), $query = array()) { - $path = $doc->getId() . '/_mlt'; + $path = $doc->getId().'/_mlt'; $query = Query::create($query); $response = $this->request($path, Request::GET, $query->toArray(), $params); - return new ResultSet($response, $query); + return ResultSet::create($response, $query); } /** - * Makes calls to the elasticsearch server based on this type + * Makes calls to the elasticsearch server based on this type. + * + * @param string $path Path to call + * @param string $method Rest method to use (GET, POST, DELETE, PUT) + * @param array $data OPTIONAL Arguments as array + * @param array $query OPTIONAL Query params * - * @param string $path Path to call - * @param string $method Rest method to use (GET, POST, DELETE, PUT) - * @param array $data OPTIONAL Arguments as array - * @param array $query OPTIONAL Query params * @return \Elastica\Response Response object */ public function request($path, $method, $data = array(), array $query = array()) { - $path = $this->getName() . '/' . $path; + $path = $this->getName().'/'.$path; return $this->getIndex()->request($path, $method, $data, $query); } /** - * Sets the serializer callable used in addObject + * Sets the serializer callable used in addObject. + * * @see \Elastica\Type::addObject * * @param array|string $serializer @see \Elastica\Type::_serializer + * + * @return $this */ public function setSerializer($serializer) { $this->_serializer = $serializer; + + return $this; } /** - * Checks if the given type exists in Index + * Checks if the given type exists in Index. * * @return bool True if type exists */ @@ -532,6 +567,6 @@ class Type implements SearchableInterface $response = $this->getIndex()->request($this->getName(), Request::HEAD); $info = $response->getTransferInfo(); - return (bool)($info['http_code'] == 200); + return (bool) ($info['http_code'] == 200); } } |