Stores query time, and result array -> is given to result set, returned by ...
Author: Nicolas Ruflin (spam@ruflin.com)
 /**
  * @group unit
  */
 public function testExists()
 {
     $name = 'index_template1';
     $response = new Response('');
     $response->setTransferInfo(array('http_code' => 200));
     /** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */
     $clientMock = $this->getMock('\\Elastica\\Client', array('request'));
     $clientMock->expects($this->once())->method('request')->with('/_template/' . $name, Request::HEAD, array(), array())->willReturn($response);
     $indexTemplate = new IndexTemplate($clientMock, $name);
     $this->assertTrue($indexTemplate->exists());
 }
Exemplo n.º 2
0
 /**
  * Builds individual result objects.
  *
  * @param Response $response
  *
  * @return Result[]
  */
 private function buildResults(Response $response)
 {
     $data = $response->getData();
     $results = [];
     if (!isset($data['hits']['hits'])) {
         return $results;
     }
     foreach ($data['hits']['hits'] as $hit) {
         $results[] = new Result($hit);
     }
     return $results;
 }
Exemplo n.º 3
0
 /**
  * Makes calls to the elasticsearch server.
  *
  * @param \Elastica\Request $request
  * @param array             $params  Host, Port, ...
  *
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\InvalidException
  *
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $memcache = new \Memcache();
     $memcache->connect($this->getConnection()->getHost(), $this->getConnection()->getPort());
     $data = $request->getData();
     $content = '';
     if (!empty($data) || '0' === $data) {
         if (is_array($data)) {
             $content = JSON::stringify($data);
         } else {
             $content = $data;
         }
         // Escaping of / not necessary. Causes problems in base64 encoding of files
         $content = str_replace('\\/', '/', $content);
     }
     $responseString = '';
     $start = microtime(true);
     switch ($request->getMethod()) {
         case Request::POST:
         case Request::PUT:
             $key = $request->getPath();
             $this->_checkKeyLength($key);
             $memcache->set($key, $content);
             break;
         case Request::GET:
             $key = $request->getPath() . '?source=' . $content;
             $this->_checkKeyLength($key);
             $responseString = $memcache->get($key);
             break;
         case Request::DELETE:
             $key = $request->getPath() . '?source=' . $content;
             $this->_checkKeyLength($key);
             $responseString = $memcache->delete($key);
             break;
         default:
         case Request::HEAD:
             throw new InvalidException('Method ' . $request->getMethod() . ' is not supported in memcache transport');
     }
     $end = microtime(true);
     $response = new Response($responseString);
     if (\Elastica\Util::debugEnabled()) {
         $response->setQueryTime($end - $start);
     }
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
Exemplo n.º 4
0
 /**
  * Makes calls to the elasticsearch server
  *
  * All calls that are made to the server are done through this function
  *
  * @param  \Elastica\Request $request
  * @param  array $params Host, Port, ...
  * @throws \Elastica\Exception\ConnectionException
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\Connection\HttpException
  * @return \Elastica\Response                    Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     try {
         $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent());
         $options = array();
         if ($connection->getTimeout()) {
             $options['timeout'] = $connection->getTimeout();
         }
         if ($connection->getProxy()) {
             $options['proxy'] = $connection->getProxy();
         }
         $req = $client->createRequest($request->getMethod(), $this->_getActionPath($request), $options);
         $req->setHeaders($connection->hasConfig('headers') ?: array());
         $data = $request->getData();
         if (isset($data) && !empty($data)) {
             if ($req->getMethod() == Request::GET) {
                 $req->setMethod(Request::POST);
             }
             if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) {
                 $request->setMethod(Request::POST);
                 $req->setMethod(Request::POST);
             }
             if (is_array($data)) {
                 $content = JSON::stringify($data, 'JSON_ELASTICSEARCH');
             } else {
                 $content = $data;
             }
             $req->setBody(Stream::factory($content));
         }
         $start = microtime(true);
         $res = $client->send($req);
         $end = microtime(true);
         $response = new Response((string) $res->getBody(), $res->getStatusCode());
         if (defined('DEBUG') && DEBUG) {
             $response->setQueryTime($end - $start);
         }
         $response->setTransferInfo(array('request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode()));
         if ($response->hasError()) {
             throw new ResponseException($request, $response);
         }
         if ($response->hasFailedShards()) {
             throw new PartialShardFailureException($request, $response);
         }
         return $response;
     } catch (ClientException $e) {
         // ignore 4xx errors
     } catch (TransferException $e) {
         throw new GuzzleException($e, $request, new Response($e->getMessage()));
     }
 }
Exemplo n.º 5
0
 /**
  * @param Response     $response
  * @param BaseSearch[] $searches
  *
  * @return \Elastica\ResultSet[]
  */
 private function buildResultSets(Response $response, $searches)
 {
     $data = $response->getData();
     if (!isset($data['responses']) || !is_array($data['responses'])) {
         return [];
     }
     $resultSets = [];
     reset($searches);
     foreach ($data['responses'] as $responseData) {
         list($key, $search) = each($searches);
         $resultSets[$key] = $this->buildResultSet(new Response($responseData), $search);
     }
     return $resultSets;
 }
 /**
  * Loads all data into the results object (initialisation)
  *
  * @param \Elastica\Response $response Response object
  */
 protected function _init(Response $response)
 {
     $this->_response = $response;
     $result = $response->getData();
     $this->_totalHits = isset($result['hits']['total']) ? $result['hits']['total'] : 0;
     $this->_maxScore = isset($result['hits']['max_score']) ? $result['hits']['max_score'] : 0;
     $this->_took = isset($result['took']) ? $result['took'] : 0;
     $this->_timedOut = !empty($result['timed_out']);
     if (isset($result['hits']['hits'])) {
         foreach ($result['hits']['hits'] as $hit) {
             $this->_results[] = new Result($hit);
         }
     }
 }
Exemplo n.º 7
0
 /**
  * @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'])) {
         foreach ($responseData['responses'] as $key => $responseData) {
             if (!isset($searches[$key])) {
                 throw new InvalidException('No result found for search #' . $key);
             } elseif (!$searches[$key] instanceof BaseSearch) {
                 throw new InvalidException('Invalid object for search #' . $key . ' provided. Should be Elastica\\Search');
             }
             $search = $searches[$key];
             $query = $search->getQuery();
             $response = new Response($responseData);
             $this->_resultSets[] = new BaseResultSet($response, $query);
         }
     }
 }
Exemplo n.º 8
0
 /**
  * Makes calls to the elasticsearch server
  *
  * @param \Elastica\Request $request
  * @param  array                               $params Host, Port, ...
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\InvalidException
  * @return \Elastica\Response                   Response object
  */
 public function exec(Request $request, array $params)
 {
     $memcache = new \Memcache();
     $memcache->connect($this->getConnection()->getHost(), $this->getConnection()->getPort());
     // Finds right function name
     $function = strtolower($request->getMethod());
     $data = $request->getData();
     $content = '';
     if (!empty($data)) {
         if (is_array($data)) {
             $content = json_encode($data);
         } else {
             $content = $data;
         }
         // Escaping of / not necessary. Causes problems in base64 encoding of files
         $content = str_replace('\\/', '/', $content);
     }
     $responseString = '';
     switch ($function) {
         case 'post':
         case 'put':
             $memcache->set($request->getPath(), $content);
             break;
         case 'get':
             $responseString = $memcache->get($request->getPath() . '?source=' . $content);
             echo $responseString . PHP_EOL;
             break;
         case 'delete':
             break;
         default:
             throw new InvalidException('Method ' . $function . ' is not supported in memcache transport');
     }
     $response = new Response($responseString);
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
Exemplo n.º 9
0
 /**
  * logging.
  *
  * @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface.
  *
  * @param mixed $context
  */
 protected function _log($context)
 {
     if ($context instanceof ConnectionException) {
         $this->_logger->error('Elastica Request Failure', ['exception' => $context, 'request' => $context->getRequest()->toArray(), 'retry' => $this->hasConnection()]);
         return;
     }
     if ($context instanceof Request) {
         $this->_logger->debug('Elastica Request', ['request' => $context->toArray(), 'response' => $this->_lastResponse ? $this->_lastResponse->getData() : null, 'responseStatus' => $this->_lastResponse ? $this->_lastResponse->getStatus() : null]);
         return;
     }
     $this->_logger->debug('Elastica Request', ['message' => $context]);
 }
Exemplo n.º 10
0
 /**
  * Loads all data into the results object (initialisation)
  *
  * @param \Elastica\Response $response Response object
  */
 protected function _init(Response $response)
 {
     $this->_response = $response;
     $result = $response->getData();
     $this->_totalHits = isset($result['hits']['total']) ? $result['hits']['total'] : 0;
     $this->_maxScore = isset($result['hits']['max_score']) ? $result['hits']['max_score'] : 0;
     $this->_took = isset($result['took']) ? $result['took'] : 0;
     $this->_timedOut = !empty($result['timed_out']);
     if (isset($result['hits']['hits'])) {
         foreach ($result['hits']['hits'] as $hit) {
             $this->_results[] = new Result($hit);
         }
     }
     foreach ($result as $key => $value) {
         if ($key != '_shards') {
             if (isset($value[0]['options']) && count($value[0]['options']) > 0) {
                 $this->_suggests[$key] = $value[0];
             }
         }
     }
 }
Exemplo n.º 11
0
 /**
  * Makes calls to the elasticsearch server.
  *
  * All calls that are made to the server are done through this function
  *
  * @param \Elastica\Request $request
  * @param array             $params  Host, Port, ...
  *
  * @throws \Elastica\Exception\ConnectionException
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\Connection\HttpException
  *
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent());
     $options = array('exceptions' => false);
     if ($connection->getTimeout()) {
         $options['timeout'] = $connection->getTimeout();
     }
     $proxy = $connection->getProxy();
     // See: https://github.com/facebook/hhvm/issues/4875
     if (is_null($proxy) && defined('HHVM_VERSION')) {
         $proxy = getenv('http_proxy') ?: null;
     }
     if (!is_null($proxy)) {
         $options['proxy'] = $proxy;
     }
     $req = $this->_createPsr7Request($request, $connection);
     try {
         $start = microtime(true);
         $res = $client->send($req, $options);
         $end = microtime(true);
     } catch (TransferException $ex) {
         throw new GuzzleException($ex, $request, new Response($ex->getMessage()));
     }
     $response = new Response((string) $res->getBody(), $res->getStatusCode());
     $response->setQueryTime($end - $start);
     if ($connection->hasConfig('bigintConversion')) {
         $response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
     }
     $response->setTransferInfo(array('request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode()));
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
 /**
  * @group unit
  */
 public function testDecodeResponseWithBigIntSetToTrue()
 {
     $response = new Response(json_encode(array('took' => 213, 'items' => array(array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)), array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200))))));
     $response->setJsonBigintConversion(true);
     $this->assertTrue(is_array($response->getData()));
 }
Exemplo n.º 13
0
 /**
  * Makes calls to the elasticsearch server
  *
  * All calls that are made to the server are done through this function
  *
  * @param  \Elastica\Request $request
  * @param  array $params Host, Port, ...
  * @throws \Elastica\Exception\ConnectionException
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\Connection\HttpException
  * @return \Elastica\Response                    Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $conn = $this->_getConnection($connection->isPersistent());
     // If url is set, url is taken. Otherwise port, host and path
     $url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
     if (!empty($url)) {
         $baseUri = $url;
     } else {
         $baseUri = $this->_scheme . '://' . $connection->getHost() . ':' . $connection->getPort() . '/' . $connection->getPath();
     }
     $baseUri .= $request->getPath();
     $query = $request->getQuery();
     if (!empty($query)) {
         $baseUri .= '?' . http_build_query($query);
     }
     curl_setopt($conn, CURLOPT_URL, $baseUri);
     curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
     curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
     $proxy = $connection->getProxy();
     if (!is_null($proxy)) {
         curl_setopt($conn, CURLOPT_PROXY, $proxy);
     }
     $this->_setupCurl($conn);
     $headersConfig = $connection->hasConfig('headers') ? $connection->getConfig('headers') : array();
     if (!empty($headersConfig)) {
         $headers = array();
         while (list($header, $headerValue) = each($headersConfig)) {
             array_push($headers, $header . ': ' . $headerValue);
         }
         curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
     }
     // TODO: REFACTOR
     $data = $request->getData();
     $httpMethod = $request->getMethod();
     if (!empty($data) || '0' === $data) {
         if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) {
             $httpMethod = Request::POST;
         }
         if (is_array($data)) {
             $content = JSON::stringify($data, 'JSON_ELASTICSEARCH');
         } else {
             $content = $data;
         }
         // Escaping of / not necessary. Causes problems in base64 encoding of files
         $content = str_replace('\\/', '/', $content);
         curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
     } else {
         curl_setopt($conn, CURLOPT_POSTFIELDS, '');
     }
     curl_setopt($conn, CURLOPT_NOBODY, $httpMethod == 'HEAD');
     curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $httpMethod);
     if (defined('DEBUG') && DEBUG) {
         // Track request headers when in debug mode
         curl_setopt($conn, CURLINFO_HEADER_OUT, true);
     }
     $start = microtime(true);
     // cURL opt returntransfer leaks memory, therefore OB instead.
     ob_start();
     curl_exec($conn);
     $responseString = ob_get_clean();
     $end = microtime(true);
     // Checks if error exists
     $errorNumber = curl_errno($conn);
     $response = new Response($responseString, curl_getinfo($this->_getConnection(), CURLINFO_HTTP_CODE));
     if (defined('DEBUG') && DEBUG) {
         $response->setQueryTime($end - $start);
     }
     $response->setTransferInfo(curl_getinfo($conn));
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     if ($errorNumber > 0) {
         throw new HttpException($errorNumber, $request, $response);
     }
     return $response;
 }
Exemplo n.º 14
0
 /**
  * @param \Elastica\Response $response
  * @param \Elastica\Document $document
  * @param string             $fields   Array of field names to be populated or '_source' if whole document data should be updated
  */
 protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields)
 {
     $responseData = $response->getData();
     if ('_source' == $fields) {
         if (isset($responseData['get']['_source']) && is_array($responseData['get']['_source'])) {
             $document->setData($responseData['get']['_source']);
         }
     } else {
         $keys = explode(',', $fields);
         $data = $document->getData();
         foreach ($keys as $key) {
             if (isset($responseData['get']['fields'][$key])) {
                 $data[$key] = $responseData['get']['fields'][$key];
             } elseif (isset($data[$key])) {
                 unset($data[$key]);
             }
         }
         $document->setData($data);
     }
 }
 /**
  * merge top level multi-queries and resolve returned pageIds into Title objects.
  *
  * WARNING: experimental API
  *
  * @param string $query the user query
  * @param \Elastica\Response $response Response from elasticsearch _suggest api
  * @param array $profiles the suggestion profiles
  * @param int $limit Maximum suggestions to return, -1 for unlimited
  * @return SearchSuggestionSet a set of Suggestions
  */
 protected function postProcessSuggest(\Elastica\Response $response, $profiles, $limit = -1)
 {
     $this->logContext['elasticTookMs'] = intval($response->getQueryTime() * 1000);
     $data = $response->getData();
     unset($data['_shards']);
     $suggestions = array();
     foreach ($data as $name => $results) {
         $discount = $profiles[$name]['discount'];
         foreach ($results as $suggested) {
             foreach ($suggested['options'] as $suggest) {
                 $output = SuggestBuilder::decodeOutput($suggest['text']);
                 if ($output === null) {
                     // Ignore broken output
                     continue;
                 }
                 $pageId = $output['id'];
                 $type = $output['type'];
                 $score = $discount * $suggest['score'];
                 if (!isset($suggestions[$pageId]) || $score > $suggestions[$pageId]->getScore()) {
                     $suggestion = new SearchSuggestion($score, null, null, $pageId);
                     // If it's a title suggestion we have the text
                     if ($type === SuggestBuilder::TITLE_SUGGESTION) {
                         $suggestion->setText($output['text']);
                     }
                     $suggestions[$pageId] = $suggestion;
                 }
             }
         }
     }
     // simply sort by existing scores
     uasort($suggestions, function ($a, $b) {
         return $b->getScore() - $a->getScore();
     });
     $this->logContext['hitsTotal'] = count($suggestions);
     if ($limit > 0) {
         $suggestions = array_slice($suggestions, 0, $limit, true);
     }
     $this->logContext['hitsReturned'] = count($suggestions);
     $this->logContext['hitsOffset'] = 0;
     // we must fetch redirect data for redirect suggestions
     $missingText = array();
     foreach ($suggestions as $id => $suggestion) {
         if ($suggestion->getText() === null) {
             $missingText[] = $id;
         }
     }
     if (!empty($missingText)) {
         // Experimental.
         //
         // Second pass query to fetch redirects.
         // It's not clear if it's the best option, this will slowdown the whole query
         // when we hit a redirect suggestion.
         // Other option would be to encode redirects as a payload resulting in a
         // very big index...
         // XXX: we support only the content index
         $type = $this->connection->getPageType($this->indexBaseName, Connection::CONTENT_INDEX_TYPE);
         // NOTE: we are already in a poolCounterWork
         // Multi get is not supported by elastica
         $redirResponse = null;
         try {
             $redirResponse = $type->request('_mget', 'GET', array('ids' => $missingText), array('_source_include' => 'redirect'));
             if ($redirResponse->isOk()) {
                 $this->logContext['elasticTook2PassMs'] = intval($redirResponse->getQueryTime() * 1000);
                 $docs = $redirResponse->getData();
                 foreach ($docs['docs'] as $doc) {
                     if (empty($doc['_source']['redirect'])) {
                         continue;
                     }
                     // We use the original query, we should maybe use the variant that generated this result?
                     $text = Util::chooseBestRedirect($this->term, $doc['_source']['redirect']);
                     if (!empty($suggestions[$doc['_id']])) {
                         $suggestions[$doc['_id']]->setText($text);
                     }
                 }
             } else {
                 LoggerFactory::getInstance('CirrusSearch')->warning('Unable to fetch redirects for suggestion {query} with results {ids} : {error}', array('query' => $this->term, 'ids' => serialize($missingText), 'error' => $redirResponse->getError()));
             }
         } catch (\Elastica\Exception\ExceptionInterface $e) {
             LoggerFactory::getInstance('CirrusSearch')->warning('Unable to fetch redirects for suggestion {query} with results {ids} : {error}', array('query' => $this->term, 'ids' => serialize($missingText), 'error' => $this->extractMessage($e)));
         }
     }
     return new SearchSuggestionSet(array_filter($suggestions, function ($suggestion) {
         // text should be not empty for suggestions
         return $suggestion->getText() != null;
     }));
 }
Exemplo n.º 16
0
 /**
  * Makes calls to the elasticsearch server.
  *
  * All calls that are made to the server are done through this function
  *
  * @param \Elastica\Request $request
  * @param array             $params  Host, Port, ...
  *
  * @throws \Elastica\Exception\ConnectionException
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\Connection\HttpException
  *
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $conn = $this->_getConnection($connection->isPersistent());
     // If url is set, url is taken. Otherwise port, host and path
     $url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
     if (!empty($url)) {
         $baseUri = $url;
     } else {
         $baseUri = $this->_scheme . '://' . $connection->getHost() . ':' . $connection->getPort() . '/' . $connection->getPath();
     }
     $baseUri .= $request->getPath();
     $query = $request->getQuery();
     if (!empty($query)) {
         $baseUri .= '?' . http_build_query($query);
     }
     curl_setopt($conn, CURLOPT_URL, $baseUri);
     curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
     curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
     /* @see Connection::setConnectTimeout() */
     $connectTimeout = $connection->getConnectTimeout();
     if ($connectTimeout > 0) {
         curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
     }
     $proxy = $connection->getProxy();
     // See: https://github.com/facebook/hhvm/issues/4875
     if (is_null($proxy) && defined('HHVM_VERSION')) {
         $proxy = getenv('http_proxy') ?: null;
     }
     if (!is_null($proxy)) {
         curl_setopt($conn, CURLOPT_PROXY, $proxy);
     }
     $this->_setupCurl($conn);
     $headersConfig = $connection->hasConfig('headers') ? $connection->getConfig('headers') : array();
     if (!empty($headersConfig)) {
         $headers = array();
         while (list($header, $headerValue) = each($headersConfig)) {
             array_push($headers, $header . ': ' . $headerValue);
         }
         curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
     }
     // TODO: REFACTOR
     $data = $request->getData();
     $httpMethod = $request->getMethod();
     if (!empty($data) || '0' === $data) {
         if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) {
             $httpMethod = Request::POST;
         }
         if (is_array($data)) {
             $content = JSON::stringify($data, 'JSON_ELASTICSEARCH');
         } else {
             $content = $data;
         }
         // Escaping of / not necessary. Causes problems in base64 encoding of files
         $content = str_replace('\\/', '/', $content);
         if ($connection->hasCompression()) {
             // An "Accept-Encoding" header containing all supported encoding types is sent
             // Curl will decode the response automatically
             curl_setopt($conn, CURLOPT_ENCODING, '');
             // Let's precise that the request is also compressed
             curl_setopt($conn, CURLOPT_HTTPHEADER, array('Content-Encoding: gzip'));
             // Let's compress the request body,
             curl_setopt($conn, CURLOPT_POSTFIELDS, gzencode($content));
         } else {
             curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
         }
     } else {
         curl_setopt($conn, CURLOPT_POSTFIELDS, '');
     }
     curl_setopt($conn, CURLOPT_NOBODY, $httpMethod == 'HEAD');
     curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $httpMethod);
     $start = microtime(true);
     // cURL opt returntransfer leaks memory, therefore OB instead.
     ob_start();
     curl_exec($conn);
     $responseString = ob_get_clean();
     $end = microtime(true);
     // Checks if error exists
     $errorNumber = curl_errno($conn);
     $response = new Response($responseString, curl_getinfo($conn, CURLINFO_HTTP_CODE));
     $response->setQueryTime($end - $start);
     $response->setTransferInfo(curl_getinfo($conn));
     if ($connection->hasConfig('bigintConversion')) {
         $response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
     }
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     if ($errorNumber > 0) {
         throw new HttpException($errorNumber, $request, $response);
     }
     return $response;
 }
