当前位置:   article > 正文

Elasticsearch学习笔记(初学) 3 php代码操作_clientbuilder::create()->sethosts

clientbuilder::create()->sethosts

首先引用composer安装客户端

composer require elasticsearch/elasticsearch
  1. <?php
  2. /*
  3. Create By 2022/1/14 - 14:11 - Delimma
  4. To overcome difficulties!
  5. */
  6. namespace app\xadmin\controller;
  7. use Elasticsearch\ClientBuilder;
  8. use think\Controller;
  9. class Es extends Controller
  10. {
  11. // composer require elasticsearch/elasticsearch
  12. private $client ;
  13. public function __construct()
  14. {
  15. $hosts = [
  16. '121.196.35.102:9200'
  17. ];
  18. $this->client = ClientBuilder::create()->setHosts($hosts)
  19. ->setRetries(10)->build();
  20. }
  21. /**
  22. * 添加索引以及映射
  23. */
  24. public function insertDataIntoEs()
  25. {
  26. $params = [
  27. 'index' => 'test_index',
  28. 'body' => [
  29. 'settings' => [ // 分片
  30. 'number_of_shards' => 3, // 3个分片
  31. 'number_of_replicas' => 2, // 分片副本 2
  32. ],
  33. #通过设置mapping结构创建一个索引库(相当于mysql创建一个数据库)
  34. 'mappings' => [
  35. '_source' => [
  36. 'enabled' => true , //原数据查找
  37. ],
  38. # 文档属性定义
  39. #文档类型设置(相当于mysql的数据类型)
  40. 'properties' => [
  41. 'name' => [
  42. 'type' => 'keyword' // 关键字
  43. ],
  44. 'age' => [
  45. 'type' => 'integer' //整形
  46. ],
  47. 'mobile' => [
  48. 'type' => 'text' // 文本
  49. ],
  50. 'createtime' => [
  51. 'type' => 'date' // 日期
  52. ],
  53. 'desc' => [
  54. 'type' => 'text' ,
  55. 'analyzer' => 'ik_max_word' // 分词
  56. ]
  57. ]
  58. ],
  59. ]
  60. ];
  61. # 创建索引以及文档
  62. $result = $this->client->indices()->create($params);
  63. return $result;
  64. }
  65. /**
  66. * 添加
  67. */
  68. public function addDataInfoEs()
  69. {
  70. $params = [
  71. 'index' => 'test_index',
  72. 'id' => '7',
  73. 'body' => [
  74. 'name' => '张三',
  75. 'age' => 20,
  76. 'mobile' => '17767749257',
  77. 'createtime' => date('Y-m-d'),
  78. 'desc' => '这里是张三的描述,PHP 是一种创建并完成动态交互性站点的强有力的服务器端脚本语言。PHP 是免费的,并且使用非常广泛。同时,对于像微软 ASP 这样的竞争者来说,PHP 无疑是另一种高效率的选项。',
  79. ]
  80. ];
  81. $res = $this->client->index($params);
  82. dump($res);
  83. die;
  84. return $res;
  85. }
  86. /**
  87. * 批量添加
  88. */
  89. public function batchAddDataInfoEs()
  90. {
  91. $data = [
  92. ['name' => '张三', 'age' => 10, 'mobile' => '17767749257', 'createtime' => '2020-12-12', 'desc' => '协程MySQL客户端,但是人家不推荐使用了,现在推荐使用的是Swoole\Runtime::enableCoroutine+PDO或Mysqli方式,即一键协程化原生PHP的MySQL客户端'],
  93. ['name' => '李四', 'age' => 20, 'mobile' => '13147649824', 'createtime' => '2020-10-15', 'desc' => '基于hyperf/pool实现数据库连接池并对模型进行了新的抽象,以它作为桥梁,hyperf才能把数据库组件及事件组件接进来'],
  94. ['name' => '白兮', 'age' => 15, 'mobile' => '13411588956', 'createtime' => '2020-08-12', 'desc' => 'TypeScript里的类型兼容性是基于结构子类型的。 结构类型是一种只使用其成员来描述类型的方式。 它正好与名义(nominal)类型形成对比。(译者注:在基于名义类型的类型系统中,数据类型的兼容性或等价性是通过明确的声明和/或类型的名称来决定的。这与结构性类型系统不同,它是基于类型的组成结构,且不要求明确地声明。) 看下面的例子'],
  95. ['name' => '王五', 'age' => 25, 'mobile' => '18955045536', 'createtime' => '2020-12-01', 'desc' => '在使用基于名义类型的语言,比如C#或Java中,这段代码会报错,因为Person类没有明确说明其实现了Named接口。'],
  96. ];
  97. foreach ($data as $k=>$v){
  98. $params['body'][] = [
  99. 'index' => [
  100. '_index' => 'test_index',
  101. '_id' => $k+1
  102. ],
  103. ];
  104. $params['body'][] = [
  105. 'name' => $v['name'],
  106. 'age' => $v['age'],
  107. 'mobile' => $v['mobile'],
  108. 'desc' => $v['desc'],
  109. 'createtime' => $v['createtime'],
  110. ];
  111. }
  112. $res = $this->client->bulk($params);
  113. dump($res);
  114. die;
  115. return $res;
  116. }
  117. /**
  118. * 删除数据
  119. */
  120. public function delDataInfoEs()
  121. {
  122. $params = [
  123. 'index' => 'test_index',
  124. 'id' => 'EFndYX4BAQfgVXMQYs3p',
  125. ];
  126. $res = $this->client->delete($params);
  127. dump($res);
  128. die;
  129. }
  130. /**
  131. * 删除索引下所有数据
  132. */
  133. public function delAllDataInfoEs()
  134. {
  135. $params = [
  136. 'index' => 'test_index',
  137. ];
  138. $res = $this->client->indices()->delete($params);
  139. dump($res);
  140. die;
  141. }
  142. /**
  143. * id查詢
  144. */
  145. public function findDataInfoEs()
  146. {
  147. $params = [
  148. 'index' => 'test_index',
  149. 'id' => '1',
  150. ];
  151. $res = $this->client->get($params);
  152. return $res;
  153. }
  154. /**
  155. * 排序搜索最大id
  156. */
  157. public function findMaxIdDataInfoEs()
  158. {
  159. $params = [
  160. 'index' => 'test_index',
  161. 'body' => [
  162. 'query' => [
  163. 'match' => [
  164. // 'mobile' => '17767749257', # 全匹配
  165. 'desc' => '客端', # 分词器模糊匹配 'analyzer' => 'ik_max_word' // 分词
  166. ]
  167. ]
  168. ]
  169. ];
  170. $res = $this->client->search($params);
  171. dump($res);
  172. die;
  173. }
  174. /**
  175. * 模糊搜索
  176. */
  177. public function searchDataInfoEs()
  178. {
  179. $params = [
  180. 'index' => 'test_index',
  181. 'body' => [
  182. 'query' => [
  183. 'match' => [
  184. // 'mobile' => '17767749257', # 全匹配
  185. 'desc' => '客端', # 分词器模糊匹配 'analyzer' => 'ik_max_word' // 分词
  186. ]
  187. ],
  188. 'sort' => [
  189. '_score' => ['order'=>'desc']
  190. ]
  191. ]
  192. ];
  193. $res = $this->client->search($params);
  194. dump($res);
  195. die;
  196. }
  197. /**
  198. * 布尔搜索
  199. */
  200. public function boolSearchDataInfoEs()
  201. {
  202. $params = [
  203. 'index' => 'test_index',
  204. 'body' => [
  205. 'query' => [
  206. 'bool' => [
  207. 'must' => [
  208. // 查询手机号, 并且姓名=张三
  209. ['match' => ['mobile' => '17767749257']],
  210. ['match' => ['name' => '张三']]
  211. ]
  212. ]
  213. ]
  214. ]
  215. ];
  216. $res = $this->client->search($params);
  217. dump($res);
  218. die;
  219. }
  220. /**
  221. * 布尔+过滤器查询
  222. */
  223. public function boolAndFilterSearchDataInfoEs()
  224. {
  225. $params = [
  226. 'index' => 'test_index',
  227. 'body' => [
  228. 'query' =>[
  229. 'bool' => [
  230. 'filter' => [
  231. 'term' => ['mobile' => '17767749257']
  232. ],
  233. 'should' => [
  234. 'match' => ['name' => '张三']
  235. ]
  236. ]
  237. ]
  238. ]
  239. ];
  240. $res = $this->client->search($params);
  241. dump($res);
  242. die;
  243. }
  244. /**
  245. * 分页查询
  246. */
  247. public function getDataInfoEsByPage()
  248. {
  249. $params = [
  250. 'scroll' => '30s', // 滚动请求间隔多长时间。应该很小!
  251. 'size' => 2 , // 你想要回多少个结果
  252. 'index' => 'test_index',
  253. 'body' => [
  254. 'query' => [
  255. # 查询所有
  256. // 'match_all' => new \stdClass()
  257. # 查询手机号, 并且姓名=张三
  258. 'bool' => [
  259. 'must' => [
  260. ['match' => ['mobile' => '17767749257']],
  261. ['match' => ['name' => '张三']]
  262. ]
  263. ],
  264. ],
  265. ],
  266. ];
  267. $res = $this->client->search($params);
  268. dump($res);
  269. die;
  270. }
  271. /**
  272. * 更新文档
  273. */
  274. public function updateDataInfoEs()
  275. {
  276. $params = [
  277. 'index' => 'test_index',
  278. 'id' => 6,
  279. 'body' => [
  280. 'doc' => [
  281. 'mobile' => '17868422241',
  282. 'name' => '法外狂徒'
  283. ]
  284. ]
  285. ];
  286. $res = $this->client->update($params);
  287. dump($res);
  288. die;
  289. }
  290. /**
  291. * 自增更新
  292. */
  293. public function setIncDataInfoEs()
  294. {
  295. //执行一个脚本来进行更新操作,如对字段进行自增操作或添加新字段。
  296. //为了执行一个脚本更新,你要提供脚本命令和一些参数:
  297. $params = [
  298. 'index' => 'test_index',
  299. 'id' => 6,
  300. 'body' => [
  301. 'script' => 'ctx._source.age +=20',
  302. ]
  303. ];
  304. $res = $this->client->update($params);
  305. dump($res);
  306. }
  307. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/883333
推荐阅读
相关标签
  

闽ICP备14008679号