ElasticSearch 协程客户端 - 插入文档

单条插入文档用法

  1. <?php
  2. $config = new \EasySwoole\ElasticSearch\Config([
  3. 'host' => '127.0.0.1',
  4. 'port' => 9200
  5. ]);
  6. $elasticsearch = new \EasySwoole\ElasticSearch\ElasticSearch($config);
  7. go(function () use ($elasticsearch) {
  8. $bean = new \EasySwoole\ElasticSearch\RequestBean\Create();
  9. $bean->setIndex('my_index');
  10. $bean->setType('my_type');
  11. $bean->setId('my_id');
  12. $bean->setBody(['test_field' => 'test_data']);
  13. $response = $elasticsearch->client()->create($bean)->getBody();
  14. $response = json_decode($response, true);
  15. var_dump($response['result']);
  16. });

批量插入文档用法

  1. <?php
  2. $config = new \EasySwoole\ElasticSearch\Config([
  3. 'host' => '127.0.0.1',
  4. 'port' => 9200
  5. ]);
  6. $elasticsearch = new \EasySwoole\ElasticSearch\ElasticSearch($config);
  7. go(function () use ($elasticsearch) {
  8. $bean = new \EasySwoole\ElasticSearch\RequestBean\Bulk();
  9. $bean->setIndex('my_index');
  10. $bean->setType('my_type');
  11. $body = [];
  12. for ($i = 1; $i <= 5; $i++) {
  13. $body[] = [
  14. 'create' => [
  15. '_index' => 'my-index',
  16. '_type' => 'my-type',
  17. '_id' => $i * 1000
  18. ]
  19. ];
  20. $body[] = [
  21. 'test-field' => 'test-data',
  22. ];
  23. }
  24. $bean->setBody($body);
  25. $response = $elasticsearch->client()->bulk($bean)->getBody();
  26. $response = json_decode($response, true);
  27. var_dump($response);
  28. });