Created
September 20, 2016 21:10
-
-
Save fernandodebrando/030445cc05b862086ca706150f049e13 to your computer and use it in GitHub Desktop.
PHP e ElasticSearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"elasticsearch/elasticsearch": "~2.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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