laravel es 相关代码 ElasticSearch

ops/2025/3/10 22:24:18/

来源:

github

php"><?phpnamespace App\Http\Controllers;use Elastic\Elasticsearch\ClientBuilder;
use Illuminate\Support\Facades\DB;class ElasticSearch extends Controller
{public $client = null;public function __construct(){$this->client = ClientBuilder::create()->setHosts(["http://elasticsearch:9200"])->setBasicAuthentication('elastic', "123456")->build();}public function infos(){$response = $this->client->info();echo "<pre>";print_R($response);}// 创建分词索引public function esCreateIk(){$params = ['index' => 'ik','body' => ['mappings' => ['properties' => ['content' => ['type' => 'text','analyzer' => 'ik_max_word',],],],],];$ik = $this->client->indices()->create($params);dd($ik->asArray());}//判断索引是否存在public function isIndex(){$index = $this->client->indices()->exists(['index' => 'ik'])->asBool();dd($index);}//查看索引的的信息public function indexInfo(){$index = $this->client->indices()->getMapping(['index' => 'ik']);dd($index->asArray());}//删除索引及数据public function indexDelete(){$index = $this->client->indices()->delete(['index' => 'ik']);dd($index->asArray());}//删除索引下面id=1的数据public function esDelete(){$params = ['index' => 'ik','id' => 1,];$response = $this->client->delete($params);dd($response->asArray());}// 数据插入public function esCreateIkData(){$array = ['index' => 'ik','type' => 'doc','id' => 1,'body' => ['content' => '测试数据',],];$result = $this->client->index($array);dd($result);}// 批量插入数据public function eaCreateIkDataBulk(){set_time_limit(0);$data = DB::table('faq')->get();// 一条一条插入foreach ($data as $key => $value) {$array['body'][] = ['index' => ['_index' => 'ik', '_id' => $value->id]];$array['body'][] = ['content' => $value->content];}$result = $this->client->bulk($array);dd($result);}// 查询当前索引下有多少数据public function esCountData(){$params = ['index' => 'ik',];echo $this->client->count($params);}/*** 查询 ik 下面所有数据* 默认返回最多10数据*/public function esIkSearch(){$query = ['index' => 'ik',// 'id' => 1, // 查询id 的话就加这个字段];$result = $this->client->search($query);dd($result->asArray());}/*** 查询 ik 下面数据 加各种条件**/public function esIkSearchWhere(){$query = ['index' => 'ik','body' => ['query' => ['match' => ['content' => '被骗了几千块钱,有微信怎么要回来']]],'_source' => ['content'], //目前只有content 如果字段多了 想要那个返回哪个。可以不设置。默认返回所有数据 'size' => 5, //设置一次返回5条数据、可以不设置'from' => 2, //从第几条开始 类似于limit 5,2 可以不设置];$result = $this->client->search($query);dd($result->asArray());}/*** 修改数据* 把 ik 下面 id=1 的content 修改*/public function esIkedit(){$query = ['index' => 'ik','id' => 1,'body' => ['doc' => ['content' => '修改数据'],],];$result = $this->client->update($query);dd($result);}
}

在这里插入图片描述

php">Route::get("es/info", [ElasticSearch::class, "infos"]); //ES信息Route::get("es/create_ik", [ElasticSearch::class, "esCreateIk"]); // 创建分词索引Route::get("es/delete", [ElasticSearch::class, "esDelete"]); //删除索引的里面的某条数据Route::get("es/indexDelete", [ElasticSearch::class, "indexDelete"]); //删除索引Route::get("es/esCreateIkData", [ElasticSearch::class, "esCreateIkData"]); //插入数据Route::get("es/eaCreateIkDataBulk", [ElasticSearch::class, "eaCreateIkDataBulk"]); //批量插入数据Route::get("es/esIkSearch", [ElasticSearch::class, "esIkSearch"]); //返回所有数据Route::get("es/esIkSearchWhere", [ElasticSearch::class, "esIkSearchWhere"]); //加查询条件,返回所有数据

http://www.ppmy.cn/ops/164773.html

相关文章

【RAG】基于向量检索的 RAG (BGE示例)

RAG机器人 结构体 文本向量化: 使用 BGE 模型将文档和查询编码为向量。 &#xff08;BGE 是专为检索任务优化的开源 Embedding 模型&#xff0c;除了本文API调用&#xff0c;也可以通过Hugging Face 本地部署BGE 开源模型&#xff09; 向量检索: 从数据库中找到与查询相关的文…

HTML 插入图片(简单易懂较详细)

在 HTML 中&#xff0c;插入图片是通过 <img> 标签实现的。<img> 标签是一个空标签&#xff0c;意味着它不需要闭合标签。以下是插入图片的基本语法和常用属性的详细讲解。 一、基本语法 <img src"图片路径" alt"替代文本">src&#x…

使用Wireshark截取并解密摄像头画面

在物联网&#xff08;IoT&#xff09;设备普及的今天&#xff0c;安全摄像头等智能设备在追求便捷的同时&#xff0c;往往忽视了数据传输过程中的加密保护。很多摄像头默认通过 HTTP 协议传输数据&#xff0c;而非加密的 HTTPS&#xff0c;从而给潜在攻击者留下了可乘之机。本文…

Calico-BGP FullMesh模式与RR模式 Day04

1. BGP协议简单介绍 BGP是什么&#xff1f;BGP是如何工作的&#xff1f; - 华为 Configure BGP peering | Calico Documentation 1.1 什么是BGP 边界网关协议&#xff08;BGP&#xff09;是一种用于在网络中的路由器之间交换路由信息的标准协议。每台运行 BGP 的路由器都有一…

SAP HANA Merge

在SAP HANA数据库中&#xff0c;数据表都分为两个区域&#xff1a;Main Store和Delta Store。Main Store中的数据经过高压缩处理&#xff0c;查询和计算效率高&#xff0c;但写入成本高&#xff1b;而Delta Store则是为写入优化的区域&#xff0c;数据会定期从Delta Store合并到…

【愚公系列】《Python网络爬虫从入门到精通》045-Charles的SSL证书的安装

标题详情作者简介愚公搬代码头衔华为云特约编辑&#xff0c;华为云云享专家&#xff0c;华为开发者专家&#xff0c;华为产品云测专家&#xff0c;CSDN博客专家&#xff0c;CSDN商业化专家&#xff0c;阿里云专家博主&#xff0c;阿里云签约作者&#xff0c;腾讯云优秀博主&…

STM32Cubemx配置E22-xxxT22D lora模块实现定点传输

文章目录 一、STM32Cubemx配置二、定点传输**什么是定点传输?****定点传输的特点****定点传输的工作方式****E22 模块定点传输配置****如何启用定点传输?****示例****应用场景****总结****配置 1:`C0 00 07 00 02 04 62 00 17 40`****解析****配置 2:`C0 00 07 00 01 04 62…

配置nacos

解压资料中的nacos-server-1.2.0.zip 进入bin目录双击 startup.cmd 运行文件 访问http://localhost:8848/nacos 注册admin服务 1<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-d…