Laravel5使用ElasticSearch
https://blog.csdn.net/qq_16829085/article/details/80725125
- 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf
- 测试安装 启动elasticSearch:bin/elasticSearch -d windows系统以管理员身份运行elasticsearch.bat
- 测试是否安装成功 127.0.0.1:9200
- 安装 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的更多相关文章
- laravel5+ElasticSearch+go-mysql-elasticsearch MySQL数据实时导入(mac)
1. ElasticSearch安装 直接使用brew install elasticsearch 安装最新版本的es,基本没有障碍. 2.Laravel5 框架添加elasticsearch支持 在 ...
- 在 Laravel 项目中使用 Elasticsearch 做引擎,scout 全文搜索(小白出品, 绝对白话)
项目中需要搜索, 所以从零开始学习大家都在用的搜索神器 elasiticsearch. 刚开始 google 的时候, 搜到好多经验贴和视频(中文的, 英文的), 但是由于是第一次接触, 一点概念都没 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- Elasticsearch 5.0 中term 查询和match 查询的认识
Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...
- 以bank account 数据为例,认识elasticsearch query 和 filter
Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...
- Ubuntu 14.04中Elasticsearch集群配置
Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...
- ElasticSearch 5学习(10)——结构化查询(包括新特性)
之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- .net Elasticsearch 学习入门笔记
一. es安装相关1.elasticsearch安装 运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsear ...
随机推荐
- Thinkphp 不足之处
1.报错机制 //控制器里面直接输出如下内容,代码不提示.TP报错机制已经开启 echo $aaaaaa; bbbbbbbbb; eco bbbbbbbb; 正常应该给出以下提示 Notice: Un ...
- AutoDesk产品,Maya 2018 安装,Microsoft Visual C++ 2012 安装失败,结果 = -2147024546,安装Microsoft Visual C++ 2012 Redistributable 错误0x80070005 等等
今日老弟装Maya 2018出现问题,我帮忙解决了一下问题,过程颇为曲折,记录一下,看能否帮到有类似困惑的朋友. 我和老弟的电脑牌子一样,就现在自己电脑上装了,竟然开始和他的错误是一样的!都是Micr ...
- DirectX11笔记(五)--Direct3D渲染1--VERTICES AND INPUT LAYOUTS
原文:DirectX11笔记(五)--Direct3D渲染1--VERTICES AND INPUT LAYOUTS 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...
- Vue.之. 动态设置按钮Disabled
Vue.之. 动态设置按钮Disabled 按钮代码如下: 添加了一个 属性 :disabled="isAble" ,控制:更新按钮.重置按钮 <el-form- ...
- 【水滴石穿】github_popular
项目不难,就是文件摆放位置跟别的不一样 https://github.com/chenji336/github_popular //定义入口是app.js ///** @format */ impor ...
- 怎么用PHP+sqlite3验证登录用户名和密码
Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象 ...
- Liferay 7 module项目的依赖问题
build.gradle中的dependencies和bnd.bnd的Private-Package的关系是,build.gradle解决编译时候所需的所有依赖问题,但是这些依赖并不会被打包到buil ...
- qt获取本机ip
//获取本机IP QString getIP(QString localHost) { QString ipAddr; #if 0 QList<QHostAddress> AddressL ...
- 【JZOJ4896】【NOIP2016提高A组集训第16场11.15】兔子
题目描述 在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与其相连.换句话讲,这些兔子窝之 ...
- java 使用poi导出Excel,设置单元格保护不可编辑
//sheet表加密:等效excel的审阅菜单下的保护工作表 sheet.protectSheet(new String("333"));//333是密码 更多设置请参考:http ...