本文整理汇总了PHP中Elastica\Client::request方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::request方法的具体用法?PHP Client::request怎么用?PHP Client::request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elastica\Client
的用法示例。
在下文中一共展示了Client::request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: iterate
/**
* 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();
}
}
示例2: execute
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: find
/**
* 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;
}
示例4: requestByType
/**
* 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']);
}
示例5: scanAvailablePlugins
/**
* @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: testSearchWithSynonyms
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: send
/**
* @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);
}
示例8: getIndexHealth
/**
* 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();
}
示例9: request
/**
* {@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: request
/**
* {@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: request
/**
* @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: request
/**
* @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: _getIndices
/**
* 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: request
/**
* @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;
}
示例15: getHealth
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();
}
}