ElasticSearch 协程客户端 - 查询文档

查询

根据 文档ID 查询 document 用法

  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\Get();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setId('my-id');
  12. $response = $elasticsearch->client()->get($bean)->getBody();
  13. var_dump(json_decode($response, true));
  14. });

根据 文档ID 批量查询 document 用法

  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\Mget();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setBody(['ids' => ['my-id', '1']]);
  12. $response = $elasticsearch->client()->mget($bean)->getBody();
  13. var_dump(json_decode($response, true));
  14. });

根据 文档ID 查询 source 用法

  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\Get();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setId('my-id');
  12. $response = $response = $elasticsearch->client()->getSource($bean)->getBody();
  13. var_dump(json_decode($response, true));
  14. });

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\Search();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setBody(['query' => ['match' => ['test-field' => 'ab']]]);
  12. $response = $elasticsearch->client()->search($bean)->getBody();
  13. var_dump(json_decode($response, true));
  14. });

查询总数用法

  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\Count();
  9. $response = $elasticsearch->client()->count($bean)->getBody();
  10. $response = json_decode($response, true);
  11. var_dump($response['count']);
  12. });

scroll 分页查询用法

  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. $sBean = new \EasySwoole\ElasticSearch\RequestBean\Search();
  9. $sBean->setIndex('my-index');
  10. $sBean->setScroll('1m');
  11. $sBean->setBody([
  12. 'query' => [
  13. 'match' => [
  14. 'test-field' => 'abd'
  15. ]
  16. ],
  17. 'sort' => ['_doc'],
  18. 'size' => 1
  19. ]);
  20. $sResponse = $elasticsearch->client()->search($sBean)->getBody();
  21. $sResponse = json_decode($sResponse, true);
  22. $bean = new \EasySwoole\ElasticSearch\RequestBean\Scroll();
  23. $bean->setScrollId($sResponse['_scroll_id']);
  24. $bean->setScroll('1m');
  25. $response = $elasticsearch->client()->scroll($bean)->getBody();
  26. var_dump(json_decode($response, true));
  27. });

template 查询用法

  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\SearchTemplate();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setBody([
  12. 'inline' =>
  13. [
  14. 'query' =>
  15. [
  16. 'match' => ["{{field}}" => "{{value}}"]
  17. ]
  18. ],
  19. 'params' =>
  20. [
  21. 'field' => 'test-field',
  22. 'value' => '博客'
  23. ]
  24. ]);
  25. $response = $elasticsearch->client()->searchTemplate($bean)->getBody();
  26. var_dump(json_decode($response, true));
  27. });

termVectors 用法

  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\TermVectors();
  9. $bean->setIndex('my-index');
  10. $bean->setType('my-type');
  11. $bean->setId('my-id');
  12. $bean->setPretty(true);
  13. $bean->setBody([
  14. 'fields' => ['test-field'],
  15. 'offsets' => true,
  16. 'payloads' => true,
  17. 'positions' => true,
  18. "term_statistics" => true,
  19. "field_statistics" => true
  20. ]);
  21. $response = $elasticsearch->client()->termvectors($bean)->getBody();
  22. var_dump(json_decode($response, true));
  23. });

分片信息查询用法

  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\SearchShards();
  9. $bean->setIndex('my-index');
  10. $response = $elasticsearch->client()->searchShards($bean)->getBody();
  11. $response = json_decode($response, true);
  12. var_dump($response);
  13. });

节点状态获取用法

  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\Info();
  9. $response = $elasticsearch->client()->info($bean)->getBody();
  10. $response = json_decode($response, true);
  11. var_dump($response);
  12. });