Skip to content

Instantly share code, notes, and snippets.

@fernandodebrando
Created September 20, 2016 21:10
Show Gist options
  • Save fernandodebrando/030445cc05b862086ca706150f049e13 to your computer and use it in GitHub Desktop.
Save fernandodebrando/030445cc05b862086ca706150f049e13 to your computer and use it in GitHub Desktop.
PHP e ElasticSearch
{
"require": {
"elasticsearch/elasticsearch": "~2.0"
}
}
<?php
require 'vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();
//Teste de conexão
if ($client) {
echo 'connected';
}
//Criando o primeiro índice
/*$params = [
'index' => 'meu_indice',
'type' => 'meu_tipo',
'id' => 'meu_id2',
'body' => [
'primeiro campo' => 'Adicionando meu primeiro campo no Elasticsearch'
],
];
$response = $client->index($params);
echo $response['created'];
*/
//Obtendo dados
/*$params = [
'index' => 'meu_indice',
'type' => 'meu_tipo',
'id' => 'meu_id2',
];
$response = $client->get($params);
echo $response['_source']['primeiro campo'];*/
//print_r($response);
//Pesquisando dados
/*$params = [
'index' => 'meu_indice',
'type' => 'meu_tipo',
'body' => [
'query' => [
'match' => [
'primeiro campo' => 'meu primeiro campo'
],
],
],
];
$response = $client->search($params);
$hits = count($response['hits']['hits']);
$result = null;
$i = 0;
//print_r($response);die;
while ($i < $hits) {
$result[$i] = $response['hits']['hits'][$i]['_source'];
$i++;
}
foreach ($result as $key => $value) {
echo $value['primeiro campo'] . "<br>";
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment