Created
April 7, 2016 00:54
-
-
Save JacobHsu/8d334082172038f15709bb47475f165f to your computer and use it in GitHub Desktop.
#Elasticsearch #patch #PHP
This file contains hidden or 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 | |
/* | |
* search app 建立資料 | |
* 說明: | |
* | |
* 執行 `patch/search_app_add.php` | |
* php patch/search_app_add.php | |
* | |
* 清除 | |
* php search_app_add.php clear | |
* | |
* 執行patch | |
* php search_app_add.php patch | |
* | |
* 檢查 | |
* http://localhost:9200/$search_type/_search?pretty&size=10000 | |
* | |
* #CodeIgniter #MySQL #Elasticsearch | |
--------------------------------------------------*/ | |
//common line execute only | |
if(php_sapi_name() != 'cli'){ | |
exit(); | |
} | |
set_time_limit(0); | |
define('BASEPATH', __DIR__.'/../application/config/database.php'); | |
include BASEPATH; | |
mysql_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']); | |
mysql_select_db($db['default']['database']); | |
mysql_query('SET NAMES utf8'); | |
mysql_query('SET collation_connection = "utf8_general_ci"'); | |
include '/../application/config/global.php'; | |
$search_url = $config['search_url']; | |
$search_type = "data/app"; | |
if(!$search_url) | |
{ | |
echo 'config load error'; | |
return; | |
} | |
if(!isset($argv[1])) | |
{ | |
echo "please enter clear or patch"; | |
return; | |
} | |
if($argv[1] =='clear') | |
{ | |
clear(); | |
} | |
elseif($argv[1] =='patch') | |
{ | |
patch(); | |
} | |
/** | |
* 清除資料 | |
* @access public | |
*/ | |
function clear() | |
{ | |
$curl = curl_init($GLOBALS['search_url'].'/'.$GLOBALS['search_type']); | |
curl_setopt_array( | |
$curl, | |
array( | |
CURLOPT_CUSTOMREQUEST => 'DELETE', | |
CURLOPT_NOBODY => 1 | |
) | |
); | |
curl_exec($curl); | |
curl_close($curl); | |
$sql = "UPDATE application SET search_id = ''"; | |
mysql_query($sql); | |
echo 'init ok', PHP_EOL; | |
} | |
/** | |
* 建立資料 | |
* @access public | |
*/ | |
function patch() | |
{ | |
echo date("Y-m-d H:i:s"), " patch start\n"; | |
$sql = "SELECT * FROM application WHERE deleted = 0 AND search_id =''"; | |
$result = mysql_query($sql); | |
$total = mysql_num_rows($result); | |
$limit = 100; | |
for ($offset = 0; $offset < $total; $offset += $limit) | |
{ | |
$sql = "SELECT * FROM application WHERE deleted = 0 AND search_id ='' ORDER BY id LIMIT {$limit} offset {$offset}"; | |
$result = mysql_query($sql); | |
while ($field = mysql_fetch_assoc($result)) | |
{ | |
$data = array( | |
'id' => (int)$field['id'], | |
'type' => $field['type'], | |
'name' => $field['name'], | |
'description' => $field['description'], | |
'enterdate' => $field['enterdate'], | |
'admin_id' => $field['admin_id'], | |
'source_type' => 'app', | |
'publish' => $field['publish'], | |
'tag' => $field['tag']? explode(',', strtolower($field['tag'])): array(), | |
'rec_image' => '', | |
'icon' => $field['icon'], | |
'publisher' => $field['publisher'], | |
); | |
$add_result = curl_search('POST', $GLOBALS['search_type'], $data); | |
if(!$add_result) | |
{ | |
echo $data['id'].' add_search_data FAIL!'; | |
continue; | |
} | |
$sql = "UPDATE application SET search_id = '".$add_result['_id']." 'WHERE id = '".$data['id']."'";; | |
mysql_query($sql); | |
} | |
} | |
echo date("Y-m-d H:i:s"), " patch end\n"; | |
echo 'search add ok', PHP_EOL; | |
} | |
/** | |
* handle search engine (ES) api | |
* @access public | |
* @param string | |
* @param string | |
* @param array | |
* @return array | |
*/ | |
function curl_search($method, $type, $data = array()) | |
{ | |
$ch = curl_init(); | |
//mothed | |
$method = strtoupper($method); | |
$data = json_encode($data); | |
if ($method == 'GET') | |
{ | |
$url .= '?' . http_build_query($data); | |
} | |
if ($method == 'POST') | |
{ | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data)) | |
); | |
} | |
if ($method == 'DELETE') | |
{ | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
} | |
if ($method == 'PUT') | |
{ | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data)) | |
); | |
} | |
curl_setopt($ch, CURLOPT_URL, $GLOBALS['search_url'].'/'.$type); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($result, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment