request() публичный Метод

It's possible to make any REST query directly over this method
public request ( string $path, string $method = Request::GET, array | string $data = [], array $query = [] ) : Response
$path string Path to call
$method string Rest method to use (GET, POST, DELETE, PUT)
$data array | string OPTIONAL Arguments as array or pre-encoded string
$query array OPTIONAL Query params
Результат Response Response object
Пример #1
1
 /**
  * Iterate on index documents and perform $closure
  * Iteration uses ElasticSearch scroll scan methods
  * Note: Using setLimit(N) and setFrom(N) for query does not affect actual limit and offset (Limited by ES scan/scroll functionality, see Docs in link)
  *
  * See docs about $scroll in link:
  * @link http://www.elasticsearch.org/guide/reference/api/search/scroll.html
  *
  * @param Query|AbstractQuery $query
  * @param \Closure $closure Receives arguments: function(DataProviderDocument $doc, $i, $total); Return TRUE in $closure if you want to break and stop iteration
  * @param int $batchSize
  * @param string $scroll
  */
 public function iterate(Query $query, \Closure $closure, $batchSize = 100, $scroll = '5m')
 {
     $response = $this->index->request('_search', 'GET', $query->toArray(), array('search_type' => 'scan', 'scroll' => $scroll, 'size' => $batchSize, 'limit' => 1));
     $data = $response->getData();
     $scrollId = $data['_scroll_id'];
     $total = $data['hits']['total'];
     $i = 0;
     $response = $this->client->request('_search/scroll', 'GET', $scrollId, array('scroll' => $scroll));
     $data = $response->getData();
     while (count($data['hits']['hits']) > 0) {
         foreach ($data['hits']['hits'] as $item) {
             $itemData = $item['_source'];
             $doc = new DataProviderDocument($item['_id'], $item['_type'], $itemData);
             if ($break = $closure($doc, $i, $total)) {
                 break 2;
             }
             $i++;
         }
         $scrollId = $data['_scroll_id'];
         $response = $this->client->request('_search/scroll', 'GET', $scrollId, array('scroll' => $scroll));
         $data = $response->getData();
     }
 }
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $table = new Console\Helper\TableHelper();
     $output->writeln('Cluster overview:');
     $output->writeln('');
     $output->writeln('Nodes:');
     $cluster = $this->elastica->getCluster();
     $table->setHeaders(['name', 'documents', 'node', 'ip', 'port', 'hostname', 'version', 'transport address', 'http address']);
     $nodes = $cluster->getNodes();
     foreach ($nodes as $node) {
         $name = $node->getName();
         $ip = $node->getInfo()->getIp();
         $data = $node->getInfo()->getData();
         $port = $node->getInfo()->getPort();
         $stats = $node->getStats()->get();
         //dump($stats->get());exit;
         $table->addRow([$data['name'], $stats['indices']['docs']['count'], $name, $ip, $port, $data['hostname'], $data['version'], $data['transport_address'], $data['http_address']]);
     }
     $table->render($output);
     $table->setRows([]);
     /* INFO */
     $info = $this->elastica->request('', 'GET')->getData();
     $table->setHeaders(['name', 'version', 'status', 'ok']);
     $table->addRow([$info['name'], $info['version']['number'], $info['status'], $info['ok']]);
     $table->render($output);
     $table->setRows([]);
     $output->writeln('');
 }
