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. das,nas,san区别——大型数据中心会用NAS+SAN软硬结合思路
  2. 实践篇:利用函数计算轻松构建全文检索系统
  3. Oracle10g安装步骤(一)
  4. windows下编译openssl
  5. Spring官网阅读(二)(依赖注入及方法注入)
  6. 知乎上-翻车的笔记本
  7. 文献记录(part52)--基于度相关性的病毒传播模型及其分析
  8. 3D深度估计,让视频特效更梦幻!
  9. 红外测距模块 51单片机_[51单片机] HC-SR04超声波测距仪
  10. matlab是以什么运算为基础,matlab基础运算
  11. python函数作用的描述_python基础之函数内容介绍
  12. 最简单易懂的C语言代码实现最小二乘法线性拟合直线
  13. VS2010 快捷键
  14. Microsoft.NET离线运行库合集
  15. Verifying dml pool data
  16. 计算机if函数自动填充,Excel表格函数怎么能实现自动填充-excel填充函数,excel表格根据公式自动填充...
  17. c 与易语言程序间通信,易语言与三菱PLC通信-FX系列
  18. 电子技术基础(三)__第1章 并联电路的电阻_电阻的分流公式
  19. python输出成绩分析代码_Python根据成绩分析系统浅析
  20. 第七届“云鼎奖”投票火热开启!七大奖项您做主

热门文章

  1. 2022年全国职业院校技能大赛(中职组)网络安全竞赛试题(10)(总分100分)
  2. 用树莓派构建家庭智能家居控制中心
  3. JAVA7-8 最佳情侣身高差 (10 分)
  4. AngularJS+RequireJs实现动态加载JS和页面的方案研究
  5. 习题 7-12 移动小球(Moving Pegs, ACM/ICPC Taejon 2000, UVa1533)
  6. [Inno]制作类似矮人工具箱的程序/设置系统启动时间工具
  7. java的arraylist_Java ArrayList排序的3种方法
  8. 佳博打印机 ANDROID
  9. Win2000 蓝屏画面错误代码说明
  10. java后台实现excel文件下载功能