Sql.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php namespace App\Servers\ElasticSearch\Lib;
  2. use Illuminate\Pagination\LengthAwarePaginator;
  3. use Illuminate\Pagination\Paginator;
  4. /**
  5. * ES Sql
  6. *
  7. * @author 刘相欣
  8. *
  9. */
  10. class Sql{
  11. // 查询属性
  12. private $query = ['index'=>'','type'=>'','body'=>[]];
  13. private $client = null;
  14. private $searchResult = null;
  15. /**
  16. * 指定索引
  17. *
  18. * @param \Elasticsearch\Client $client ES链接
  19. * @param String $index 索引名称
  20. * @param String $type 类型
  21. * @return $this
  22. *
  23. */
  24. public function __construct($client,$index,$type='_doc'){
  25. // 终端
  26. $this->client = $client;
  27. // 指定索引
  28. $this->query['index'] = $index;
  29. $this->query['type'] = $type;
  30. // 链式操作
  31. return $this;
  32. }
  33. /**
  34. * 忽略错误
  35. *
  36. * @param Array $ignore 忽略的错误代码
  37. *
  38. * @return $this
  39. */
  40. public function ignore( array $ignore=[400,401,403,404,408,409]){
  41. // 更新忽略错误
  42. $this->query['client'] = ['ignore' => $ignore];
  43. // 链式操作
  44. return $this;
  45. }
  46. /**
  47. * SQL查询
  48. *
  49. * @param String $query 查询sql语句
  50. * @param String $format 简短标头如json,yaml,smile,cbor,txt,csv,tsv
  51. * @param Array $body 更多参数
  52. * String $cursor 游标
  53. * Int $fetch_size 分页条数
  54. */
  55. public function sql($query,$body=[],$format='json'){
  56. // // 简短标头,如json,yaml,smile,cbor,txt,csv,tsv
  57. // $params = ['format'=>$format];
  58. // // 查询sql语句
  59. // $params['body']['query'] = $query;
  60. // // 游标
  61. // if( !empty($body['cursor']) ) $params['body']['cursor'] = $body['cursor'];
  62. // // 游标
  63. // if( !empty($body['fetch_size']) ) $params['body']['fetch_size'] = $body['fetch_size'];
  64. // 转换成参数
  65. $param = $this->translate($query);
  66. $this->query['body'] = $param;
  67. if( isset($param['fields']) ) $this->query['body']['_source'] = array_column($param['fields'],'field');
  68. unset($this->query['body']['fields']);
  69. // 查询结果
  70. $result = $this->client->search($this->query);
  71. dd($result);
  72. // 最终数据
  73. $data = [];
  74. // 循环获取数据
  75. foreach ($result['rows'] as $row) {
  76. // 重新赋值
  77. $temp = [];
  78. // 匹配字段
  79. foreach ($row as $key => $value) {
  80. $temp[$result['columns'][$key]['name']] = $value;
  81. }
  82. // 获取数据
  83. $data[] = $temp;
  84. }
  85. // 获取数据
  86. $data = ['data'=>$data];
  87. // 获取数据
  88. if( !empty($body['fetch_size']) ) $data['cursor'] = $result['cursor'];
  89. // 进行查询
  90. return $data;
  91. }
  92. /**
  93. * SQL转DSL查询
  94. *
  95. * @param String $query 查询sql语句
  96. *
  97. */
  98. public function translate($query){
  99. // 查询sql语句
  100. $params['body']['query'] = $query;
  101. // 查询结果
  102. $result = $this->client->sql()->translate($params);
  103. // 进行查询
  104. return $result;
  105. }
  106. }