summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Multi
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Multi')
-rw-r--r--vendor/ruflin/elastica/lib/Elastica/Multi/ResultSet.php208
-rw-r--r--vendor/ruflin/elastica/lib/Elastica/Multi/Search.php198
2 files changed, 406 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Multi/ResultSet.php b/vendor/ruflin/elastica/lib/Elastica/Multi/ResultSet.php
new file mode 100644
index 00000000..a0df5785
--- /dev/null
+++ b/vendor/ruflin/elastica/lib/Elastica/Multi/ResultSet.php
@@ -0,0 +1,208 @@
+<?php
+
+namespace Elastica\Multi;
+use Elastica\Exception\InvalidException;
+use Elastica\Response;
+use Elastica\Search as BaseSearch;
+use Elastica\ResultSet as BaseResultSet;
+
+/**
+ * Elastica multi search result set
+ * List of result sets for each search request
+ *
+ * @category Xodoa
+ * @package Elastica
+ * @author munkie
+ */
+class ResultSet implements \Iterator, \ArrayAccess, \Countable
+{
+ /**
+ * Result Sets
+ *
+ * @var array|\Elastica\ResultSet[] Result Sets
+ */
+ protected $_resultSets = array();
+
+ /**
+ * Current position
+ *
+ * @var int Current position
+ */
+ protected $_position = 0;
+
+ /**
+ * Response
+ *
+ * @var \Elastica\Response Response object
+ */
+ protected $_response;
+
+ /**
+ * Constructs ResultSet object
+ *
+ * @param \Elastica\Response $response
+ * @param array|\Elastica\Search[] $searches
+ */
+ public function __construct(Response $response, array $searches)
+ {
+ $this->rewind();
+ $this->_init($response, $searches);
+ }
+
+ /**
+ * @param \Elastica\Response $response
+ * @param array|\Elastica\Search[] $searches
+ * @throws \Elastica\Exception\InvalidException
+ */
+ protected function _init(Response $response, array $searches)
+ {
+ $this->_response = $response;
+ $responseData = $response->getData();
+
+ if (isset($responseData['responses']) && is_array($responseData['responses'])) {
+ reset($searches);
+ foreach ($responseData['responses'] as $key => $responseData) {
+ $currentSearch = each($searches);
+
+ if ($currentSearch === false) {
+ throw new InvalidException('No result found for search #' . $key);
+ } elseif (!$currentSearch['value'] instanceof BaseSearch) {
+ throw new InvalidException('Invalid object for search #' . $key . ' provided. Should be Elastica\Search');
+ }
+
+ $search = $currentSearch['value'];
+ $query = $search->getQuery();
+
+ $response = new Response($responseData);
+ $this->_resultSets[$currentSearch['key']] = new BaseResultSet($response, $query);
+ }
+ }
+ }
+
+ /**
+ * @return array|\Elastica\ResultSet[]
+ */
+ public function getResultSets()
+ {
+ return $this->_resultSets;
+ }
+
+ /**
+ * Returns response object
+ *
+ * @return \Elastica\Response Response object
+ */
+ public function getResponse()
+ {
+ return $this->_response;
+ }
+
+ /**
+ * There is at least one result set with error
+ *
+ * @return bool
+ */
+ public function hasError()
+ {
+ foreach ($this->getResultSets() as $resultSet) {
+ if ($resultSet->getResponse()->hasError()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * @return bool|\Elastica\ResultSet
+ */
+ public function current()
+ {
+ if ($this->valid()) {
+ return $this->_resultSets[$this->key()];
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * @return void
+ */
+ public function next()
+ {
+ $this->_position++;
+ }
+
+ /**
+ * @return int
+ */
+ public function key()
+ {
+ return $this->_position;
+ }
+
+ /**
+ * @return bool
+ */
+ public function valid()
+ {
+ return isset($this->_resultSets[$this->key()]);
+ }
+
+ /**
+ * @return void
+ */
+ public function rewind()
+ {
+ $this->_position = 0;
+ }
+
+ /**
+ * @return int
+ */
+ public function count()
+ {
+ return count($this->_resultSets);
+ }
+
+ /**
+ * @param string|int $offset
+ * @return boolean true on success or false on failure.
+ */
+ public function offsetExists($offset)
+ {
+ return isset($this->_resultSets[$offset]);
+ }
+
+ /**
+ * @param mixed $offset
+ * @return mixed Can return all value types.
+ */
+ public function offsetGet($offset)
+ {
+ return isset($this->_resultSets[$offset]) ? $this->_resultSets[$offset] : null;
+ }
+
+ /**
+ * @param mixed $offset
+ * @param mixed $value
+ * @return void
+ */
+ public function offsetSet($offset, $value)
+ {
+ if (is_null($offset)) {
+ $this->_resultSets[] = $value;
+ } else {
+ $this->_resultSets[$offset] = $value;
+ }
+ }
+
+ /**
+ * @param mixed $offset
+ * @return void
+ */
+ public function offsetUnset($offset)
+ {
+ unset($this->_resultSets[$offset]);
+ }
+}
diff --git a/vendor/ruflin/elastica/lib/Elastica/Multi/Search.php b/vendor/ruflin/elastica/lib/Elastica/Multi/Search.php
new file mode 100644
index 00000000..a9874eb2
--- /dev/null
+++ b/vendor/ruflin/elastica/lib/Elastica/Multi/Search.php
@@ -0,0 +1,198 @@
+<?php
+
+namespace Elastica\Multi;
+
+use Elastica\Client;
+use Elastica\JSON;
+use Elastica\Request;
+use Elastica\Search as BaseSearch;
+
+/**
+ * Elastica multi search
+ *
+ * @category Xodoa
+ * @package Elastica
+ * @author munkie
+ * @link http://www.elasticsearch.org/guide/reference/api/multi-search.html
+ */
+class Search
+{
+ /**
+ * @var array|\Elastica\Search[]
+ */
+ protected $_searches = array();
+
+ /**
+ * @var array
+ */
+ protected $_options = array();
+
+ /**
+ * @var \Elastica\Client
+ */
+ protected $_client;
+
+ /**
+ * Constructs search object
+ *
+ * @param \Elastica\Client $client Client object
+ */
+ public function __construct(Client $client)
+ {
+ $this->setClient($client);
+ }
+
+ /**
+ * @return \Elastica\Client
+ */
+ public function getClient()
+ {
+ return $this->_client;
+ }
+
+ /**
+ * @param \Elastica\Client $client
+ * @return \Elastica\Multi\Search
+ */
+ public function setClient(Client $client)
+ {
+ $this->_client = $client;
+
+ return $this;
+ }
+
+ /**
+ * @return \Elastica\Multi\Search
+ */
+ public function clearSearches()
+ {
+ $this->_searches = array();
+
+ return $this;
+ }
+
+ /**
+ * @param \Elastica\Search $search
+ * @param string $key Optional key
+ * @return \Elastica\Multi\Search
+ */
+ public function addSearch(BaseSearch $search, $key = null)
+ {
+ if ($key) {
+ $this->_searches[$key] = $search;
+ } else {
+ $this->_searches[] = $search;
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param array|\Elastica\Search[] $searches
+ * @return \Elastica\Multi\Search
+ */
+ public function addSearches(array $searches)
+ {
+ foreach ($searches as $key => $search) {
+ $this->addSearch($search, $key);
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param array|\Elastica\Search[] $searches
+ * @return \Elastica\Multi\Search
+ */
+ public function setSearches(array $searches)
+ {
+ $this->clearSearches();
+ $this->addSearches($searches);
+
+ return $this;
+ }
+
+ /**
+ * @return array|\Elastica\Search[]
+ */
+ public function getSearches()
+ {
+ return $this->_searches;
+ }
+
+ /**
+ * @param string $searchType
+ * @return \Elastica\Multi\Search
+ */
+ public function setSearchType($searchType)
+ {
+ $this->_options[BaseSearch::OPTION_SEARCH_TYPE] = $searchType;
+
+ return $this;
+ }
+
+ /**
+ * @return \Elastica\Multi\ResultSet
+ */
+ public function search()
+ {
+ $data = $this->_getData();
+
+ $response = $this->getClient()->request(
+ '_msearch',
+ Request::POST,
+ $data,
+ $this->_options
+ );
+
+ return new ResultSet($response, $this->getSearches());
+ }
+
+ /**
+ * @return string
+ */
+ protected function _getData()
+ {
+ $data = '';
+ foreach ($this->getSearches() as $search) {
+ $data.= $this->_getSearchData($search);
+ }
+
+ return $data;
+ }
+
+ /**
+ * @param \Elastica\Search $search
+ * @return string
+ */
+ protected function _getSearchData(BaseSearch $search)
+ {
+ $header = $this->_getSearchDataHeader($search);
+ $header = (empty($header)) ? new \stdClass : $header;
+ $query = $search->getQuery();
+
+ $data = JSON::stringify($header) . "\n";
+ $data.= JSON::stringify($query->toArray()) . "\n";
+
+ return $data;
+ }
+
+ /**
+ * @param \Elastica\Search $search
+ * @return array
+ */
+ protected function _getSearchDataHeader(BaseSearch $search)
+ {
+ $header = $search->getOptions();
+
+ if ($search->hasIndices()) {
+ $header['index'] = $search->getIndices();
+ }
+
+ if ($search->hasTypes()) {
+ $header['types'] = $search->getTypes();
+ }
+
+ return $header;
+ }
+}