Exemplo n.º 17
0
 /**
  * Construct Exception.
  *
  * @param \Elastica\Request  $request
  * @param \Elastica\Response $response
  */
 public function __construct(Request $request, Response $response)
 {
     $this->_request = $request;
     $this->_response = $response;
     parent::__construct($response->getError());
 }
 /**
  * merge top level multi-queries and resolve returned pageIds into Title objects.
  *
  * WARNING: experimental API
  *
  * @param string $query the user query
  * @param \Elastica\Response $response Response from elasticsearch _suggest api
  * @param array $profile the suggestion profile
  * @param int $limit Maximum suggestions to return, -1 for unlimited
  * @return Title[] List of suggested titles
  */
 protected function postProcessSuggest($query, \Elastica\Response $response, $profile, $limit = -1)
 {
     $this->logContext['elasticTookMs'] = intval($response->getQueryTime() * 1000);
     $data = $response->getData();
     unset($data['_shards']);
     $suggestions = array();
     foreach ($data as $name => $results) {
         $discount = $profile[$name]['discount'];
         foreach ($results as $suggested) {
             foreach ($suggested['options'] as $suggest) {
                 $output = explode(':', $suggest['text'], 3);
                 if (sizeof($output) < 2) {
                     // Ignore broken output
                     continue;
                 }
                 $pageId = $output[0];
                 $type = $output[1];
                 $score = $discount * $suggest['score'];
                 if (!isset($suggestions[$pageId]) || $score > $suggestions[$pageId]['score']) {
                     $suggestion = array('score' => $score, 'pageId' => $pageId);
                     // If it's a title suggestion we have the text
                     if ($type === 't' && sizeof($output) == 3) {
                         $suggestion['text'] = $output[2];
                     }
                     $suggestions[$pageId] = $suggestion;
                 }
             }
         }
     }
     // simply sort by existing scores
     uasort($suggestions, function ($a, $b) {
         return $b['score'] - $a['score'];
     });
     $this->logContext['hitsTotal'] = count($suggestions);
     if ($limit > 0) {
         $suggestions = array_slice($suggestions, 0, $limit, true);
     }
     $this->logContext['hitsReturned'] = count($suggestions);
     $this->logContext['hitsOffset'] = 0;
     // we must fetch redirect data for redirect suggestions
     $missingText = array();
     foreach ($suggestions as $id => $suggestion) {
         if (!isset($suggestion['text'])) {
             $missingText[] = $id;
         }
     }
     if (!empty($missingText)) {
         // Experimental.
         //
         // Second pass query to fetch redirects.
         // It's not clear if it's the best option, this will slowdown the whole query
         // when we hit a redirect suggestion.
         // Other option would be to encode redirects as a payload resulting in a
         // very big index...
         // XXX: we support only the content index
         $type = $this->connection->getPageType($this->indexBaseName, Connection::CONTENT_INDEX_TYPE);
         // NOTE: we are already in a poolCounterWork
         // Multi get is not supported by elastica
         $redirResponse = null;
         try {
             $redirResponse = $type->request('_mget', 'GET', array('ids' => $missingText), array('_source_include' => 'redirect'));
             if ($redirResponse->isOk()) {
                 $this->logContext['elasticTook2PassMs'] = intval($redirResponse->getQueryTime() * 1000);
                 $docs = $redirResponse->getData();
                 $docs = $docs['docs'];
                 foreach ($docs as $doc) {
                     $id = $doc['_id'];
                     if (!isset($doc['_source']['redirect']) || empty($doc['_source']['redirect'])) {
                         continue;
                     }
                     $text = Util::chooseBestRedirect($query, $doc['_source']['redirect']);
                     $suggestions[$id]['text'] = $text;
                 }
             } else {
                 LoggerFactory::getInstance('CirrusSearch')->warning('Unable to fetch redirects for suggestion {query} with results {ids} : {error}', array('query' => $query, 'ids' => serialize($missingText), 'error' => $redirResponse->getError()));
             }
         } catch (\Elastica\Exception\ExceptionInterface $e) {
             LoggerFactory::getInstance('CirrusSearch')->warning('Unable to fetch redirects for suggestion {query} with results {ids} : {error}', array('query' => $query, 'ids' => serialize($missingText), 'error' => $this->extractMessage($e)));
         }
     }
     $retval = array();
     foreach ($suggestions as $suggestion) {
         if (!isset($suggestion['text'])) {
             // We were unable to find a text to display
             // Maybe a page with redirects when we built the suggester index
             // but now without redirects?
             continue;
         }
         $retval[] = array('title' => Title::makeTitle(0, $suggestion['text']), 'pageId' => $suggestion['pageId'], 'score' => $suggestion['score']);
     }
     return $retval;
 }
