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 ...
随机推荐
- 2018-8-10-win10-uwp-禁止编译器优化代码
title author date CreateTime categories win10 uwp 禁止编译器优化代码 lindexi 2018-08-10 19:16:50 +0800 2018-2 ...
- Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- python 中初始化二维数组的方法
最好的方法是: 初始化4*3的二维数组 a = [[0 for col in xrange(3)] for row in xrange(4)] 而不可以用: a = [[0]*3]*4 [0]*3是生 ...
- JS---案例:拖曳对话框
案例:拖曳对话框 ps: 实际没有要拖曳登录框的需求,只是演示拖曳的这个效果 1. 获取超链接,注册点击事件,显示登陆框和遮挡层 2. 获取关闭,注册点击事件,隐藏登陆框和遮挡层 3. 按下鼠标,移动 ...
- R语言基础画图/绘图/作图
R语言基础画图/绘图/作图 R语言基础画图 R语言免费且开源,其强大和自由的画图功能,深受广大学生和可视化工作人员喜爱,这篇文章对如何使用R语言作基本的图形,如直方图,点图,饼状图以及箱线图进行简单介 ...
- JSP Web第五章整理复习 JSP访问数据库
P164 例5-1 常用SQL语句 P178 数据库连接池 (1)连接池的作用 存储多个数据库连接对象,当程序需要时,从池中获取1个连接,程序执行完成后再还给连接池.避免数据库连接建立.关闭的开 ...
- golang之数据结构
4种:bool/int/uint/uintptr(其中bool类型的零值为false,其余类型的零值为0) 4种:float32/float64/complex64/complex126 (零值为0) ...
- sublime配置node开发环境
下载node插件 https://github.com/tanepiper/SublimeText-Nodejs 下载zip压缩包后解压,文件名改为Node 打开文件“Nodejs.sublime-s ...
- oralce触发器
n 触发器的分类 DML(insert,delete,update)触发器 DDL(create table ,create view...drop...)触发器 系统触发器(与系统相关的触发器,比 ...
- 初探postman
第一种:安装postman 扩展程序 第二种:本地 安装postman 登陆进来postman的界面 发送第一个postman请求 将请求保存到集合 未完,待续...