Пример #3
0
 /**
  * Find a document by id.
  *
  * @param $id
  * @param $type
  * @param $index
  *
  * @return bool|Document
  */
 public function find($id, $type, $index = 'activehire')
 {
     $path = $index . '/' . $type . '/' . $id;
     $response = $this->client->request($path);
     if ($response->isOk()) {
         $responseData = $response->getData();
         return $this->createDocument($responseData['_source'], $type, $index);
     }
     return false;
 }
 /**
  * Making a request by giving in raw json as the query
  * @param  jsonstring  $query       Raw json query
  * @return array                    An array containing the mapped entities.
  */
 public function requestByType($query, $type = 'organisation,vacancy,person', $requestType = Request::GET)
 {
     $client = new Client(array('host' => $this->es_host, 'port' => $this->es_port));
     $path = $this->getIndex() . '/' . $type . '/_search';
     $response = $client->request($path, $requestType, $query)->getData();
     return $this->esMapper->getEntities($response['hits']['hits']);
 }
 /**
  * @param array $bannedPlugins
  * @return array
  */
 public function scanAvailablePlugins(array $bannedPlugins = array())
 {
     $this->outputIndented("Scanning available plugins...");
     $result = $this->client->request('_nodes');
     $result = $result->getData();
     $availablePlugins = array();
     $first = true;
     foreach (array_values($result['nodes']) as $node) {
         $plugins = array();
         foreach ($node['plugins'] as $plugin) {
             $plugins[] = $plugin['name'];
         }
         if ($first) {
             $availablePlugins = $plugins;
             $first = false;
         } else {
             $availablePlugins = array_intersect($availablePlugins, $plugins);
         }
     }
     if (count($availablePlugins) === 0) {
         $this->output('none');
     }
     $this->output("\n");
     if (count($bannedPlugins)) {
         $availablePlugins = array_diff($availablePlugins, $bannedPlugins);
     }
     foreach (array_chunk($availablePlugins, 5) as $pluginChunk) {
         $plugins = implode(', ', $pluginChunk);
         $this->outputIndented("\t{$plugins}\n");
     }
     return $availablePlugins;
 }
Пример #6
0
 public function testSearchWithSynonyms()
 {
     $indexSettings = json_decode(file_get_contents(__DIR__ . '/../_data/productIndex.json'), true);
     // custom synonyms
     $synonyms = [];
     $synonyms[] = "tai nghe,headphone";
     $synonyms[] = "chụp hình,chụp ảnh";
     $indexSettings['settings']['index']['analysis']['filter']['name_synonym_filter']['synonyms'] = $synonyms;
     $path = '/test/product/';
     // create index
     $this->client->request($path, Request::PUT, $indexSettings);
     // insert data test
     $productData = json_decode($this->sampleProductJson, true);
     $productNames = ["tai nghe abc", "headphone xyz", "headphone tai nghe", "tai abc nghe", "wrong name", "gậy chụp hình abc", "gậy chụp ảnh xyz", "chụp hình cho trẻ", "công việc chụp ảnh", "ảnh hình"];
     $i = 1;
     foreach ($productNames as $name) {
         $productData['searchable_name'] = $name;
         $this->client->request($path . $i++, Request::PUT, ['searchable_name' => $name, 'type' => 'sample', 'attribute' => 'any attribtue', 'value' => rand(1, 999)]);
     }
     // wait for index
     sleep(3);
     foreach ($synonyms as $syn) {
         $keywords = explode(',', $syn);
         // with unmarked field is using synonyms and not unmarked field not use
         $dataForFirstKeyword = $this->client->request($path . '_search', Request::GET, ['query' => ['match_phrase' => ['searchable_name.unmarked' => $keywords[0]]]])->getData();
         $dataForFirstKeywordNotUnmarked = $this->client->request($path . '_search', Request::GET, ['query' => ['match_phrase' => ['searchable_name' => $keywords[0]]]])->getData();
         // responses of search no synonym and search with synonyms are different
         $this->assertNotEquals($dataForFirstKeyword['hits'], $dataForFirstKeywordNotUnmarked['hits']);
         $dataForSecondKeyword = $this->client->request($path . '_search', Request::GET, ['query' => ['match_phrase' => ['searchable_name.unmarked' => $keywords[1]]]])->getData();
         // test responses of 2 synonym keywords are same
         $this->assertEquals($dataForFirstKeyword['hits'], $dataForSecondKeyword['hits']);
     }
 }
Пример #7
0
 /**
  * @return \Elastica\Bulk\ResponseSet
  */
 public function send()
 {
     $path = $this->getPath();
     $data = $this->toString();
     $response = $this->_client->request($path, Request::POST, $data, $this->_requestParams);
     return $this->_processResponse($response);
 }
 /**
  * Get index health
  * @param string $indexName
  * @return array the index health status
  */
 public function getIndexHealth($indexName)
 {
     $path = "_cluster/health/{$indexName}";
     $response = $this->client->request($path);
     if ($response->hasError()) {
         throw new \Exception("Error while fetching index health status: " . $response->getError());
     }
     return $response->getData();
 }
 /**
  * {@inheritdoc}
  */
 public function request($path, $method = Request::GET, $data = [], array $query = [])
 {
     $start = microtime(true);
     $response = parent::request($path, $method, $data, $query);
     $time = microtime(true) - $start;
     if (!defined('DEBUG') || false === DEBUG) {
         $response->setQueryTime($time);
     }
     $this->logQuery($path, $method, $data, $query, $time);
     return $response;
 }
