侯体宗的博客 windows7+laravel8+Elasticsearch7.9.3环境搭建步骤及使用

石可破,丹可磨
  • 首页
  • laravel8仿版
  • beego仿版
  • go_聊天
  • 人生
  • 技术
  • php
  • 架构
  • 数据库
  • 更多
    • 文件下载
    • 匿名群聊
    • 群聊(进来吹会!)
    • 留言
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册
    • 设置栏目
    • 更多设置
    • 分割线

windows7+laravel8+Elasticsearch7.9.3环境搭建步骤及使用

Laravel  /  管理员 发布于 2020-11-27 16:54:39   206

因为要用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]]]).

启动完:

1.png

二.安装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" => "5142245@qq.com"
        "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" => "houtizong@dingtalk.com"
        "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" => "houtizong@dingtalk.com",
         "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",
       ],
     ],
   ]


昵称:

邮箱:

0条评论
最新最热
  • 分类目录
  • 人生 (119)
  • 技术 (46)
  • linux (23)
  • blog从零开始 (9)
  • php (48)
  • 架构 (14)
  • 前端 (22)
  • TP(3/5) (14)
  • 数据库 (29)
  • 微信 (2)
  • Laravel (56)
  • Redis (3)
  • Docker (2)
  • Go (8)
  • 近期文章
  • PHP程序员2021年最新面试题集-持续更新中...(0个评论)
  • PHP数组的底层实现原理浅析(0个评论)
  • php7垃圾回收变量的GC机制详解(0个评论)
  • 在Laravel中进行类型转换详解(0个评论)
  • mysql数据库中事务的四个特征及四种隔离级别的浅析描述(0个评论)
  • Linux awk 命令及统计nginx日志里访问次数最多的前十个IP(0个评论)
  • 论不要在mysql中使用[utf8]编码,如果要用请用[utf8mb4](0个评论)
  • JWT源码实现逻辑详解(0个评论)
  • Laravel内核分析-设计模式之观察者模式(0个评论)
  • Laravel内核分析-设计模式之装饰模式(0个评论)
  • 近期评论
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 @请教  小图标没有出现 重复检查步骤5,进去看看右下角是否开启..
  • 请教 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 你好,我也遇到了安装完右上角没有显示图标,也不能打开相关网页的问题,用的是谷歌浏..
  • Test11 在

    laravel查询构造器中whereNotKey,whereKey,firstWhere用法详解中评论 <script>alert(\test\)</script&g..
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 @西瓜: 每一步都操作完达到效果了吗? 对了是用谷歌浏览器吧..
  • 西瓜 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 你好 我安装完右上角没有显示图标,也不能打开相关网页,是怎么回事呢?..
  • 文章归档
  • 2016-10 (34)
  • 2016-11 (21)
  • 2017-06 (5)
  • 2017-07 (11)
  • 2017-08 (6)
  • 2017-09 (7)
  • 2017-10 (11)
  • 2017-11 (4)
  • 2017-12 (3)
  • 2018-01 (9)
  • 2018-02 (2)
  • 2018-03 (2)
  • 2018-04 (1)
  • 2018-05 (3)
  • 2018-06 (1)
  • 2018-10 (1)
  • 2018-11 (1)
  • 2020-03 (5)
  • 2020-04 (85)
  • 2020-05 (42)
  • 2020-06 (35)
  • 2020-07 (22)
  • 2020-08 (11)
  • 2020-09 (14)
  • 2020-10 (7)
  • 2020-11 (8)
  • 2020-12 (6)
  • 2021-01 (6)
  • 2021-02 (6)
  • 2021-03 (2)
Top
  • 友情链接
  • 侯体宗的博客
  • 三防加固笔记本
  • 澜溪博客
  • 心中hope
  • 徒步认知的博客
  • 陈大剩博客
  • 赵波的博客
  • 佘春晓的博客
  • 自动友链系统

Auther ·HouTiZong© 2009-2020 zongscan.com 版权所有ICP证: 粤ICP备20027696号 PHP交流群 也可以扫右边的二维码

侯体宗的博客