protected $es;public function __construct(){$params = array('127.0.0.1:9200');$this->es = ClientBuilder::create()->setHosts($params)->build();}

1.创建索引(分词,拼音,同义词)

  public function setMapping(){$params = ['index' => 'news','body' => ['settings' => ['analysis' => ['filter' => ['my_synonym_filter' => ['type' => 'dynamic_synonym',
//                                'synonyms_path' => "analysis/synonym.txt"'synonyms_path' => "http://127.0.0.1/phpproject/tp5es/synonym.txt","interval" => 30]],'analyzer' => ['my_synonyms' => ['tokenizer' => 'ik_max_word','filter' => ['lowercase','my_synonym_filter']]]]],'mappings' => ['_doc' => ['properties' => ['name' => ['type' => 'keyword','fields' => ['pinyin' => ['type' => 'text','analyzer' => 'pinyin',]]],'age' => ['type' => 'integer'],'content' => ['type' => 'text','analyzer' => 'my_synonyms','fields' => ['pinyin' => ['type' => 'text','analyzer' => 'pinyin']]]]]]]];$res = $this->es->indices()->create($params);var_dump($res);}

2.删除索引

//删除索引public function deleteindex(){$params = ['index' => 'test2',];$response = $this->es->indices()->delete($params);var_dump($response);}

3.添加数据(单个添加,批量添加)

 //单个添加public function add(){$params =['index' => 'news','type' => '_doc','id' => 'id_1','body' => ['name' => '体育课','age' => 31,'content' => '煤化工哦看']];$response = $this->es->index($params);var_dump($response);}//批量添加public function addall(){for($i = 2; $i < 10; $i ++) {$params['body'][] = ['index' => ['_index' => 'news','_type' => '_doc','_id' => 'id_'.$i]];$params['body'][] = ['name' => '李四'.$i,'age' => $i,'content' => '李四也爱吃西红柿'];}$response = $this->es->bulk($params);var_dump($response);}

4.更新(单个或者批量更新)

//单个更新
public function update(){$params = ['index' => 'news','type' => '_doc','id' => 'id_2','body' => ['doc' => ['name' => null]]];$response = $this->es->update($params);var_dump($response);}//批量更新(1)public function updatesome(){$params = ['index' => 'news','type' => '_doc','body' => [
//                把年龄为50的加1'script' => 'ctx._source.age += 1','query' => ['term' => ['age' => 50]]]];$res = $this->es->updateByQuery($params);var_dump($res);}//    批量更新(2)public function testbulk(){
//        查出需要更新的字段$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['bool' => ['must' => ['range' => ['age' => ['gt' => '5','lt' => '40']]]]],'from' => 0,'size' => 100,'sort' => ['age' => 'desc']]];$info = $this->es->search($params);$data = $info['hits']['hits'];// 批量执行更新文档$params['body'] = [];$nameArr = ['赵一','钱二','孙三','李四','周五','吴六'];foreach ($data as $key => $value) {$params['body'][] = [
//                更新文档'update' => ['_index' => $value['_index'],'_type' => $value['_type'],'_id' => $value['_id'],]];$params['body'][] = ['doc' => [
//                   更新指定的字段值'name' => $nameArr[mt_rand(0,5)]]];}$res = $this->es->bulk($params);var_dump($res);}

5.删除数据(单个或批量删除)

//单个删除
public function delete(){$params = ['index' => 'news','type' => '_doc','id' => 'id_3'];$response = $this->es->delete($params);var_dump($response);}//    批量删除public function deletesome(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['wildcard' => ['content' => '*123*',]]]];$res = $this->es->deleteByQuery($params);var_dump($res);}

6.搜索

(1)拼音

 public function search(){$params = ['index' => 'news','type' =>  '_doc','body' => ['query' => ['match' => ['name.pinyin' => 'zhang']],//查询呢个字段"_source" => [ "name", "age" ],]];$result = $this->es->search($params);var_dump($result);}

(2)排序加高亮显示

 public function simSearch(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['match' => ['content' => '中美']],'sort' => ['age' => ['order' => 'desc']],//高亮'highlight' => [//高亮标签(默认<em></em>)'pre_tags' => ["<em style='color: red'>"],'post_tags' => ["</em>"],'fields' => ['content' => new \stdClass()]]]];$result = $this->es->search($params);var_dump($result);}

(3)分页

// from -> size分页public function page(){$params = ['index' => 'news','type' => '_doc','body' => ['from' => 0,'size' => 3,'query' => ['match' => ['content' => '番茄']]]];$result = $this->es->search($params);var_dump($result);}//scroll 分页public function scroll(){//查询5页$page = 5;$params = ['index' => 'news','type' => '_doc','size' => 3,'scroll' => '1m','body' => [
//                查询条件]];$result = $this->es->search($params);var_dump($result);echo "<br><br>";$scroll_id = $result['_scroll_id'];for ($i = 2; $i < $page; $i ++) {$response = $this->es->scroll(['scroll_id' => $scroll_id,'scroll' => '1m']);if (count($response['hits']['hits']) > 0) {$scroll_id = $response['_scroll_id'];var_dump($response);echo "<br><br>";} else {break;}}}

(4)处理null字符

 //过滤非空 is not nullpublic function filternull(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['constant_score' => ['filter' => [//exists过滤为null的字段'exists' => ['field' => 'name']]]]]];$result = $this->es->search($params);var_dump($result);}//查找出某字段为空 is null public function isnull(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['bool' => ['must_not' => ['exists' => ['field' => 'name']]]]]];$result = $this->es->search($params);var_dump($result);}

(5)查询多个字段并设置权重

 //多字段查询public function allSearch(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['multi_match' => ['query' => '马铃薯','fields' => ['name^2.0', 'content^1.0']]]]];$result = $this->es->search($params);var_dump($result);}

(6)聚合函数示例

//group分组public function group(){$params = ['index' => 'news','type' => '_doc','body' => ['aggs' => ['group_by' => ['terms' => ['field' => 'age']]],]];$result = $this->es->search($params);var_dump($result);}//minpublic function min(){$params = ['index' => 'news','type' => '_doc','body' => ['aggs' => ['group_by' => ['min' => ['field' => 'age']]]]];$result = $this->es->search($params);var_dump($result);}

(7)组合查询

// 组合查询public function muchSearch(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['bool' => ['must' => [['term' => ['name' => '马铃薯']]],'must_not' => [['match' => ['content' => '中国和美国']]],]]]];$result = $this->es->search($params);var_dump($result);}// must+shouldpublic function getall(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['bool' => ['must' => [['match' => ['age' => 50]],['bool' => ['should' => [['match' => ['content' => '番茄']],['match' => ['content' => '中国和美国']]]]]]]]]];$result = $this->es->search($params);var_dump($result);}

(8)热搜词

//出现最多的前两个public function gethot(){$params = ['index' => 'news','type' =>  '_doc','body' => ['aggs' => ['top' => ['terms' => ['field' => 'name','size' => 2, //top2'order' => ['_count' => 'desc'] //降序排列]]]]];$result = $this->es->search($params);var_dump($result);}

(9)ids查询

    public function  ids(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => [
//                    根据id查询'ids' => ['values' => ['id_1', 'id_3']]]]];$result = $this->es->search($params);var_dump($result);}

(10)type查询

public function type(){$params = ['index' => 'news','body' => ['query' => ['type' => ['value' => '_doc',]]]];$result = $this->es->search($params);var_dump($result);}

(11)正则查询

 public function regexp(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['regexp' => ['name' => '体育.']]]];$result = $this->es->search($params);var_dump($result);}

(12)通配符查询

public function wildcard(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['wildcard' => ['content' => '*工*',]]]];$result = $this->es->search($params);var_dump($result);}

(13)前缀查询

public function prefix(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['prefix' => ['content' => '马铃',]]]];$result = $this->es->search($params);var_dump($result);}

(14)范围查询

  public function range(){$params = ['index' => 'news','type' => '_doc','body' => ['query' => ['range' => ['age' => ['gte' => 2,'lte' => 40,]]]]];$result = $this->es->search($params);var_dump($result);}

tp5结合es6.x的基本用法相关推荐

  1. ES6之Object.assign()用法,Object.assign()到底是浅拷贝还是深拷贝?

    基本用法 Object.assign方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target). const target = { a: 1 }; const sou ...

  2. 最全ES6详解及用法

    最全ES6详解及用法 前言 babel babel使用方法 变量的定义 let.const this 和作用域 do 顶层对象 global对象 import class JS中的原型 原型语言 pr ...

  3. es6 Promise 的基本用法

    Promise 的基本用法 ES6 规定,Promise对象是一个构造函数,用来生成Promise实例. 下面代码创造了一个Promise实例. const promise = new Promise ...

  4. ES6之Promise基本用法

    1.Promise相关概念 Promise 是异步编程的一种解决方案,比传统的解决方案(回调函数和事件)更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了语法,原生提供了Prom ...

  5. es6中reduce的用法_es6中reduce的基本使用方法

    前言 为啥要把es6 中 reduce 单独拿出来说呢,因为这个功能实在太骚,值得如此. reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值.reduc ...

  6. es6在原生代码的用法_原生JavaScript之es6中Class的用法分析

    对比一下 基本上,ES6 的class可以看作只是一个语法糖,它的绝大部分功能,ES5 都可以做到,新的class写法只是让对象原型的写法更加清晰.更像面向对象编程的语法而已.上面的代码用 ES6 的 ...

  7. es6中reduce的用法_25个你不得不知道的数组reduce高级用法

    背景 距离上一篇技术文章<1.5万字概括ES6全部特性>发布到现在,已经有整整4个月没有输出过一篇技术文章了.哈哈,不是不想写,而是实在太忙,这段时间每天不是上班就是加班,完全没有自己的时 ...

  8. ES6关于Promise的用法详解

    Node的产生,大大推动了Javascript这门语言在服务端的发展,使得前端人员可以以很低的门槛转向后端开发. 当然,这并不代表迸发成了全栈.全栈的技能很集中,绝不仅仅是前端会写一些HTML和一些交 ...

  9. ES6关于Promise的用法

    Node的产生,大大推动了Javascript这门语言在服务端的发展,使得前端人员可以以很低的门槛转向后端开发. 当然,这并不代表迸发成了全栈.全栈的技能很集中,绝不仅仅是前端会写一些HTML和一些交 ...

最新文章

  1. mysql修改配置文件内存后无法启动_记一次Oracle实例在修改内存大小后无法启动的惊悚经历...
  2. Python爬虫与一汽项目【三】爬取中国五矿集团采购平台
  3. nginx模块学习六 add_header 跨域访问
  4. 万户OA应变大考验之新员工学习篇
  5. Spring Web Flow 入门demo(二)与业务结合 附源码
  6. python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行
  7. org.apache.jasper.JasperException: An exception occurred processing JSP page /admin/jiaoshi/daochuEx
  8. ubuntu修改root密码
  9. 前端学习(2533):mapgetter和actions
  10. 女性买房需要注意哪些问题
  11. 美国太空部队加入美国情报系统,以确保太空的安全
  12. jquery ajax和servlet,浅谈ajax在jquery中的请求和servlet中的响应
  13. 【渝粤教育】国家开放大学2018年春季 0008-22T简明现代汉语 参考试题
  14. 解决错误---undefined reference to `pthread_create‘
  15. 智能实验室-全能优化(Guardio) 4.6.0.760
  16. C/c++输入输出函数
  17. 目标规划运筹学例题doc_运筹学之目标规划(胡运权版).doc
  18. python txt文本特定字符串提取
  19. java se 试题_JavaSE基础试题附答案
  20. STM32之TIM1高级定时器

热门文章

  1. swissprot评论区_利用SWISS_PROT网获取生物信息学资源
  2. Ansible 学习总结(11)—— task 并行执行之 forks 与 serial 参数详解
  3. 百度地图mapsdkvi.com.gdi.bgl.android.java.EnvDrawText异常
  4. CSDN物联网学习5 从芯片到云端 Python物联网全栈开发经验教训共享
  5. 黎曼猜想证明了?Michael Atiyah的愚人节难道在9月吗……
  6. 兆骑科创科创赛事举办,创业大赛活动路演
  7. 体育场地预定球馆订场小程序APP公众版开发建设定制
  8. RedHat Enterprise Linux Server 6.4 迅雷快传 下载地址
  9. windows server2019 AD域环境下用gpo统一员工桌面壁纸不久变黑屏
  10. android u盘读写权限,Android 外部SD卡/U盘无法写入解决方法(需要root)