Exemplo n.º 19
0
 /**
  * @group unit
  */
 public function testIsNotOkBulkItemsWithStatusField()
 {
     $response = new Response(json_encode(array('took' => 213, 'items' => array(array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)), array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 301))))));
     $this->assertFalse($response->isOk());
 }
Exemplo n.º 20
0
 /**
  * Makes calls to the elasticsearch server.
  *
  * All calls that are made to the server are done through this function
  *
  * @param \Elastica\Request $request
  * @param array             $params  Host, Port, ...
  *
  * @throws \Elastica\Exception\ConnectionException
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\Connection\HttpException
  *
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent());
     $options = array('exceptions' => false);
     if ($connection->getTimeout()) {
         $options['timeout'] = $connection->getTimeout();
     }
     $proxy = $connection->getProxy();
     // See: https://github.com/facebook/hhvm/issues/4875
     if (is_null($proxy) && defined('HHVM_VERSION')) {
         $proxy = getenv('http_proxy') ?: null;
     }
     if (!is_null($proxy)) {
         $options['proxy'] = $proxy;
     }
     $req = $client->createRequest($request->getMethod(), $this->_getActionPath($request), $options);
     $req->setHeaders($connection->hasConfig('headers') ? $connection->getConfig('headers') : array());
     $data = $request->getData();
     if (!empty($data) || '0' === $data) {
         if ($req->getMethod() == Request::GET) {
             $req->setMethod(Request::POST);
         }
         if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) {
             $request->setMethod(Request::POST);
             $req->setMethod(Request::POST);
         }
         if (is_array($data)) {
             $content = JSON::stringify($data, 'JSON_ELASTICSEARCH');
         } else {
             $content = $data;
         }
         $req->setBody(Stream::factory($content));
     }
     try {
         $start = microtime(true);
         $res = $client->send($req);
         $end = microtime(true);
     } catch (TransferException $ex) {
         throw new GuzzleException($ex, $request, new Response($ex->getMessage()));
     }
     $response = new Response((string) $res->getBody(), $res->getStatusCode());
     $response->setQueryTime($end - $start);
     $response->setTransferInfo(array('request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode()));
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
 /**
  * @param \Elastica\Response        $response
  * @param \Elastica\Bulk\Response[] $bulkResponses
  */
 public function __construct(BaseResponse $response, array $bulkResponses)
 {
     parent::__construct($response->getData());
     $this->_bulkResponses = $bulkResponses;
 }
