demo.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. require_once 'vendor/autoload.php';
  3. /** 实例化客户端 elasticsearch链式操作演示 */
  4. $client = new \Xiaosongshu\Elasticsearch\ESClient(
  5. [
  6. 'nodes' => [
  7. "192.168.110.72:9201",
  8. "127.0.0.1:9201",
  9. ],
  10. 'username' => 'elastic',
  11. 'password' => '123456',
  12. 'proxy' => [
  13. 'client' => [
  14. 'curl' => [
  15. CURLOPT_PROXY => '', // 明确禁用代理
  16. CURLOPT_PROXYUSERPWD => '', // 清除代理认证
  17. CURLOPT_NOPROXY => '*' // 对所有主机禁用代理
  18. ]
  19. ]
  20. ]
  21. ]
  22. );
  23. /** 如果不存在index索引,则创建index索引 */
  24. if (!$client->IndexExists('index')) {
  25. /** 创建索引 */
  26. $client->createIndex('index', '_doc');
  27. /** 创建表 */
  28. $result = $client->createMappings('index', '_doc', [
  29. 'id' => ['type' => 'long',],
  30. 'title' => ['type' => 'text', "fielddata" => true,],
  31. 'content' => ['type' => 'text', 'fielddata' => true],
  32. 'create_time' => ['type' => 'text'],
  33. 'test_a' => ["type" => "integer"],
  34. 'test_b' => ["type" => "rank_feature", "positive_score_impact" => false],
  35. 'test_c' => ["type" => "rank_feature"],
  36. 'name' => ['type' => 'text', "fielddata" => true,],
  37. 'age' => ['type' => 'integer'],
  38. 'sex' => ['type' => 'integer'],
  39. ]);
  40. }
  41. /** 批量插入数据链式操作 */
  42. //$result = $client->table('index', '_doc')->insertAll([
  43. // [
  44. // 'id' => rand(1, 99999),
  45. // 'title' => '天有不测风云',
  46. // 'content' => '月有阴晴圆缺',
  47. // 'create_time' => date('Y-m-d H:i:s'),
  48. // 'test_a' => rand(1, 10),
  49. // 'test_b' => rand(1, 10),
  50. // 'test_c' => rand(1, 10),
  51. // 'name' => '张三',
  52. // 'age' => 27,
  53. // 'sex' => 1
  54. // ]
  55. //]);
  56. $result = $client->table('index', '_doc')->getAll();
  57. //print_r($result);
  58. /** 查询数据链式操作 */
  59. $result = $client
  60. /** 设置表名 */
  61. ->table('index','_doc')
  62. /** must限制条件 */
  63. ->where(['title','=','天有不测风云'])
  64. /** whereIn查询 */
  65. // ->whereIn('age',[28])
  66. /** whereNotIn查询 */
  67. // ->whereNotIn('age',[27,29])
  68. /** should限制条件 当must和should冲突的时候,must条件优先 */
  69. ->orWhere(['test_a','>',8])
  70. /** 排序 */
  71. ->orderBy('test_a','asc')
  72. /** 分页 */
  73. ->limit(0,10)
  74. /** 筛选查询字段 */
  75. // ->select(['name','age'])
  76. /** 按字段分组 */
  77. // ->groupBy(['age','sex'])
  78. /** 聚合查询 */
  79. // ->sum(['age'])
  80. ->max(['age'])
  81. /** 执行查询操作 */
  82. ->getAll();
  83. print_r($result);