Пример #10
0
 /**
  * {@inheritdoc}
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     $start = microtime(true);
     $response = parent::request($path, $method, $data, $query);
     if ($this->_logger and $this->_logger instanceof ElasticaLogger) {
         $time = microtime(true) - $start;
         $connection = $this->getLastRequest()->getConnection();
         $connection_array = array('host' => $connection->getHost(), 'port' => $connection->getPort(), 'transport' => $connection->getTransport(), 'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : array());
         $this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query);
     }
     return $response;
 }
Пример #11
0
 /**
  * @param string $path
  * @param string $method
  * @param array $data
  * @param array $query
  * @throws \Exception
  * @return Elastica\Response
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     $begin = microtime(TRUE);
     try {
         $response = parent::request($path, $method, $data, $query);
         $this->onSuccess($this, $this->_lastRequest, $response, microtime(TRUE) - $begin);
         return $response;
     } catch (\Exception $e) {
         $this->onError($this, $this->_lastRequest, $e, microtime(TRUE) - $begin);
         throw $e;
     }
 }
Пример #12
0
 /**
  * @param string $path
  * @param string $method
  * @param array  $data
  * @param array  $query
  *
  * @return \Elastica\Response
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     $start = microtime(true);
     $response = parent::request($path, $method, $data, $query);
     $responseData = $response->getData();
     if (isset($responseData['took']) && isset($responseData['hits'])) {
         $this->logQuery($path, $method, $data, $query, $start, $response->getEngineTime(), $responseData['hits']['total']);
     } else {
         $this->logQuery($path, $method, $data, $query, $start, 0, 0);
     }
     return $response;
 }
Пример #13
0
 /**
  * Retrieves the indices available from ElasticSearch
  */
 protected function _getIndices()
 {
     if (null === static::$_indices) {
         $_indices = [];
         try {
             $_response = $this->_client->request('_aliases?pretty=1');
             foreach ($_response->getData() as $_index => $_aliases) {
                 //  No recent index
                 if (false === stripos($_index, '_recent') && '.kibana' !== $_index) {
                     $_indices[] = $_index;
                 }
             }
             if (!empty($_indices)) {
                 static::$_indices = $_indices;
             }
         } catch (\Exception $_ex) {
             Log::error($_ex);
             throw $_ex;
         }
     }
     return static::$_indices;
 }
