ElasticSearch 协程客户端 - 修改文档

根据 id 修改文档用法

  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\Update();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setId('my-id');
  12. $bean->setBody([
  13. 'doc' => [
  14. 'test-field' => 'abd'
  15. ]
  16. ]);
  17. $response = $elasticsearch->client()->update($bean)->getBody();
  18. $response = json_decode($response, true);
  19. var_dump($response);
  20. });

根据 query 修改文档用法

  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\UpdateByQuery();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setBody([
  12. 'query' => [
  13. 'match' => ['test-field' => 'abd']
  14. ],
  15. 'script' => [
  16. 'source' => 'ctx._source["test-field"]="testing"'
  17. ]
  18. ]);
  19. $response = $elasticsearch->client()->updateByQuery($bean)->getBody();
  20. $response = json_decode($response, true);
  21. var_dump($response);
  22. });

Reindex 用法

  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\Reindex();
  9. $bean->setBody([
  10. 'source' => [
  11. 'index' => 'my-index'
  12. ],
  13. 'dest' => [
  14. 'index' => 'my-index-new'
  15. ]
  16. ]);
  17. $response = $elasticsearch->client()->reindex($bean)->getBody();
  18. $response = json_decode($response, true);
  19. var_dump($response);
  20. });