数据库添加数据

  1. public function actionIndex()
  2. {
  3. // $name = ['赵','钱','孙','李','周','吴','郑','王' ,'冯','陈'];
  4. $name = [
  5. '胡泽洋', '胡藁跃', '胡煜羲', '胡煜曦', '胡凯雷',
  6. '胡晓', '胡邺斓', '胡迈骖', '胡颖妤', '胡罐蔺',
  7. '胡筵陇', '胡钥乙', '胡资戆', '胡镔锋', '胡腾巍',
  8. '胡瑾钰', '胡慧', '胡雨萱', '胡致宁', '胡骥乾',
  9. '胡力蔺', '胡木亮', '胡辰胤', '胡聍', '胡耀方',
  10. '胡耀瀚', '胡耀幻', '胡志广', '胡艺萱', '胡骥浙',
  11. '胡智霭', '胡耀淦', '胡天浩', '胡驭龙', '胡宇诚',
  12. '胡穆然', '胡粲然', '胡锦绣', '胡耀仁', '胡锦程',
  13. '胡怡然', '胡博涵', '胡华', '胡嘉欣', '胡俊霄',
  14. '胡宇航', '胡妤琲', '胡雨薇', '胡誉鳖', '胡誉溦',
  15. '胡皝', '胡榆溦', '胡璇砻', '胡妤溦', '胡昱',
  16. '胡菀溦', '胡文昭', '胡隆杰', '胡龙杰', '胡丹',
  17. '胡爱玲', '胡嘉言', '胡博', '胡婷婷', '胡芮涵'
  18. ];
  19. for ($i = 0; $i < 10000; $i++) {
  20. $demo = new Demo();
  21. $demo->name = $name[mt_rand(0, 13 * 5 - 1)];
  22. $demo->age = mt_rand(1, 100);
  23. $demo->status = mt_rand(-100, 100) > 0 ? 1 : -1;
  24. $time = strtotime("2021-1-1");
  25. $end_time = strtotime("2021-12-31");
  26. $range_time = mt_rand($time, $end_time);
  27. $demo->add_date = date('Y-m-d H:i:s', $range_time);
  28. $demo->update_date = date('Y-m-d H:i:s', mt_rand($range_time, $end_time));
  29. $demo->add_time = date('Y-m-d H:i:s', time());
  30. $demo->save();
  31. }
  32. return 1;
  33. return $this->render('index');
  34. }

创建索引并添加数据

  1. public function actionCreateIndex()
  2. {
  3. set_time_limit(0);
  4. $client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://10.0.18.170:9200'])->build();
  5. $params['index'] = 'es_test';
  6. //判断索引是否存在
  7. if ($client->indices()->exists($params)) {
  8. //存在则删除
  9. $client->indices()->delete($params);
  10. }
  11. //重新创建索引
  12. $client->indices()->create($params);
  13. //设置配置
  14. $data = [ //尽量正确写对字段的数据类型,以及相关配置,否则后续排序,可能会报错!!!
  15. 'name' => 'text', // 字段name,字段类型text
  16. 'add_time' => 'date', // 字段add_time,字段类型date
  17. 'add_date' => 'date',
  18. 'update_date' => 'date',
  19. 'status' => 'integer',
  20. 'age' => 'integer',
  21. 'id' => 'integer',
  22. ];
  23. $mapParam = [];
  24. foreach ($data as $field => $field_type) {
  25. if ($field_type == 'text') { // 需要做全文检索的ik中文分词配置
  26. $mapParam[$field] = [
  27. 'type' => $field_type,
  28. // 'analyzer' => 'ik_max_word',
  29. // 'search_analyzer' => 'ik_max_word'
  30. ];
  31. } elseif ($field_type == 'integer' || $field_type == 'long') { // 数值型的字段,就不需要配置ik中文分词的全文检索了!
  32. $mapParam[$field] = [
  33. 'type' => $field_type
  34. ];
  35. } elseif ($field_type == 'date') {
  36. $mapParam[$field] = [
  37. 'type' => $field_type,
  38. "format" => "yyyy-MM-dd HH:mm:ss"
  39. ];
  40. } else {
  41. $mapParam[$field] = [
  42. 'type' => $field_type
  43. ];
  44. }
  45. }
  46. $indexParam = [
  47. 'index' => 'es_test', ## 索引:数据库
  48. 'type' => 'demo', ## 类型:数据表
  49. ];
  50. $indexParam['body'][$indexParam['type']]['properties'] = $mapParam;
  51. // $indexParam['body']['properties'] = $mapParam;
  52. $indexParam['include_type_name'] = 'true';
  53. $client->indices()->putMapping($indexParam);
  54. //数据
  55. $rtn = Demo::find()->asArray()->all();
  56. $rtnCount = count($rtn);
  57. for ($i = 0; $i < $rtnCount; $i++) {
  58. $params = array();
  59. $params['body'] = array(
  60. 'id' => $rtn[$i]['id'],
  61. 'name' => $rtn[$i]['name'],
  62. 'age' => $rtn[$i]['age'],
  63. 'status' => $rtn[$i]['status'],
  64. 'add_date' => $rtn[$i]['add_date'],
  65. 'update_date' => $rtn[$i]['update_date'],
  66. 'add_time' => $rtn[$i]['add_time']
  67. );
  68. $params['index'] = 'es_test';
  69. $params['type'] = 'demo';
  70. //Document will be indexed to log_index/log_type/autogenerate_id
  71. $client->index($params);
  72. }
  73. echo 'create index done!';
  74. return 1;
  75. }

数据查询

  1. public function actionGetIndex()
  2. {
  3. $client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://10.0.18.170:9200'])->build();
  4. $params = array();
  5. $params['index'] = 'es_test';
  6. $params['type'] = 'demo';
  7. // $params['body']['query']['match']['status'] = '1';
  8. // $params['body']['query']['match_phrase']['name'] = '胡木';
  9. // $params['body']['sort'] = ['add_date' => 'desc','age' => 'desc','id' => 'desc'];
  10. /**
  11. * 精确查找 term
  12. */
  13. // $params['body']['query']['term'] = ['age' => 44];
  14. /**
  15. * terms(精确查找多个字段)
  16. */
  17. // $params['body']['query']['terms'] = ['age' => [44,45,49]];
  18. /**
  19. * range(范围查找),
  20. */
  21. // $params['body']['query']['range'] = [
  22. // 'age' => [
  23. // 'gt' => 1,
  24. // 'lt' => 20
  25. // ]
  26. // ];
  27. /**
  28. * exists(等同于MySQL中的 is not null),
  29. * 查找存在age属性的文档
  30. */
  31. // $params['body']['query']['exists'] = [
  32. // 'field' => 'status'
  33. // ];
  34. /**
  35. * bool(用来组合其他过滤条件,包含 must,must_not,should操作)
  36. */
  37. // $params['body']['query']['bool'] = [
  38. // //年龄应该 > 20 < 40
  39. // 'should' => [
  40. // 'range' => [
  41. // 'age' => ['gt' => '20', 'lt' => 40]
  42. // ]
  43. // ],
  44. // //状态不为-1
  45. // 'must_not' => [
  46. // 'term' => [
  47. // 'status' => '-1'
  48. // ]
  49. // ],
  50. // //查找名字中包括晓的
  51. // 'must' => [
  52. // 'term' => [
  53. // 'name' => '晓'
  54. // ]
  55. // ]
  56. // ];
  57. /**
  58. * match(匹配一个字段)
  59. */
  60. // $params['body']['query']['match'] = ['age' => '44'];
  61. /**
  62. * match_all(匹配所有文档,相当于没有条件)
  63. * 等于是 $query = []
  64. */
  65. /**
  66. * multi_match(匹配多个字段)
  67. *
  68. */
  69. // $params['body']['query']['multi_match'] = [
  70. // 'query' => '1',
  71. // 'fields' => ['name', 'age', 'status']
  72. // ];
  73. $params['size'] = 100;
  74. $params['from'] = 0;
  75. $rtn = $client->search($params);
  76. header("content-type: text/html;charset=utf-8");
  77. echo "<pre>";
  78. print_r($rtn);
  79. exit;
  80. }