Пример #14
0
 /**
  * @param string $path
  * @param string $method
  * @param array  $data
  * @param array  $query
  *
  * @return \Elastica\Response
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     if ($this->stopwatch) {
         $this->stopwatch->start('es_request', 'fos_elastica');
     }
     $start = microtime(true);
     $response = parent::request($path, $method, $data, $query);
     $this->logQuery($path, $method, $data, $query, $start);
     if ($this->stopwatch) {
         $this->stopwatch->stop('es_request');
     }
     return $response;
 }
 private function getHealth()
 {
     while (true) {
         $indexName = $this->specificIndexName;
         $path = "_cluster/health/{$indexName}";
         $response = $this->client->request($path);
         if ($response->hasError()) {
             $this->error('Error fetching index health but going to retry.  Message: ' . $response->getError());
             sleep(1);
             continue;
         }
         return $response->getData();
     }
 }
Пример #16
0
 public function testWithNoValidConnection()
 {
     $connections = array(new \Elastica\Connection(array('host' => '255.255.255.0', 'timeout' => 2)), new \Elastica\Connection(array('host' => '45.45.45.45', 'port' => '80', 'timeout' => 2)), new \Elastica\Connection(array('host' => '10.123.213.123', 'timeout' => 2)));
     $count = 0;
     $client = new Client(array(), function () use(&$count) {
         ++$count;
     });
     $client->setConnections($connections);
     try {
         $client->request('/_aliases');
         $this->fail('Should throw exception as no connection valid');
     } catch (\Elastica\Exception\ConnectionException $e) {
         $this->assertEquals(count($connections), $count);
     }
 }
Пример #17
0
 /**
  * @param string $path
  * @param string $method
  * @param array  $data
  * @param array  $query
  *
  * @return \Elastica\Response
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     if ($this->stopwatch) {
         $this->stopwatch->start('es_request', 'fos_elastica');
     }
     $response = parent::request($path, $method, $data, $query);
     $responseData = $response->getData();
     if (isset($responseData['took']) && isset($responseData['hits'])) {
         $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']);
     } else {
         $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0);
     }
     if ($this->stopwatch) {
         $this->stopwatch->stop('es_request');
     }
     return $response;
 }
 /**
  * @param array $cities
  * @return \Elastica\Response
  */
 public function run(array $cities)
 {
     $client = new Client();
     //Create a new index
     $index = $client->getIndex(self::INDEX_NAME);
     $index->create(array(), true);
     //Set type
     $type = $index->getType(self::TYPE_NAME);
     //Add Document
     $documents = array();
     foreach ($cities as $city) {
         // Fetching content from the database
         $documents[] = new Document($city['ID'], array('name' => $city['Name']));
     }
     $type->addDocuments($documents);
     $index->refresh();
     //Example simple query
     $query = '{"query":{"query_string":{"query":"Eindhoven"}}}';
     $path = $index->getName() . '/' . $type->getName() . '/_search';
     $response = $client->request($path, Request::GET, $query);
     return $response;
 }
Пример #19
0
 /**
  * {@inheritdoc}
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     $exceptionOccured = false;
     if (isset($this->stopwatch)) {
         $this->stopwatch->start('mpd_elastica', 'mapado_elastica_query');
     }
     try {
         $response = parent::request($path, $method, $data, $query);
     } catch (ResponseException $e) {
         $response = $e->getResponse();
         $exceptionOccured = true;
     }
     if (isset($this->dataCollector)) {
         $this->dataCollector->addQuery(['path' => $path, 'method' => $method, 'data' => $data, 'query' => $query], $response);
     }
     if (isset($this->stopwatch)) {
         $this->stopwatch->stop('mpd_elastica');
     }
     if ($exceptionOccured) {
         throw $e;
     }
     return $response;
 }
Пример #20
0
 /**
  * Perform a snapshot request.
  *
  * @param string $path   the URL
  * @param string $method the HTTP method
  * @param array  $data   request body data
  * @param array  $query  query string parameters
  *
  * @return Response
  */
 public function request($path, $method = Request::GET, $data = [], array $query = [])
 {
     return $this->_client->request('/_snapshot/' . $path, $method, $data, $query);
 }
 /**
  * Retrieve document by id from Elasticsearch
  * 
  * @param Client $client
  *        	Elastica client
  * @param string $index        	
  * @param string $type        	
  * @param string $documentId        	
  * @return array
  */
 protected function getDocSourceFromElastic(Client $client, $index, $type, $documentId)
 {
     $resp = $client->request("/{$index}/{$type}/{$documentId}", Request::GET);
     $data = $resp->getData();
     if (!empty($data['_source'])) {
         return $data['_source'];
     }
     return array();
 }
 /**
  * Delete an index
  *
  * @param Client $client
  * @param string $indexName Index name to delete
  */
 private function deleteIndex(Client $client, $indexName)
 {
     $path = sprintf("%s", $indexName);
     $client->request($path, Request::DELETE);
 }
