数据库添加数据
public function actionIndex() { // $name = ['赵','钱','孙','李','周','吴','郑','王' ,'冯','陈']; $name = [ '胡泽洋', '胡藁跃', '胡煜羲', '胡煜曦', '胡凯雷', '胡晓', '胡邺斓', '胡迈骖', '胡颖妤', '胡罐蔺', '胡筵陇', '胡钥乙', '胡资戆', '胡镔锋', '胡腾巍', '胡瑾钰', '胡慧', '胡雨萱', '胡致宁', '胡骥乾', '胡力蔺', '胡木亮', '胡辰胤', '胡聍', '胡耀方', '胡耀瀚', '胡耀幻', '胡志广', '胡艺萱', '胡骥浙', '胡智霭', '胡耀淦', '胡天浩', '胡驭龙', '胡宇诚', '胡穆然', '胡粲然', '胡锦绣', '胡耀仁', '胡锦程', '胡怡然', '胡博涵', '胡华', '胡嘉欣', '胡俊霄', '胡宇航', '胡妤琲', '胡雨薇', '胡誉鳖', '胡誉溦', '胡皝', '胡榆溦', '胡璇砻', '胡妤溦', '胡昱', '胡菀溦', '胡文昭', '胡隆杰', '胡龙杰', '胡丹', '胡爱玲', '胡嘉言', '胡博', '胡婷婷', '胡芮涵' ]; for ($i = 0; $i < 10000; $i++) { $demo = new Demo(); $demo->name = $name[mt_rand(0, 13 * 5 - 1)]; $demo->age = mt_rand(1, 100); $demo->status = mt_rand(-100, 100) > 0 ? 1 : -1; $time = strtotime("2021-1-1"); $end_time = strtotime("2021-12-31"); $range_time = mt_rand($time, $end_time); $demo->add_date = date('Y-m-d H:i:s', $range_time); $demo->update_date = date('Y-m-d H:i:s', mt_rand($range_time, $end_time)); $demo->add_time = date('Y-m-d H:i:s', time()); $demo->save(); } return 1; return $this->render('index'); }
创建索引并添加数据
public function actionCreateIndex() { set_time_limit(0); $client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://10.0.18.170:9200'])->build(); $params['index'] = 'es_test'; //判断索引是否存在 if ($client->indices()->exists($params)) { //存在则删除 $client->indices()->delete($params); } //重新创建索引 $client->indices()->create($params); //设置配置 $data = [ //尽量正确写对字段的数据类型,以及相关配置,否则后续排序,可能会报错!!! 'name' => 'text', // 字段name,字段类型text 'add_time' => 'date', // 字段add_time,字段类型date 'add_date' => 'date', 'update_date' => 'date', 'status' => 'integer', 'age' => 'integer', 'id' => 'integer', ]; $mapParam = []; foreach ($data as $field => $field_type) { if ($field_type == 'text') { // 需要做全文检索的ik中文分词配置 $mapParam[$field] = [ 'type' => $field_type, // 'analyzer' => 'ik_max_word', // 'search_analyzer' => 'ik_max_word' ]; } elseif ($field_type == 'integer' || $field_type == 'long') { // 数值型的字段,就不需要配置ik中文分词的全文检索了! $mapParam[$field] = [ 'type' => $field_type ]; } elseif ($field_type == 'date') { $mapParam[$field] = [ 'type' => $field_type, "format" => "yyyy-MM-dd HH:mm:ss" ]; } else { $mapParam[$field] = [ 'type' => $field_type ]; } } $indexParam = [ 'index' => 'es_test', ## 索引:数据库 'type' => 'demo', ## 类型:数据表 ]; $indexParam['body'][$indexParam['type']]['properties'] = $mapParam; // $indexParam['body']['properties'] = $mapParam; $indexParam['include_type_name'] = 'true'; $client->indices()->putMapping($indexParam); //数据 $rtn = Demo::find()->asArray()->all(); $rtnCount = count($rtn); for ($i = 0; $i < $rtnCount; $i++) { $params = array(); $params['body'] = array( 'id' => $rtn[$i]['id'], 'name' => $rtn[$i]['name'], 'age' => $rtn[$i]['age'], 'status' => $rtn[$i]['status'], 'add_date' => $rtn[$i]['add_date'], 'update_date' => $rtn[$i]['update_date'], 'add_time' => $rtn[$i]['add_time'] ); $params['index'] = 'es_test'; $params['type'] = 'demo'; //Document will be indexed to log_index/log_type/autogenerate_id $client->index($params); } echo 'create index done!'; return 1; }
数据查询
public function actionGetIndex() { $client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://10.0.18.170:9200'])->build(); $params = array(); $params['index'] = 'es_test'; $params['type'] = 'demo'; // $params['body']['query']['match']['status'] = '1'; // $params['body']['query']['match_phrase']['name'] = '胡木'; // $params['body']['sort'] = ['add_date' => 'desc','age' => 'desc','id' => 'desc']; /** * 精确查找 term */ // $params['body']['query']['term'] = ['age' => 44]; /** * terms(精确查找多个字段) */ // $params['body']['query']['terms'] = ['age' => [44,45,49]]; /** * range(范围查找), */ // $params['body']['query']['range'] = [ // 'age' => [ // 'gt' => 1, // 'lt' => 20 // ] // ]; /** * exists(等同于MySQL中的 is not null), * 查找存在age属性的文档 */ // $params['body']['query']['exists'] = [ // 'field' => 'status' // ]; /** * bool(用来组合其他过滤条件,包含 must,must_not,should操作) */ // $params['body']['query']['bool'] = [ // //年龄应该 > 20 < 40 // 'should' => [ // 'range' => [ // 'age' => ['gt' => '20', 'lt' => 40] // ] // ], // //状态不为-1 // 'must_not' => [ // 'term' => [ // 'status' => '-1' // ] // ], // //查找名字中包括晓的 // 'must' => [ // 'term' => [ // 'name' => '晓' // ] // ] // ]; /** * match(匹配一个字段) */ // $params['body']['query']['match'] = ['age' => '44']; /** * match_all(匹配所有文档,相当于没有条件) * 等于是 $query = [] */ /** * multi_match(匹配多个字段) * */ // $params['body']['query']['multi_match'] = [ // 'query' => '1', // 'fields' => ['name', 'age', 'status'] // ]; $params['size'] = 100; $params['from'] = 0; $rtn = $client->search($params); header("content-type: text/html;charset=utf-8"); echo "<pre>"; print_r($rtn); exit; }