windows7+laravel8+Elasticsearch7.9.3环境搭建步骤及使用
管理员 发布于 3年前   354
因为要用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",
],
],
]
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成
是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客
文章标签
友情链接