Exemplo n.º 22
0
 protected function requestAuthority(Elastica\Response $response = NULL)
 {
     if ($response) {
         $info = $response->getTransferInfo();
         $url = new Nette\Http\Url($info['url']);
     } else {
         $url = new Nette\Http\Url(key($this->queries) ?: 'http://localhost:9200/');
     }
     return $url->hostUrl;
 }
Exemplo n.º 23
0
 /**
  * Makes calls to the elasticsearch server
  *
  * All calls that are made to the server are done through this function
  *
  * @param  \Elastica\Request                     $request
  * @param  array                                $params  Host, Port, ...
  * @throws \Elastica\Exception\ConnectionException
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\Connection\HttpException
  * @return \Elastica\Response                    Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $conn = $this->_getConnection($connection->isPersistent());
     // If url is set, url is taken. Otherwise port, host and path
     $url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
     if (!empty($url)) {
         $baseUri = $url;
     } else {
         $baseUri = $this->_scheme . '://' . $connection->getHost() . ':' . $connection->getPort() . '/' . $connection->getPath();
     }
     $baseUri .= $request->getPath();
     $query = $request->getQuery();
     if (!empty($query)) {
         $baseUri .= '?' . http_build_query($query);
     }
     curl_setopt($conn, CURLOPT_URL, $baseUri);
     curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
     curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod());
     curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
     $this->_setupCurl($conn);
     $headersConfig = $connection->hasConfig('headers') ? $connection->getConfig('headers') : array();
     if (!empty($headersConfig)) {
         $headers = array();
         while (list($header, $headerValue) = each($headersConfig)) {
             array_push($headers, $header . ': ' . $headerValue);
         }
         curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
     }
     // TODO: REFACTOR
     $data = $request->getData();
     if (isset($data) && !empty($data)) {
         if (is_array($data)) {
             $content = json_encode($data);
         } else {
             $content = $data;
         }
         // Escaping of / not necessary. Causes problems in base64 encoding of files
         $content = str_replace('\\/', '/', $content);
         curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
     }
     $start = microtime(true);
     // cURL opt returntransfer leaks memory, therefore OB instead.
     ob_start();
     curl_exec($conn);
     $responseString = ob_get_clean();
     $end = microtime(true);
     // Checks if error exists
     $errorNumber = curl_errno($conn);
     $response = new Response($responseString);
     if (defined('DEBUG') && DEBUG) {
         $response->setQueryTime($end - $start);
         $response->setTransferInfo(curl_getinfo($conn));
     }
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($errorNumber > 0) {
         throw new HttpException($errorNumber, $request, $response);
     }
     return $response;
 }
Exemplo n.º 24
0
 /**
  * @param \Elastica\Response $response
  *
  * @throws \Elastica\Exception\Bulk\ResponseException
  * @throws \Elastica\Exception\InvalidException
  *
  * @return \Elastica\Bulk\ResponseSet
  */
 protected function _processResponse(Response $response)
 {
     $responseData = $response->getData();
     $actions = $this->getActions();
     $bulkResponses = array();
     if (isset($responseData['items']) && is_array($responseData['items'])) {
         foreach ($responseData['items'] as $key => $item) {
             if (!isset($actions[$key])) {
                 throw new InvalidException('No response found for action #' . $key);
             }
             $action = $actions[$key];
             $opType = key($item);
             $bulkResponseData = reset($item);
             if ($action instanceof AbstractDocumentAction) {
                 $data = $action->getData();
                 if ($data instanceof Document && $data->isAutoPopulate() || $this->_client->getConfigValue(array('document', 'autoPopulate'), false)) {
                     if (!$data->hasId() && isset($bulkResponseData['_id'])) {
                         $data->setId($bulkResponseData['_id']);
                     }
                     if (isset($bulkResponseData['_version'])) {
                         $data->setVersion($bulkResponseData['_version']);
                     }
                 }
             }
             $bulkResponses[] = new BulkResponse($bulkResponseData, $action, $opType);
         }
     }
     $bulkResponseSet = new ResponseSet($response, $bulkResponses);
     if ($bulkResponseSet->hasError()) {
         throw new BulkResponseException($bulkResponseSet);
     }
     return $bulkResponseSet;
 }
