Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created January 22, 2018 14:10
Show Gist options
  • Save betweenbrain/fb6075d9ae5f8af9a5adc8442925bd72 to your computer and use it in GitHub Desktop.
Save betweenbrain/fb6075d9ae5f8af9a5adc8442925bd72 to your computer and use it in GitHub Desktop.
Joomla Elasticsearch CLI
<?php
/**
* @package ${NAMESPACE}
* @subpackage
*
* @copyright A copyright
* @license A "Slug" license name e.g. GPL2
*/
// CLI execution only
if (PHP_SAPI !== 'cli') {
die();
}
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__DIR__));
if (file_exists(JPATH_BASE . '/includes/defines.php')) {
include_once JPATH_BASE . '/includes/defines.php';
}
if (file_exists(JPATH_BASE . '/includes/framework.php')) {
require_once JPATH_BASE . '/includes/framework.php';
}
jimport('elasticsearch.autoload');
class ElasticCli extends JApplicationCli
{
/**
* ElasticCli constructor.
*/
public function __construct()
{
parent::__construct();
$hosts = [
'http://hfdvshccapp1.vm.itg.corp.us.shldcorp.com:9200',
'http://hfqashccsrch1.vm.itg.corp.us.shldcorp.com:9200',
'http://hfqashccsrch2.vm.itg.corp.us.shldcorp.com:9200'
];
$clientBuilder = new \Elasticsearch\ClientBuilder();
$this->client = $clientBuilder::create()// Instantiate a new ClientBuilder
->setHosts($hosts)// Set the hosts
->setRetries(3)// Set connection retries
->build();
}
/**
* Entry point for CLI script
*
* @return void
*
* @since 3.0
*/
public function execute()
{
try {
$params = [
'index' => 'documents',
'body' => [
'query' => [
'match_all' => []
]
],
'size' => 1
];
$results = $this->client->search($params);
$this->out('Results ' . count($results['hits']['hits']));
$this->out('Total ' . $results['hits']['total']);
foreach ($results['hits']['hits'] as $key => $value) {
$this->out('Id: ' . $value['_id']);
$params = [
'index' => $value['_index'],
'type' => $value['_type'],
'id' => $value['_id']
];
// Delete doc at /index/type/id
$response = $this->client->delete($params);
$this->out(print_r($response, true));
}
} catch (\Elasticsearch\Common\Exceptions\TransportException $e) {
$previous = $e->getPrevious();
if ($previous instanceof \Elasticsearch\Common\Exceptions\MaxRetriesException) {
echo "Max retries!";
}
}
}
/**
* @param $term
*
*
* @since version
*/
private function search($term)
{
try {
$params = [
'index' => 'documents',
'body' => [
'query' => [
'multi_match' => [
'query' => $term,
'fields' => [
'introtext',
'fulltext',
'metakey'
]
]
]
],
'size' => 10,
'from' => 0
];
$results = $this->client->search($params);
$this->out('Results ' . count($results['hits']['hits']));
$this->out('Total ' . $results['hits']['total']);
// Returned data $results['hits']['hits']
foreach ($results['hits']['hits'] as $key => $value) {
$this->out('Id: ' . $value['_source']['id']);
}
} catch (\Elasticsearch\Common\Exceptions\TransportException $e) {
$previous = $e->getPrevious();
if ($previous instanceof \Elasticsearch\Common\Exceptions\MaxRetriesException) {
echo "Max retries!";
}
}
}
/**
* @param $id
*
*
* @since version
*/
private function getDoc($doc)
{
$params = [
'index' => $doc['_index'],
'type' => $doc['_type'],
'id' => $doc['_id']
];
$response = $this->client->get($params);
$this->out(print_r($response, true));
}
private function setDoc($doc)
{
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
// Document will be indexed to my_index/my_type/my_id
$response = $this->client->index($params);
}
/**
* Retrieves value from the Joomla configuration.
*
* @param string $varname Name of the configuration property
* @param mixed $default Default value
*
* @return mixed
*/
public function getCfg($varname, $default = null)
{
return JFactory::getConfig()->get($varname, $default);
}
/**
* Get the current template name.
* Always return 'system' as template.
*
* @return string
*/
public function getTemplate()
{
return 'system';
}
/**
* Get the current application name.
* Always returns 'cli'.
*
* @return string
*/
public function getName()
{
return 'cli';
}
/**
* Checks if interface is site or not.
*
* @return bool
*/
public function isSite()
{
return $this->isClient('site');
}
/**
* Checks if interface is admin or not.
*
* @return bool
*/
public function isAdmin()
{
return $this->isClient('administrator');
}
/**
* Check the client interface by name.
*
* @param string $identifier String identifier for the application interface
*
* @return boolean True if this application is of the given type client interface.
*
* @since 3.7.0
*/
public function isClient($identifier)
{
return $this->getName() === $identifier;
}
}
JApplicationCli::getInstance('ElasticCli')->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment