wwwgmo/src/createindex.php
2025-02-07 20:52:27 +01:00

66 lines
2.0 KiB
PHP

<?php
require_once 'config.php';
// connect to es
$client = require 'connect-es.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $es_site_title." v".$site_version ?></title>
<!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/gmo.css">
</head>
<body>
<h2><?php echo $es_site_title ?></h2>
<?php echo $site_copyright; echo "<hr>"; ?>
<?php
// check if index exist
echo "Check if index <b>".$es_index."</b> exists<br>";
$params = [
'index' => $es_index,
];
try {
$response = $client->indices()->delete($params);
echo "Index <b>".$es_index."</b> exists. It is deleted and created<br>";
}
catch (Exception $e) {
echo "Index <b>".$es_index."</b> doesn't exist. <b>".$es_index."</b> will be created<br>";
}
// Create Index
echo "Create index <b>".$es_index."</b><br>";
$params = [
'index' => $es_index,
'body' => [
'settings' => [
'number_of_replicas' => 0
],
],
];
$response = $client->indices()->create($params);
// create document in index
$row = 0;
echo "<hr>";
if (($handle = fopen($data_csv_file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 300, ";")) !== FALSE) {
$row++;
$firstname = $data[0];
$lastname = $data[1];
$email = $data[2];
echo 'Adding: '.$row .' '.$firstname.' '.$lastname.' ('.$email.')<br>';
$params = [
'index' => $es_index,
'body' => [ 'id' => $row, 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email]
];
$response = $client->index($params);
}
fclose($handle);
}
?>
</body>
</html>