elasticsearch
打开命令行窗口 执行命令 elasticsearch -d 启动elasticsearch
安装:打开命令行,切换到Elasticsearch-Head目录,执行以下命令
npm install
启动:打开命令行,切换到Elasticsearch-Head目录,执行以下命令
npm run start
由于跨域(Elasticsearch位于9200端口),需要添加配置: E:\elasticsearch-7.1.0\config\elasticsearch.yml中 #新添加的配置行 http.cors.enabled: true http.cors.allow-origin: "*"
使用composer安装:
在项目目录下,执行以下命令
composer require elasticsearch/elasticsearch
配置php.ini
<?php namespace tools\es; use Elasticsearch\ClientBuilder; class MyElasticsearch { //ES客户端链接 private $client; /** * 构造函数 * MyElasticsearch constructor. */ public function __construct() { $params = array( '127.0.0.1:9200' ); $this->client = ClientBuilder::create()->setHosts($params)->build(); } /** * 判断索引是否存在 * @param string $index_name * @return bool|mixed|string */ public function exists_index($index_name = 'test_ik') { $params = [ 'index' => $index_name ]; try { return $this->client->indices()->exists($params); } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg,true); return $msg; } } /** * 创建索引 * @param string $index_name * @return array|mixed|string */ public function create_index($index_name = 'test_ik') { // 只能创建一次 $params = [ 'index' => $index_name, 'body' => [ 'settings' => [ 'number_of_shards' => 5, 'number_of_replicas' => 0 ] ] ]; try { return $this->client->indices()->create($params); } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg,true); return $msg; } } /** * 删除索引 * @param string $index_name * @return array */ public function delete_index($index_name = 'test_ik') { $params = ['index' => $index_name]; $response = $this->client->indices()->delete($params); return $response; } /** * 添加文档 * @param $id * @param $doc ['id'=>100, 'title'=>'phone'] * @param string $index_name * @param string $type_name * @return array */ public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => $doc ]; $response = $this->client->index($params); return $response; } /** * 判断文档存在 * @param int $id * @param string $index_name * @param string $type_name * @return array|bool */ public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->exists($params); return $response; } /** * 获取文档 * @param int $id * @param string $index_name * @param string $type_name * @return array */ public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->get($params); return $response; } /** * 更新文档 * @param int $id * @param string $index_name * @param string $type_name * @param array $body ['doc' => ['title' => '苹果手机iPhoneX']] * @return array */ public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) { // 可以灵活添加新字段,最好不要乱添加 $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => $body ]; $response = $this->client->update($params); return $response; } /** * 删除文档 * @param int $id * @param string $index_name * @param string $type_name * @return array */ public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->delete($params); return $response; } /** * 搜索文档 (分页,排序,权重,过滤) * @param string $index_name * @param string $type_name * @param array $body * $body = [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'cate_name' => [ 'query' => $keywords, 'boost' => 4, // 权重大 ] ] ], [ 'match' => [ 'goods_name' => [ 'query' => $keywords, 'boost' => 3, ] ] ], [ 'match' => [ 'goods_introduce' => [ 'query' => $keywords, 'boost' => 2, ] ] ] ], ], ], 'sort' => ['id'=>['order'=>'desc']], 'from' => $from, 'size' => $size ]; * @return array */ public function search_doc($index_name = "test_ik",$type_name = "goods",$body=[]) { $params = [ 'index' => $index_name, 'type' => $type_name, 'body' => $body ]; $results = $this->client->search($params); return $results; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现