Пример #23
0
 /**
  * Shuts this node down.
  *
  * @param string $delay OPTIONAL Delay after which node is shut down (default = 1s)
  *
  * @return \Elastica\Response
  *
  * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-shutdown.html
  */
 public function shutdown($delay = '1s')
 {
     $path = '_cluster/nodes/' . $this->getId() . '/_shutdown?delay=' . $delay;
     return $this->_client->request($path, Request::POST);
 }
 /**
  * Create index template
  * @param Client $client
  * @param string $template
  */
 protected function createTemplate(Client $client, $template)
 {
     $config = $this->getConfig();
     $settings = array('template' => $template . '-*', 'order' => 1, 'settings' => array('index.number_of_shards' => $config('elastic.template.shards', 5), 'index.number_of_replicas' => $config('elastic.template.replicas', 1), 'index.mapper.dynamic' => true, 'index.analysis.analyzer.default.type' => 'standard'), 'mappings' => array('_default_' => array('_all' => array('enabled' => false), 'include_in_all' => false, 'dynamic_templates' => array(array('string_fields' => array('match_mapping_type' => 'string', 'match' => '*', 'mapping' => array('type' => 'multi_field', 'fields' => array('raw' => array('index' => 'not_analyzed', 'type' => 'string'), '{name}' => array('index' => 'analyzed', 'omit_norms' => true, 'type' => 'string'))))))), 'CspReport' => array('properties' => array('statusCode' => array('type' => 'integer'), 'lineNumber' => array('type' => 'integer'), 'columnNumber' => array('type' => 'integer')))));
     $client->request("/_template/{$template}", Request::PUT, $settings);
 }
 public function testJSONQuery()
 {
     $client = new Client();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     $type->addDocument(new Document(1, array('username' => 'ruflin')));
     $index->refresh();
     $query = '{"query":{"query_string":{"query":"ruflin"}}}';
     $path = $index->getName() . '/' . $type->getName() . '/_search';
     $response = $client->request($path, Request::GET, $query);
     $responseArray = $response->getData();
     $this->assertEquals(1, $responseArray['hits']['total']);
 }
Пример #26
0
 /**
  * Returns the name of a single index that an alias points to or throws
  * an exception if there is more than one.
  *
  * @param Client $client
  * @param string $aliasName Alias name
  *
  * @return string|null
  *
  * @throws AliasIsIndexException
  */
 private function getAliasedIndex(Client $client, $aliasName)
 {
     $aliasesInfo = $client->request('_aliases', 'GET')->getData();
     $aliasedIndexes = array();
     foreach ($aliasesInfo as $indexName => $indexInfo) {
         if ($indexName === $aliasName) {
             throw new AliasIsIndexException($indexName);
         }
         if (!isset($indexInfo['aliases'])) {
             continue;
         }
         $aliases = array_keys($indexInfo['aliases']);
         if (in_array($aliasName, $aliases)) {
             $aliasedIndexes[] = $indexName;
         }
     }
     if (count($aliasedIndexes) > 1) {
         throw new \RuntimeException(sprintf('Alias %s is used for multiple indexes: [%s]. Make sure it\'s' . 'either not used or is assigned to one index only', $aliasName, implode(', ', $aliasedIndexes)));
     }
     return array_shift($aliasedIndexes);
 }
Пример #27
0
 /**
  * Perform a template request
  * @param string $path the URL
  * @param string $method the HTTP method
  * @param array $data request body data
  * @param array $query query string parameters
  * @return Response
  */
 public function request($path = null, $method = Request::GET, $data = array(), array $query = array())
 {
     return $this->_client->request("/_template/" . $path, $method, $data, $query);
 }
 public function testWithoutProxy()
 {
     $client = new \Elastica\Client(array('transport' => 'Guzzle'));
     $client->getConnection()->setProxy('');
     $transferInfo = $client->request('/_nodes')->getTransferInfo();
     $this->assertEquals(200, $transferInfo['http_code']);
 }
Пример #29
0
 /**
  * Perform a snapshot request
  * @param  string   $path   the URL
  * @param  string   $method the HTTP method
  * @param  array    $data   request body data
  * @param  array    $query  query string parameters
  * @return Response
  */
 public function request($path, $method = Request::GET, $data = array(), array $query = array())
 {
     return $this->_client->request("/_snapshot/" . $path, $method, $data, $query);
 }
Пример #30
0
 public function testLastRequestResponse()
 {
     $client = new Client(array('log' => '/tmp/php.log'));
     $response = $client->request('_status');
     $this->assertInstanceOf('Elastica\\Response', $response);
     $lastRequest = $client->getLastRequest();
     $this->assertInstanceOf('Elastica\\Request', $lastRequest);
     $this->assertEquals('_status', $lastRequest->getPath());
     $lastResponse = $client->getLastResponse();
     $this->assertInstanceOf('Elastica\\Response', $lastResponse);
     $this->assertSame($response, $lastResponse);
 }