https://blog.csdn.net/qq_16829085/article/details/80725125

  1. 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf
  2. 测试安装  启动elasticSearch:bin/elasticSearch -d       windows系统以管理员身份运行elasticsearch.bat
  3. 测试是否安装成功  127.0.0.1:9200
  4. 安装 ElasticSearch Scout Engine 包

    composer require tamayo/laravel-scout-elastic

    安装这个包的时候,顺便就会装好 Laravel Scout,发布一下资源

    php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

    5.在app.php中添加对应的Provider

                Laravel\Scout\ScoutServiceProvider::class,
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

    6.在count.php中配置参数

     'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
    
     'elasticsearch' => [
    'index' => env('ELASTICSEARCH_INDEX', 'laravel5'),
    'hosts' => [
    env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
    ],
    ],

    7.创建ylaravel的索引和模板

    • php artisan make:command ESInit  创建命令行
    • 修改ESInit.php文件
      <?php
      
      namespace App\Console\Commands;
      
      use GuzzleHttp\Client;
      use Illuminate\Console\Command; class ESInit extends Command
      {
      /**
      * The name and signature of the console command.
      *
      * @var string
      */
      protected $signature = 'es:init'; /**
      * The console command description.
      *
      * @var string
      */
      protected $description = 'init laravel es for post'; /**
      * Create a new command instance.
      *
      * @return void
      */
      public function __construct()
      {
      parent::__construct();
      } /**
      * Execute the console command.
      *
      * @return mixed
      */
      public function handle()
      { $client = new Client();
      // 创建模版
      $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
      $client->put($url, [
      'json' => [
      'template' => config('scout.elasticsearch.index'),
      'settings' => [
      'number_of_shards' => 1
      ],
      'mappings' => [
      '_default_' => [
      '_all' => [
      'enabled' => true
      ],
      'dynamic_templates' => [
      [
      'strings' => [
      'match_mapping_type' => 'string',
      'mapping' => [
      'type' => 'text',
      'analyzer' => 'ik_smart',
      'ignore_above' => 256,
      'fields' => [
      'keyword' => [
      'type' => 'keyword'
      ]
      ]
      ]
      ]
      ]
      ]
      ]
      ]
      ]
      ]); $this->info("========创建模板成功======="); $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
      $client->put($url, [
      'json' => [
      'settings' => [
      'refresh_interval' => '5s',
      'number_of_shards' => 1,
      'number_of_replicas' => 0,
      ],
      'mappings' => [
      '_default_' => [
      '_all' => [
      'enabled' => false
      ]
      ]
      ]
      ]
      ]);
      $this->info("========创建索引成功=======");
      } }

      挂载ESInit,在APP\Console\Kerbel.php文件$commands数组中添加如下代码

      \App\Console\Commands\ESInit::class

      8.调用es脚本    php artisan es:init

      9.导入数据库和数据

      修改数据模型,以post为例

      <?php
      
      namespace App;
      use Illuminate\Database\Eloquent\Model;
      use Illuminate\Database\Eloquent\Builder;
      use Laravel\Scout\Searchable; class Post extends Model
      {
      use Searchable; protected $guarded=[]; //不可以注入的字段 //定义索引里面的type
      public function searchableAs()
      {
      return 'posts_index';
      } //定义有哪些字段需要搜索
      public function toSearchableArray()
      {
      return[
      'title'=>$this->title,
      'content'=>$this->content,
      ];
      }
      • 导入数据模型,php artisan scout:import "App\Post"
      • 验证 http://localhost:9200/laravel5/posts_index/1

      10.Postcontroller中完成功能代码的撰写

          public function search()
      {
      $this->validate(request(),[
      'query'=>'required'
      ]);
      $query=request('query'); $posts=Post::search($query)->paginate(10);
      return view('post/search',compact('posts','query'));
      }

Laravel5使用ElasticSearch的更多相关文章

  1. laravel5+ElasticSearch+go-mysql-elasticsearch MySQL数据实时导入(mac)

    1. ElasticSearch安装 直接使用brew install elasticsearch 安装最新版本的es,基本没有障碍. 2.Laravel5 框架添加elasticsearch支持 在 ...

  2. 在 Laravel 项目中使用 Elasticsearch 做引擎,scout 全文搜索(小白出品, 绝对白话)

    项目中需要搜索, 所以从零开始学习大家都在用的搜索神器 elasiticsearch. 刚开始 google 的时候, 搜到好多经验贴和视频(中文的, 英文的), 但是由于是第一次接触, 一点概念都没 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. Elasticsearch 5.0 中term 查询和match 查询的认识

    Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...

  5. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  6. Ubuntu 14.04中Elasticsearch集群配置

    Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...

  7. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  8. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  9. .net Elasticsearch 学习入门笔记

    一. es安装相关1.elasticsearch安装  运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsear ...

随机推荐

  1. LINUX配置文件介绍

    每个 Linux 程序都是一个可执行文件,它含有操作码列表,CPU 将执行这些操作码来完成特定的操作.例如,ls 命令是由 /bin/ls 文件提供的,该文件含有机器指令的列表,在屏幕上显示当前目录中 ...

  2. UvaLive1347

    Programming contests became so popular in the year 2397 that the governor of New Earck — the largest ...

  3. 区间加值,区间gcd, 牛客949H

    牛客小白月赛16H 小阳的贝壳 题目链接 题意 维护一个数组,支持以下操作: 1: 区间加值 2: 询问区间相邻数差的绝对值的最大值 3: 询问区间gcd 题解 设原数组为\(a\), 用线段树维护\ ...

  4. 安装tengine及淘宝会话保持模块

    安装tengine及淘宝会话保持模块 下载http://tengine.taobao.org/ 解压tar -zxvf tengine-2.3.0.tar.gz 安装GCC: yum -y insta ...

  5. PyCharm如何删除工程项目

    1.在菜单中选择:file——>close project 2.选择需要删除的项目右上角的“×”号进行删除工程项目 3.找到工程项目的存放路径,删除对应的工程项目文件 通过上诉操作即可在pych ...

  6. pyenv虚拟环境管理python多版本和软件库

    可能大家在日常工作中会遇到这么个问题,现在基本的linux系统都是自带老版本的python2.7.x版本,我又不想用老版本,但直接升级可能会出问题,或是依赖老版本的程序就运行不了,有没办法能安装3.x ...

  7. FastAdmin 使用 phpmail 出现 spl_autoload_register 错误

    FastAdmin 使用 phpmail 出现 spl_autoload_register 错误 现象 意思是 __autoload() 已经废弃 问题来源于:https://ask.fastadmi ...

  8. TYVJ1340 送礼物

    P1340 送礼物 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 作为惩罚,GY被遣送去帮助某神牛给女生送礼物(GY:貌似是个好差事)但是在GY看到礼物之后 ...

  9. CSS浏览器兼容解决方案

    1.在ie8的甑别上,如何让样式只对ie8起作用? 用ie浏览器独有的文档注释的方式.像这样: <!DOCTYPE html> <!--> <html class=&qu ...

  10. mac下的抓包工具Charles

    在mac下面,居然没有好的抓包工具,这让我十分纠结,毕竟不可能为了抓一个http包就跑到win下折腾.或许有人说tcpdump这么好的工具,你怎么不用.说实话,tcpdump太复杂了,我还没有细看,再 ...