windows7+laravel8+Elasticsearch7.9.3环境搭建步骤及使用
Laravel  /  管理员 发布于 3年前   2478
因为要用Elasticsearch测试一下数据,所以在windows7电脑上搭建一回,顺便记录一下增加一篇博文
开始准备必要条件:
1.elasticsearch-7.9.3-windows-x86_64.zip,我在官网下载最新的,几k每秒捶胸口,最后在是在群里问群友要的;
2.laravel8,已经装好,phpStudy环境已跑起,用的是博客laravel8仿栏目的环境,有兴趣的看一下我之前的博文有记录
一.把Elasticsearch跑起来,解压直接运行文件:D:\elasticsearch-7.9.3\bin\elasticsearch.bat
cmd.exe
...
}, term: 5, version: 51, reason: Publication{term=5, version=51}
[2020-11-27T13:51:08,003][INFO ][o.e.h.AbstractHttpServerTransport] [WD2VNW3O8RK
H2QI] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}, {[::1]
:9200}
[2020-11-27T13:51:08,004][INFO ][o.e.n.Node ] [WD2VNW3O8RKH2QI] st
arted
[2020-11-27T13:51:08,155][INFO ][o.e.l.LicenseService ] [WD2VNW3O8RKH2QI] li
cense [09bedffd-1635-497c-889b-0efb6071476b] mode [basic] - valid
[2020-11-27T13:51:08,158][INFO ][o.e.x.s.s.SecurityStatusChangeListener] [WD2VNW
3O8RKH2QI] Active license is now [BASIC]; Security is disabled
[2020-11-27T13:51:08,167][INFO ][o.e.g.GatewayService ] [WD2VNW3O8RKH2QI] re
covered [2] indices into cluster_state
[2020-11-27T13:51:09,216][INFO ][o.e.c.r.a.AllocationService] [WD2VNW3O8RKH2QI]
Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[
name][0]]]).
启动完:
二.安装elasticsearch扩展,我这直接安装最新的,注意composer的时候记得转阿里云镜像源,不然要卡死
composer require elasticsearch/elasticsearch
三.laravel8中配置文件 .env、config/database.php
1. .env 在底部添加
ES_HOSTS=127.0.0.1:9200
2. config/database.php 也在底部添加
'elasticsearch' => [
// Elasticsearch 支持多台服务器负载均衡,因此这里是一个数组
'hosts' => explode(',', env('ES_HOSTS')),
]
四.将elasticsearch对象注入到laravel容器中 在laravel项目文件
App\Provider\AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; //add fixed sql
use Elasticsearch\ClientBuilder as ESClientBuilder;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
* @return void
*/
public function register()
{
// 注册一个名为 es 的单例
$this->app->singleton('es', function () {
// 从配置文件读取 Elasticsearch 服务器列表
$builder = ESClientBuilder::create()->setHosts(config('database.elasticsearch.hosts'));
// 如果是开发环境
if (app()->environment() === 'local') {
// 配置日志,Elasticsearch 的请求和返回数据将打印到日志文件中,方便我们调试
$builder->setLogger(app('log')->driver());
}
return $builder->build();
});
}
/**
* Bootstrap any application services.
* @return void
*/
public function boot()
{
//
Schema::defaultStringLength(191); //add fixed sql
}
}
五.laravel项目中使用elasticsearch
1.routes\api.php文件中添加 (路由功能描述在控制器里面)
//测试路由 Elasticsearch
use App\Http\Controllers\API\TestController;
Route::get('test', [TestController::class, 'test']);
Route::get('testinsert', [TestController::class, 'testinsert']);
Route::get('testmysqllist', [TestController::class, 'testmysqllist']);
2.添加测试控制器文件 app\Http\Constrollers\API\TestController.php
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Elasticsearch\ClientBuilder;
class TestController extends Controller
{
public $client = null;
public function __construct() {
$this-> client = ClientBuilder::create()->build();
}
//根据索引名(表名),跟id取出数据
public function test()
{
$a = app('es')->get(['index' => 'name', 'id' => 2]);
dd($a);
}
//从数据库表中取出数据 对应库表 存入Elasticsearch中(当然我这是测试就没有对应库表)
public function testinsert()
{
$id = 2;
$info = User::where(['id' => 2])->first()->toArray();
$params = [
'index' => 'name',
'type' => 'title',
'id' => $id,
'body' => ['testField' => $info]
];
$response = $this->client->index($params);
dd($response);
}
//打印Elasticsearch中所有索引数据
public function testmysqllist()
{
$client = ClientBuilder::create()->build();
$data = [
'type' => 'title'
];
$response = $client->search($data);
dd($response['hits']['hits']);
return $response['hits']['hits'];
}
}
3.看看 testmysqllist方法的 效果
array:4 [▼
0 => array:5 [▼
"_index" => "name"
"_type" => "title"
"_id" => "3"
"_score" => 1.0
"_source" => array:1 [▼
"testField" => array:10 [▼
"id" => 3
"name" => "站长二号"
"email" => "[email protected]"
"email_verified_at" => null
"current_team_id" => null
"profile_photo_path" => null
"created_at" => "2020-09-30T06:00:25.000000Z"
"updated_at" => "2020-09-30T06:00:25.000000Z"
"subscribe" => 0
"profile_photo_url" => "https://ui-avatars.com/api/?name=%E7%AB%99%E9%95%BF%E4%BA%8C%E5%8F%B7&color=7F9CF5&background=EBF4FF"
]
]
]
1 => array:5 [▼
"_index" => "name"
"_type" => "title"
"_id" => "2"
"_score" => 1.0
"_source" => array:1 [▼
"testField" => array:10 [▼
"id" => 2
"name" => "站长小号"
"email" => "[email protected]"
"email_verified_at" => null
"current_team_id" => null
"profile_photo_path" => null
"created_at" => "2020-09-30T06:00:25.000000Z"
"updated_at" => "2020-09-30T06:00:25.000000Z"
"subscribe" => 1
"profile_photo_url" => "https://ui-avatars.com/api/?name=%E7%AB%99%E9%95%BF%E5%B0%8F%E5%8F%B7&color=7F9CF5&background=EBF4FF"
]
]
]
2 => array:5 [▼
"_index" => "name1"
"_type" => "title"
"_id" => "1"
"_score" => 1.0
"_source" => array:1 [▶]
]
]
六.cmd.exe中测试
D:\phpStudy\WWW\larabg>php artisan tinker
Psy Shell v0.10.4 (PHP 7.3.22 — cli) by Justin Hileman
>>> app('es')->info();
=> [
"name" => "WD2VNW3O8RKH2QI",
"cluster_name" => "elasticsearch",
"cluster_uuid" => "dupYVfwPSPiDMFoDg3fWlg",
"version" => [
"number" => "7.9.3",
"build_flavor" => "default",
"build_type" => "zip",
"build_hash" => "c4138e51121ef06a6404866cddc601906fe5c868",
"build_date" => "2020-10-16T10:36:16.141335Z",
"build_snapshot" => false,
"lucene_version" => "8.6.2",
"minimum_wire_compatibility_version" => "6.8.0",
"minimum_index_compatibility_version" => "6.0.0-beta1",
],
"tagline" => "You Know, for Search",
]
>>> exit;
Exit: Goodbye
>>> app('es')->get(['index' => 'name', 'id' => 1])
Elasticsearch/Common/Exceptions/Missing404Exception with message '{"_index":"nam
e","_type":"_doc","_id":"1","found":false}'
>>> app('es')->get(['index' => 'name', 'id' => 2])
=> [
"_index" => "name",
"_type" => "_doc",
"_id" => "2",
"_version" => 2,
"_seq_no" => 9,
"_primary_term" => 2,
"found" => true,
"_source" => [
"testField" => [
"id" => 2,
"name" => "站长å°å·",
"email" => "[email protected]",
"email_verified_at" => null,
"current_team_id" => null,
"profile_photo_path" => null,
"created_at" => "2020-09-30T06:00:25.000000Z",
"updated_at" => "2020-09-30T06:00:25.000000Z",
"subscribe" => 1,
"profile_photo_url" => "https://ui-avatars.com/api/?name=%E7%AB%99%E9%9
5%BF%E5%B0%8F%E5%8F%B7&color=7F9CF5&background=EBF4FF",
],
],
]
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号