tuyenvv

sync es

May 18th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. <?php
  2. require 'vendor/autoload.php';
  3.  
  4. use Elasticsearch\ClientBuilder;
  5.  
  6.  
  7. $esHost = [
  8.     'elasticsearch:9200'
  9. ];
  10. $client = ClientBuilder::create()->setHosts($esHost)->build();
  11.  
  12. $indexName = 'mkp';
  13.  
  14. $params = [
  15.     'index' => $indexName,
  16.     'body' => [
  17.         'mappings' => [
  18.             'properties' => [
  19.                 'attributes' => [
  20.                     'type' => 'nested'
  21.                 ],
  22.             ],
  23.         ],
  24.     ]
  25. ];
  26.  
  27. $client->indices()->create($params);
  28.  
  29.  
  30. $servername = "mariadb";
  31. $username = "root";
  32. $password = "root";
  33. $dbName = 'mykiot';
  34.  
  35. // Create connection
  36. $conn = new mysqli($servername, $username, $password, $dbName);
  37.  
  38. // Check connection
  39. if ($conn->connect_error) {
  40.     die("Connection failed: " . $conn->connect_error);
  41. }
  42.  
  43. $sql = "SELECT id, data FROM cache_products where store_id = 423677";
  44. $result = $conn->query($sql);
  45.  
  46. if ($result->num_rows > 0) {
  47.     // output data of each row
  48.     while ($row = $result->fetch_assoc()) {
  49.         echo "id: " . $row["id"] . "\n";
  50.         $data = json_decode($row['data'], true);
  51.  
  52.         $params = [
  53.             'index' => $indexName,
  54.             'id' => $row['id'],
  55.             'body' => $data
  56.         ];
  57.  
  58.         $response = $client->index($params);
  59.  
  60.     }
  61. } else {
  62.     echo "0 results";
  63. }
  64. $conn->close();
Add Comment
Please, Sign In to add comment