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 ...
随机推荐
- 洛谷P1077 [NOIP2012普及组]摆花 [2017年四月计划 动态规划14]
P1077 摆花 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能 ...
- js中index()的四种经典用法111
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 初次接触python的re模块
刷CF的时候,看到一个简单的题目,可以用来练练正则表达式 于是乎找到了re.sub的用法,说明如下 re.sub: (pattern, repl, string, count=0, │ f ...
- Django 用 userena 做用户注册验证登陆
django-admin startproject userena2 cd userena2python manage.py startapp accounts vim userena2/settin ...
- jeecg Access restriction 问题解决
最近导入新项目,导入所有用到的jar包,发现其中一个引用报错 import com.sun.istack.internal.Nullable; 具体信息如下: Access restriction: ...
- oracle 写存储过程有返回值时 注意在loop循环处添加返回值:=
例子: create or replace procedure p_xl is v_count NUMBER(10); begin for rs in(select yhbh from dbyh) l ...
- 建立DbLink 时报ORA-01017/ORA-02063分析及解决
今天在11G的oracle数据库上创建DBlink连接10g的库时, 出现: ORA-01017: invalid username/password; logon denied ORA-02063: ...
- 廖雪峰Python总结3
1.模块简介 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件中,这样每个文件包含的代码相对来说就比较少.一个.py文件就称之为一个模块(Module). 使用模块的好处: 提高了代码的可 ...
- Directx教程(24) 简单的光照模型(3)
原文:Directx教程(24) 简单的光照模型(3) 在工程myTutorialD3D11_17中,我们重新定义我们的cube顶点法向,每个三角形面的顶点法向都是和这个三角形的面法向是一致 ...
- 阿里云应用高可用 AHAS 正式商用,可一键提升云上应用可用性
在分布式架构环境下,服务间的依赖日益复杂,可能没有人能说清单个故障对整个系统的影响,构建一个高可用的分布式系统面临着很大挑战. 7月17日,阿里云应用高可用服务AHAS 正式商用,包含架构感知.流控降 ...