diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Index.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Index.php | 219 |
1 files changed, 136 insertions, 83 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Index.php b/vendor/ruflin/elastica/lib/Elastica/Index.php index 5e1e7138..3b8d431b 100644 --- a/vendor/ruflin/elastica/lib/Elastica/Index.php +++ b/vendor/ruflin/elastica/lib/Elastica/Index.php @@ -1,5 +1,4 @@ <?php - namespace Elastica; use Elastica\Exception\InvalidException; @@ -9,53 +8,53 @@ use Elastica\Index\Stats as IndexStats; use Elastica\Index\Status as IndexStatus; /** - * Elastica index object + * Elastica index object. * * Handles reads, deletes and configurations of an index * - * @category Xodoa - * @package Elastica * @author Nicolas Ruflin <spam@ruflin.com> */ class Index implements SearchableInterface { /** - * Index name + * Index name. * * @var string Index name */ protected $_name = ''; /** - * Client object + * Client object. * * @var \Elastica\Client Client object */ protected $_client = null; /** - * Creates a new index object + * Creates a new index object. * * All the communication to and from an index goes of this object * - * @param \Elastica\Client $client Client object - * @param string $name Index name + * @param \Elastica\Client $client Client object + * @param string $name Index name + * * @throws \Elastica\Exception\InvalidException */ public function __construct(Client $client, $name) { $this->_client = $client; - if (!is_string($name)) { - throw new InvalidException('Index name should be of type string'); + if (!is_scalar($name)) { + throw new InvalidException('Index name should be a scalar type'); } - $this->_name = $name; + $this->_name = (string) $name; } /** - * Returns a type object for the current index with the given name + * Returns a type object for the current index with the given name. + * + * @param string $type Type name * - * @param string $type Type name * @return \Elastica\Type Type object */ public function getType($type) @@ -64,7 +63,7 @@ class Index implements SearchableInterface } /** - * Returns the current status of the index + * Returns the current status of the index. * * @return \Elastica\Index\Status Index status */ @@ -74,7 +73,7 @@ class Index implements SearchableInterface } /** - * Return Index Stats + * Return Index Stats. * * @return \Elastica\Index\Stats */ @@ -94,10 +93,10 @@ class Index implements SearchableInterface $response = $this->request($path, Request::GET); $data = $response->getData(); - + // Get first entry as if index is an Alias, the name of the mapping is the real name and not alias name $mapping = array_shift($data); - + if (isset($mapping['mappings'])) { return $mapping['mappings']; } @@ -106,7 +105,7 @@ class Index implements SearchableInterface } /** - * Returns the index settings object + * Returns the index settings object. * * @return \Elastica\Index\Settings Settings object */ @@ -116,11 +115,13 @@ class Index 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 updateDocuments(array $docs) { @@ -132,11 +133,13 @@ class Index 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) { @@ -148,7 +151,30 @@ class Index implements SearchableInterface } /** - * Deletes the index + * Deletes entries in the db based on a query. + * + * @param \Elastica\Query|string $query Query object + * @param array $options Optional params + * + * @return \Elastica\Response + * + * @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); + + return $this->request('_query', Request::DELETE, array('query' => $query->getQuery()), $options); + } + + /** + * Deletes the index. * * @return \Elastica\Response Response object */ @@ -160,11 +186,13 @@ class Index implements SearchableInterface } /** - * 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) { @@ -176,24 +204,27 @@ class Index implements SearchableInterface } /** - * Optimizes search index + * Optimizes search index. * * Detailed arguments can be found here in the link * - * @param array $args OPTIONAL Additional arguments + * @param array $args OPTIONAL Additional arguments + * * @return array Server response - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-optimize.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html */ public function optimize($args = array()) { - $this->request('_optimize', Request::POST, array(), $args); + return $this->request('_optimize', Request::POST, array(), $args); } /** - * Refreshes the index + * Refreshes the index. * * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html */ public function refresh() { @@ -201,16 +232,19 @@ class Index implements SearchableInterface } /** - * Creates a new index with the given arguments + * Creates a new index with the given arguments. * - * @param array $args OPTIONAL Arguments to use + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html + * + * @param array $args OPTIONAL Arguments to use * @param bool|array $options OPTIONAL * bool=> Deletes index first if already exists (default = false). * array => Associative array of options (option=>value) + * * @throws \Elastica\Exception\InvalidException * @throws \Elastica\Exception\ResponseException - * @return array Server response - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html + * + * @return array Server response */ public function create(array $args = array(), $options = null) { @@ -240,7 +274,7 @@ class Index implements SearchableInterface $query = array('routing' => $value); break; default: - throw new InvalidException('Invalid option ' . $key); + throw new InvalidException('Invalid option '.$key); break; } } @@ -251,7 +285,7 @@ class Index implements SearchableInterface } /** - * Checks if the given index is already created + * Checks if the given index is already created. * * @return bool True if index exists */ @@ -260,12 +294,13 @@ class Index implements SearchableInterface $response = $this->getClient()->request($this->getName(), Request::HEAD); $info = $response->getTransferInfo(); - return (bool)($info['http_code'] == 200); + return (bool) ($info['http_code'] == 200); } /** - * @param string $query - * @param int|array $options + * @param string|array|\Elastica\Query $query + * @param int|array $options + * * @return \Elastica\Search */ public function createSearch($query = '', $options = null) @@ -278,11 +313,13 @@ class Index implements SearchableInterface } /** - * Searches in this index + * Searches in this index. + * + * @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) @@ -293,10 +330,12 @@ class Index implements SearchableInterface } /** - * Counts results of query + * Counts results of 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 = '') @@ -307,21 +346,23 @@ class Index implements SearchableInterface } /** - * Opens an index + * Opens an index. * * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html */ public function open() { - $this->request('_open', Request::POST); + return $this->request('_open', Request::POST); } /** - * Closes the index + * Closes the index. * * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html */ public function close() { @@ -329,7 +370,7 @@ class Index implements SearchableInterface } /** - * Returns the index name + * Returns the index name. * * @return string Index name */ @@ -339,7 +380,7 @@ class Index implements SearchableInterface } /** - * Returns index client + * Returns index client. * * @return \Elastica\Client Index client object */ @@ -349,12 +390,14 @@ class Index implements SearchableInterface } /** - * Adds an alias to the current index + * Adds an alias to the current index. + * + * @param string $name Alias name + * @param bool $replace OPTIONAL If set, an existing alias will be replaced * - * @param string $name Alias name - * @param bool $replace OPTIONAL If set, an existing alias will be replaced * @return \Elastica\Response Response - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html */ public function addAlias($name, $replace = false) { @@ -375,11 +418,13 @@ class Index implements SearchableInterface } /** - * Removes an alias pointing to the current index + * Removes an alias pointing to the current index. + * + * @param string $name Alias name * - * @param string $name Alias name * @return \Elastica\Response Response - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html */ public function removeAlias($name) { @@ -391,10 +436,11 @@ class Index implements SearchableInterface } /** - * Clears the cache of an index + * Clears the cache of an index. * * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html */ public function clearCache() { @@ -404,24 +450,27 @@ class Index implements SearchableInterface } /** - * Flushes the index to storage + * Flushes the index to storage. * * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-flush.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html */ public function flush($refresh = false) { $path = '_flush'; + return $this->request($path, Request::POST, array(), array('refresh' => $refresh)); } /** - * Can be used to change settings during runtime. One example is to use - * if for bulk updating {@link http://www.elasticsearch.org/blog/2011/03/23/update-settings.html} + * Can be used to change settings during runtime. One example is to use it for bulk updating. + * + * @param array $data Data array * - * @param array $data Data array * @return \Elastica\Response Response object - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html */ public function setSettings(array $data) { @@ -429,34 +478,38 @@ class Index implements SearchableInterface } /** - * Makes calls to the elasticsearch server based on this index + * Makes calls to the elasticsearch server based on this index. + * + * @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->getClient()->request($path, $method, $data, $query); } /** - * Analyzes a string + * Analyzes a string. * * Detailed arguments can be found here in the link * - * @param string $text String to be analyzed - * @param array $args OPTIONAL Additional arguments + * @param string $text String to be analyzed + * @param array $args OPTIONAL Additional arguments + * * @return array Server response - * @link http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze.html + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html */ public function analyze($text, $args = array()) { $data = $this->request('_analyze', Request::POST, $text, $args)->getData(); + return $data['tokens']; } } |