Exemplo n.º 25
0
 /**
  * @param array|string $responseData
  * @param \Elastica\Bulk\Action $action
  * @param string $opType
  */
 public function __construct($responseData, Action $action, $opType)
 {
     parent::__construct($responseData);
     $this->_action = $action;
     $this->_opType = $opType;
 }
Exemplo n.º 26
0
 /**
  * Makes calls to the elasticsearch server
  *
  * @param \Elastica\Request $request
  * @param  array             $params Host, Port, ...
  * @throws \Elastica\Exception\Connection\ThriftException
  * @throws \Elastica\Exception\ResponseException
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $sendTimeout = $connection->hasConfig('sendTimeout') ? $connection->getConfig('sendTimeout') : null;
     $recvTimeout = $connection->hasConfig('recvTimeout') ? $connection->getConfig('recvTimeout') : null;
     $framedTransport = $connection->hasConfig('framedTransport') ? (bool) $connection->getConfig('framedTransport') : false;
     try {
         $client = $this->_getClient($connection->getHost(), $connection->getPort(), $sendTimeout, $recvTimeout, $framedTransport);
         $restRequest = new RestRequest();
         $restRequest->method = array_search($request->getMethod(), Method::$__names);
         $restRequest->uri = $request->getPath();
         $query = $request->getQuery();
         if (!empty($query)) {
             $restRequest->parameters = $query;
         }
         $data = $request->getData();
         if (!empty($data)) {
             if (is_array($data)) {
                 $content = JSON::stringify($data);
             } else {
                 $content = $data;
             }
             $restRequest->body = $content;
         }
         /* @var $result RestResponse */
         $start = microtime(true);
         $result = $client->execute($restRequest);
         $response = new Response($result->body);
         $end = microtime(true);
     } catch (TException $e) {
         $response = new Response('');
         throw new ThriftException($e, $request, $response);
     }
     if (defined('DEBUG') && DEBUG) {
         $response->setQueryTime($end - $start);
     }
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
 /**
  * Return last created document id from ES response
  * 
  * @param Response $response
  *        	Elastica Response object
  * @return string|null
  */
 protected function getCreatedDocId(Response $response)
 {
     $data = $response->getData();
     if (!empty($data['items'][0]['create']['_id'])) {
         return $data['items'][0]['create']['_id'];
     }
 }