|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Extend; |
| 4 | + |
| 5 | +use Elasticsearch\ClientBuilder; |
| 6 | + |
| 7 | +/** |
| 8 | + * php Elasticsearch 使用示列类 |
| 9 | + */ |
| 10 | +class Elasticsearch |
| 11 | +{ |
| 12 | + |
| 13 | + public $es; |
| 14 | + |
| 15 | + /** |
| 16 | + * 构造方法 |
| 17 | + * Elasticsearch constructor. |
| 18 | + */ |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + // host数组可配置多个节点 |
| 22 | + $params = ['127.0.0.1:9200']; |
| 23 | + $this->es = ClientBuilder::create() |
| 24 | + ->setHosts($params) |
| 25 | + ->build(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * 创建索引(指定模板) |
| 30 | + * |
| 31 | + * 几个关键属性 |
| 32 | + * |
| 33 | + * String类,分为两种: |
| 34 | + * text:可分词,生成索引,不参与聚合 |
| 35 | + * keyword:不可分词,数据会作为完整字段进行匹配,可参与聚合 |
| 36 | + * |
| 37 | + * Numberical数值类型,分两类: |
| 38 | + * 基本数据类型:long、integer、short、byte、double、float、half_float |
| 39 | + * 浮点数高精度类型:scaled_float(需要制定精度因子,10或100这样,es会把真实值与之相乘后存储,取出时还原) |
| 40 | + * |
| 41 | + * Date日期类型 |
| 42 | + * ES 可以对日期格式,化为字符串存储 |
| 43 | + * |
| 44 | + * ik_max_word 和 ik_smart |
| 45 | + * ik_max_word:会对文本做最细力度的拆分 |
| 46 | + * ik_smart:会对文本做最粗粒度的拆分 |
| 47 | + * 两种分词器的最佳实践: 索引时用ik_max_word(面面俱到), 搜索时用ik_smart(精准匹配)。 |
| 48 | + * |
| 49 | + * 分词器 analyzer 和 search_analyzer |
| 50 | + * 分词器 analyzer 的作用有二: |
| 51 | + * 一:插入文档时,将 text 类型字段做分词,然后插入 倒排索引。 |
| 52 | + * 二:在查询时,先对 text 类型输入做分词, 再去倒排索引搜索。 |
| 53 | + * |
| 54 | + * 如果想要"索引"和"查询",使用不同的分词器,那么只需要在字段上使用search_analyzer。这样,索引只看analyzer,查询就看 search_analyzer。 |
| 55 | + */ |
| 56 | + public function createIndex() |
| 57 | + { |
| 58 | + $params = [ |
| 59 | + 'index' => 'test', // 索引名称 |
| 60 | + 'body' => [ |
| 61 | + 'settings' => [ // 配置 |
| 62 | + 'number_of_shards' => 3, // 主分片数 |
| 63 | + 'number_of_replicas' => 1 // 主分片的副本数 |
| 64 | + ], |
| 65 | + 'mappings'=> [ // 映射 |
| 66 | + '_source' => [ // 存储原始文档 |
| 67 | + 'enabled' => 'true' |
| 68 | + ], |
| 69 | + 'properties' => [ |
| 70 | + 'id' => [ |
| 71 | + 'type' => 'long' |
| 72 | + ], |
| 73 | + 'author' => [ |
| 74 | + 'type' => 'keyword' |
| 75 | + ], |
| 76 | + 'age' => [ |
| 77 | + 'type' => 'integer' |
| 78 | + ], |
| 79 | + 'content' => [ |
| 80 | + 'type' => 'text', |
| 81 | + 'index' => 'true', |
| 82 | + 'analyzer' => 'ik_max_word' |
| 83 | + ], |
| 84 | + 'create_at' => [ |
| 85 | + 'type' => 'date', |
| 86 | + 'format' => "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" |
| 87 | + ] |
| 88 | + ] |
| 89 | + ] |
| 90 | + ] |
| 91 | + ]; |
| 92 | + $response = $this->es->indices()->create($params); |
| 93 | + v($response); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * 删除索引 |
| 98 | + */ |
| 99 | + public function deleteIndex() |
| 100 | + { |
| 101 | + $params = ['index' => 'test']; |
| 102 | + $response = $this->es->indices()->delete($params); |
| 103 | + v($response); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * 修改索引配置参数 |
| 108 | + */ |
| 109 | + public function updateIndexSetting() |
| 110 | + { |
| 111 | + $params = [ |
| 112 | + 'index' => 'test', // 索引名 |
| 113 | + 'body' => [ |
| 114 | + 'settings' => [ // 修改设置 |
| 115 | + 'number_of_replicas' => 5 // 副本数 |
| 116 | + ] |
| 117 | + ] |
| 118 | + ]; |
| 119 | + $response = $this->es->indices()->putSettings($params); |
| 120 | + v($response); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * 获取一个或多个索引的当前配置参数 |
| 125 | + */ |
| 126 | + public function getSettings() |
| 127 | + { |
| 128 | + $params = ['index' => 'test']; |
| 129 | + $response = $this->es->indices()->getSettings($params); |
| 130 | + v($response); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 查询一个或多个索引的mapping定义 |
| 135 | + */ |
| 136 | + public function getMapping() |
| 137 | + { |
| 138 | + // 询所有的mapping定义 |
| 139 | + /*$response = $this->es->indices()->getMapping();*/ |
| 140 | + |
| 141 | + |
| 142 | + // 查询my_index和my_index2两个索引的mapping定义 |
| 143 | + /*$params = ['index' => [ 'my_index', 'my_index2']]; |
| 144 | + $response = $this->es->indices()->getMapping($params);*/ |
| 145 | + |
| 146 | + |
| 147 | + // 查询指定索引的Mapping定义 |
| 148 | + $params = ['index' => 'test']; |
| 149 | + $response = $this->es->indices()->getMapping($params); |
| 150 | + v($response); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * 创建文档 |
| 155 | + */ |
| 156 | + public function indexDoc() |
| 157 | + { |
| 158 | + $body = [ |
| 159 | + ['id' => 1, 'author' => 'wdjisn', 'age' => 28, 'content' => '只有承担起旅途风雨,才能最终守得住彩虹满天。', 'create_at' => '2022-05-01 12:35:46'], |
| 160 | + ['id' => 2, 'author' => 'style', 'age' => 30, 'content' => '因为四季轮回,我们感受着自然变幻,体味着春华秋实。', 'create_at' => '2022-05-06 09:41:32'], |
| 161 | + ['id' => 3, 'author' => 'wdjisn', 'age' => 28, 'content' => '环境永远不会十全十美,消极的人受环境控制,积极的人却控制环境。', 'create_at' => '2022-05-05 18:24:31'], |
| 162 | + ['id' => 4, 'author' => 'shuishou', 'age' => 35, 'content' => '走得最慢的人,只要他不丧失目标,也比漫无目的地徘徊的人走得快。', 'create_at' => '2022-05-08 10:01:53'], |
| 163 | + ['id' => 5, 'author' => 'dali', 'age' => 32, 'content' => '一个人一定要有自己好好过日子的信心,要有别人无法忽视的能力,这是幸福的基本。', 'create_at' => '2022-05-10 20:30:01'] |
| 164 | + ]; |
| 165 | + |
| 166 | + foreach ($body as $key=>$value) { |
| 167 | + $params = [ |
| 168 | + 'index' => 'test', |
| 169 | + 'type' => '_doc', |
| 170 | + 'id' => $value['id'], // 设置文档Id, 可以忽略Id, Es也会自动生成 |
| 171 | + 'body' => $value |
| 172 | + ]; |
| 173 | + $response = $this->es->index($params); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * 查询文档 |
| 179 | + */ |
| 180 | + public function getDoc() |
| 181 | + { |
| 182 | + $params = [ |
| 183 | + 'index' => 'test', |
| 184 | + 'type' => '_doc', |
| 185 | + 'id' => 1 |
| 186 | + ]; |
| 187 | + $response = $this->es->get($params); |
| 188 | + v($response); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * 搜索文档 |
| 193 | + */ |
| 194 | + public function searchDoc() |
| 195 | + { |
| 196 | + $params = [ |
| 197 | + 'index' => 'test', |
| 198 | + 'type' => '_doc', |
| 199 | + 'body' => [ |
| 200 | + 'query' => [ |
| 201 | + 'bool' => [ |
| 202 | + 'should' => [ |
| 203 | + ['match' => [ |
| 204 | + 'content' => '人' |
| 205 | + ] |
| 206 | + ], |
| 207 | + ['match' => [ |
| 208 | + 'author' => 'wdjisn' |
| 209 | + ] |
| 210 | + ] |
| 211 | + ], |
| 212 | + ], |
| 213 | + ], |
| 214 | + 'sort' => ['create_at' => ['order' => 'desc']], // 排序 |
| 215 | + 'from' => 0, // 分页 |
| 216 | + 'size' => 10 // 分页 |
| 217 | + ] |
| 218 | + ]; |
| 219 | + $response = $this->es->search($params); |
| 220 | + v($response); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * 更新文档 |
| 225 | + */ |
| 226 | + public function updateDoc() |
| 227 | + { |
| 228 | + $params = [ |
| 229 | + 'index' => 'test', |
| 230 | + 'type' => '_doc', |
| 231 | + 'id' => 1, |
| 232 | + 'body' => [ |
| 233 | + 'doc' => [ // doc包含的内容就是我们想更新的字段内容 |
| 234 | + 'content' => '行动是成功的阶梯,行动越多,登得越高。' |
| 235 | + ] |
| 236 | + ] |
| 237 | + ]; |
| 238 | + $response = $this->es->update($params); |
| 239 | + v($response); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * 删除文档 |
| 244 | + */ |
| 245 | + public function deleteDoc() |
| 246 | + { |
| 247 | + $params = [ |
| 248 | + 'index' => 'test', |
| 249 | + 'type' => '_dco', |
| 250 | + 'id' => 1 |
| 251 | + ]; |
| 252 | + $response = $this->es->delete($params); |
| 253 | + v($response); |
| 254 | + } |
| 255 | +} |
0 commit comments