Created
April 4, 2012 12:00
-
-
Save kennym/2300615 to your computer and use it in GitHub Desktop.
Elastic Search indexer
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 | |
/* | |
1. Connect to database. | |
2. Fetch 100 records from database. | |
3. Make curl PUT request to Elastic Search server | |
*/ | |
$DB_NAME = "abc"; | |
$DB_SCHEMA = ""; | |
$DB_TABLE = ""; | |
$DB_HOST = ""; | |
$USER = ""; | |
$PASSWORD = ""; | |
$URL = "http://localhost:9200/a/"; | |
$connect_str = "dbname=".$DB_NAME." host=$DB_HOST"." user=".$USER." password=".$PASSWORD; | |
$dbconn = pg_connect($connect_str); | |
$result = pg_query($dbconn, | |
"SELECT articulo, cuerpo FROM $DB_SCHEMA.$DB_TABLE LIMIT 100"); | |
while ($row = pg_fetch_row($result)) { | |
/* Generate an array for pushing to search index. */ | |
$articulo = $row[0]; | |
$cuerpo = $row[1]; | |
$a = array('cuerpo' => "f"); | |
/* Do a cURL XPUT request to search index */ | |
$ch = curl_init(); | |
echo $articulo; | |
$URL = $URL."a/$articulo "; | |
curl_setopt($ch, CURLOPT_URL, $URL); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($a)); | |
$response = curl_exec($ch); | |
if (!$response) { | |
print curl_error($ch); | |
die("Oops.. Kenny died"); | |
} | |
if (curl_error($ch)) { | |
print curl_error($ch); | |
} | |
if ($response["error"]) { | |
echo "<p>Ooops..."; | |
echo $response; | |
echo var_dump($a); | |
} else { | |
echo "<p>Wohooo"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment