elasticsearch-loop-php
for Looping data from Elasticsearch, (*1)
Usage
Initial setup
-
Install composer. curl -s http://getcomposer.org/installer | php, (*2)
-
Create composer.json containing:, (*3)
{
"require" : {
"salamtam/elasticsearch-loop-php" : "^1.4"
}
}
-
Run ./composer.phar install, (*4)
-
Keep up-to-date: ./composer.phar update, (*5)
Example 1
Query with URL, (*6)
require_once __DIR__ . "/vendor/autoload.php";
use ElasticsearchLoopPHP\ElasticsearchLoop;
/* set connection elasticsearch */
$url = "localhost:9200";
/* set index name and type */
$params = [
'index' => 'index_name',
'type' => 'index_type',
'body' => [
'query' => [
'match_all' => []
]
]
];
/* callback function */
$callback = function($response) {
foreach ($response['hits']['hits'] as $item) {
print_r($item);
}
};
/* set query with url */
$client = new ElasticsearchLoop($url);
$client->getElasticsearch($params, $callback);
Example 2
Query with basic authentication, (*7)
require_once __DIR__ . "/vendor/autoload.php";
use ElasticsearchLoopPHP\ElasticsearchLoop;
/* set connection elasticsearch */
$user = "root";
$pass = "";
$host = "localhost";
$port = "9200";
/* set index name and type */
$params = [
'index' => 'index_name',
'type' => 'index_type',
'body' => [
'query' => [
'match_all' => []
]
]
];
/* callback function */
$callback = function($response) {
foreach ($response['hits']['hits'] as $item) {
print_r($item);
}
};
/* set query with host, user, pass and port */
$client = new ElasticsearchLoop($host, $user, $pass, $port);
$client->getElasticsearch($params, $callback);