定义模块

创建文件XXXElasticsearch 继承 app\elasticsearch\model\elasticsearch\index\ElasticsearchBase 即可

table 为需要对应的表名称
pk 为表主键
key 为该表中的字段 type中的具体内容需要对应定义中的映射

analyzer 可以定义使用的分词器

ik_max_word IK分词器 (安装后才可使用,安装教程查看IK分词器篇)
standard standard分词器
  1. namespace app\elasticsearch\model\elasticsearch\index;
  2. class DemoElasticsearch extends \app\elasticsearch\model\elasticsearch\index\ElasticsearchBase
  3. {
  4. protected $table = 'elasticsearch_demo';
  5. protected $pk = 'elasticsearch_demo_id';
  6. protected $key = [
  7. 'elasticsearch_demo_id' =>[
  8. 'type' => 'keyword'
  9. ],
  10. 'elasticsearch_demo_content' => [
  11. 'type' => 'text',
  12. 'analyzer' => 'ik_max_word'
  13. ],
  14. 'create_time' => [
  15. 'type' => 'long',
  16. ],
  17. 'update_time' => [
  18. 'type' => 'long',
  19. ]
  20. ];
  21. public function __construct($table = '',$pk = '',$key = [])
  22. {
  23. if($table) $this->table = $table;
  24. if($pk) $this->pk = $pk;
  25. if($key) $this->key = $key;
  26. }
  27. }

同步单条记录(syncTableId)

只需要传一个ID即可自动同步单条记录 (建议做好事务操作,避免数据不统一)

参数 说明
pk_id 对应的ID

基础使用

 try {
   (new DemoElasticsearch())->syncTableId($details->elasticsearch_demo_id);
 } catch (\Exception $e) {
   throw new Exception($e->getMessage());
 }

案例

 public function submit($post = [])
 {
     $elasticsearch_demo_id = $post['elasticsearch_demo_id'] ?? 0;
     $elasticsearch_demo_content = $post['elasticsearch_demo_content'] ?? '';
     $details = self::where('elasticsearch_demo_id', '=', $elasticsearch_demo_id)
       ->findOrEmpty();
     if ($details->isEmpty()) {
       $details->create_time = time();
     }
     $details->elasticsearch_demo_content = $elasticsearch_demo_content;
     $details->update_time = time();
     $details->save();
     try {
       (new DemoElasticsearch())->syncTableId($details->elasticsearch_demo_id);
     } catch (\Exception $e) {
       throw new Exception($e->getMessage());
     }
     return true;
 }

删除单条记录(delTableId)

参数 说明
pk_id 对应的ID

基础使用

try {
    (new DemoElasticsearch())->delTableId($elasticsearch_demo_id);
} catch (Exception $e) {
  throw new Exception($e->getMessage());
}

案例

public function remove($elasticsearch_demo_id){
  self::where('elasticsearch_demo_id','=',$elasticsearch_demo_id)
    ->findOrEmpty()
    ->delete();
  try {
    (new DemoElasticsearch())->delTableId($elasticsearch_demo_id);
  } catch (Exception $e) {
    throw new Exception($e->getMessage());
  }
  return json(createReturn(true, '', '操作成功'));
}

批量同步数据(syncTable)

参数 说明
where 筛选条件 (同tp6语法)
order 排序 (同tp6语法)
limit 每页的条数

基础使用

$where[] = ['is_elasticsearch_syn','=',0];
$sync_data = (new DemoElasticsearch())->syncTable($where,'create_time desc');

案例

set_time_limit(0);
try {
  $DemoModel = new DemoModel();
  $sync_res['pk_count'] = 1;
  while($sync_res['pk_count']){
    //批量同步数据
    $where[] = ['delete_time','=',0];
    $where[] = ['is_elasticsearch_syn','=',0];
    $sync_data = (new DemoElasticsearch())->syncTable($where,'create_time desc');
    if($sync_data['status']) {
      $sync_res = $sync_data['data'];
      //将已同步内容变为已同步
      $DemoModel
        ->where('elasticsearch_demo_id','in',$sync_res['pk_ids'])
        ->update(['is_elasticsearch_syn' => 1]);
    } else {
      $sync_res['pk_count'] = 0;
    }
  }
  return json(createReturn(true, '', '同步成功'));
} catch (ValidateException $e) {
  return json(createReturn(false, '', $e->getError()));
} catch (\Exception $e) {
  return createReturn(false,'',$e->getMessage());
}

进行分词查询

语法具体可查看查询语句篇

setMustCondition

返回的文档必须满足must子句的条件,并且参与计算分值

$group_content = [];
if($elasticsearch_demo_group_id) {
  $group_content['term'] = [
    'elasticsearch_demo_group_id' => $elasticsearch_demo_group_id
  ];
}
$DemoElasticsearch->setMustCondition($group_content);

setShouldCondition

should 返回的文档可能满足should子句的条件.在一个bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回

$keywords_content = [];
if($keywords) {
  $keywords_content['multi_match'] = [
    'query' => $keywords,
    'fields' => ['elasticsearch_demo_content','elasticsearch_demo_about']
  ];
}
$DemoElasticsearch->setShouldCondition($keywords_content);

setMustNotCondition

返回的文档必须不满足定义的条件

  $DemoElasticsearch->setMustNotCondition([
    'multi_match' => [
      'query' => $not_keywords,
      'fields' => ['elasticsearch_demo_content','elasticsearch_demo_about']
    ]
  ]);

setSortCondition

进行排序控制

 $DemoElasticsearch->setSortCondition([
   'update_time' => [
     'order' => 'desc',
   ],
   '_score' => [
     "order" => "desc"
   ]
 ]);

setSourceCondition

对返回的字段进行筛选

$DemoElasticsearch->setSourceCondition([
  'excludes' => ['create_time']
]);

searchTable

执行筛选

$DemoElasticsearch->searchTable(input('page'),input('limit'));

案例

$keywords = input('keywords','','trim');
$not_keywords = input('not_keywords','','trim');
$elasticsearch_demo_group_id = input('elasticsearch_demo_group_id','','trim');
//筛选的内容
$DemoElasticsearch = (new DemoElasticsearch());
if($elasticsearch_demo_group_id) {
  $group_content = [];
  if($elasticsearch_demo_group_id) {
    $group_content['term'] = [
      'elasticsearch_demo_group_id' => $elasticsearch_demo_group_id
    ];
  }
  $DemoElasticsearch->setMustCondition($group_content);
}

if($keywords) {
  $keywords_content = [];
  if($keywords) {
    $keywords_content['multi_match'] = [
      'query' => $keywords,
      'fields' => ['elasticsearch_demo_content','elasticsearch_demo_about']
    ];
  }
  $DemoElasticsearch->setShouldCondition($keywords_content);
}

if($not_keywords) {
  $DemoElasticsearch->setMustNotCondition([
    'multi_match' => [
      'query' => $not_keywords,
      'fields' => ['elasticsearch_demo_content','elasticsearch_demo_about']
    ]
  ]);
}

//根据排序进行评分
$DemoElasticsearch->setSortCondition([
  'update_time' => [
    'order' => 'desc',
  ],
  '_score' => [
    "order" => "desc"
  ]
]);

$DemoElasticsearch->setSourceCondition([
  'excludes' => ['create_time']
]);

$list =  $DemoElasticsearch->searchTable(input('page'),input('limit'));
return  json(self::createReturn(true,[
  'list' => $list
]));