ElasticSearch(六) Elasticsearch在Thinkphp5.0中的使用
首先下载需要引入的类库
链接:https://pan.baidu.com/s/1XEXviLoWM-ypwJ_B0jXqlg 密码:u54t //Elasticsearch.zip类库压缩包地址
然后将压缩包解压到vendor目录下

<?php
namespace app\index\controller;
use think\Controller;
class Ec extends Controller
{
public function _initialize()
{ Vendor('Elasticsearch.autoload');
$params['hosts'] = array(
'192.168.9.155:9200'
);
$this->client = new \Elasticsearch\Client($params);
}
public function index(){
$this->search();
} //创建索引
//现在我们开始添加一个新的索引和一些自定义设置:
public function create_index()
{
$indexParams['index'] = 'myindex'; //索引名称
$indexParams['type'] = 'mytype'; //类型名称
$indexParams['body']['settings']['number_of_shards'] = ; //当前只有一台ES,1就可以了
$indexParams['body']['settings']['number_of_replicas'] = ; //副本0,因为只有一台ES
$this->client->create($indexParams);
}
//插入索引数据
public function add_document()
{
$params = array();
$params['body'] = array(
'product_name' => '要插入的商品名称',
'prodcut_id' =>
);
$params['index'] = 'myindex'; //索引名称
$params['type'] = 'mytype'; //类型名称
$params['id'] = ''; //不指定id,系统会自动生成唯一id
$ret = $this->client->index($params);
}
//删除索引
//由于 elasticsearch 的动态性质,我们添加第一个文档的时候自动创建了索引和一些默认设置。让我们删除这个索引,因为我们以后想要指定自己的设置:
public function delete_index()
{
$deleteParams['index'] = 'myindex';
$this->client->indices()->delete($deleteParams);
}
//删除文档
public function delete_document()
{
$deleteParams = array();
$deleteParams['index'] = 'myindex';
$deleteParams['type'] = 'mytype';
$deleteParams['id'] = '';
$retDelete = $this->client->delete($deleteParams);
}
//更改文档
public function update_document()
{
$updateParams = array();
$updateParams['index'] = 'myindex';
$updateParams['type'] = 'mytype';
$updateParams['id'] = 'my_id';
$updateParams['body']['doc']['product_name'] = '新商品名';
$response = $this->client->update($updateParams); }
//查询
public function search()
{
$searchParams['index'] = 'myindex';
$searchParams['type'] = 'mytype';
$searchParams['from'] = ;
$searchParams['size'] = ;
$searchParams['sort'] = array(
'_score' => array(
'order' => 'id'
)
);
//相当于sql语句: select * from hp_product where prodcut_name like '茶' limit 0,100 order by id desc;
$searchParams['body']['query']['match']['product_name'] = '茶';
$retDoc = $this->client->search($searchParams); echo '<pre>';
print_r($retDoc);
//相当于sql语句: select * from hp_product where product_name like '茶' and product_id = 20 limit 200,10;
// $searchParams['body']['query']['bool']['must'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
// $searchParams['size'] = 10;
// $searchParams['from'] = 200;
//
//
// 当于sql语句:select * from hp_product where product_name like '茶' or product_id = 20 limit 200,10;
// $searchParams['body']['query']['bool']['should'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
//
//
// 当于sql语句: select * from hp_product where product_name like '茶' and product_id != 20 limit 200,10;
// $searchParams['body']['query']['bool']['must_not'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
//
//
//当于sql语句:select * from hp_product where id>=20 and id<30 limit 200,10;
// $searchParams['body']['query']['range'] = array(
// 'id' => array('gte' => 20,'lt' => 30);
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
}
//获取文档
public function get_document()
{
$getParams = array();
$getParams['index'] = 'myindex';
$getParams['type'] = 'mytype';
$getParams['id'] = '';
$retDoc = $this->client->get($getParams);
print_r($retDoc);
}
}
?>
关于更多详细的说明就自己百度一下官方文档和百度其他帖子吧~
ElasticSearch(六) Elasticsearch在Thinkphp5.0中的使用的更多相关文章
- 在thinkphp5.0中调用ajax时, 返回的JSON 格式数据在html前台不能用时
在thinkphp5.0中调用ajax时,如果控制器返回的数据为json格式,视图层接收到返回值即为json格式的数据,此时应该把 JSON 文本转换为 JavaScript 对象,方便调用.具体代码 ...
- ThinkPHP5.0中Redis的使用和封装(原创)
Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不 ...
- thinkPHP5.0中使用header跳转没作用
我在controller中的方法中这样写: header("Location:".$url); 但是一直没动静,不会跳转,最后还是官方文档解决了 https://www.kancl ...
- thinkphp5.0 中使用第三方无命名空间的类库
ThinkPHP5建议所有的扩展类库都使用命名空间定义,如果你的类库没有使用命名空间,则不支持自动加载,必须使用Loader::import方法先导入文件后才能使用. 首先要在文件头部使用loader ...
- ThinkPHP5.0中Request请求对象的使用和常用的操作
request的使用 第一种方法 在控制器头部添加request引用 然后在方法里调用 ‘instance’类 然后在调用方法: public function index($name='name') ...
- ThinkPHP5.0中Request请求对象的常用操作
获取当前系统参数 // 获取当前域名 echo '获取当前域名:'.$request->domain() . '<br/>'; // 获取当前入口文件 echo '获取当前入口文件: ...
- ThinkPHP5.0中的build.php自动生成所需的目录结构的详细方法
一.来到根目录下,找到bulid.php文件进行改写. 改写方法:保留常用的目录结构,其余按照需求改吧! 二.复制一份build.php文件到application目录下 此时根目录下的bulid.p ...
- thinkphp5.0 中简单处理微信支付异步通知
public function wx_notify(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; libxml_disable_ent ...
- PHP(ThinkPHP5.0) + PHPMailer 进行邮箱发送验证码
GitHub下载最新版第三方类库PHPMailer: 第一步: 打开网址https://github.com/PHPMailer/PHPMailer/ 下载PHPMailer,PHPMailer 需要 ...
随机推荐
- 在iOS模拟器上安装程式的ios-sim
针对iOS装置进行开发时,绝大部分开发者采用的工具都是官方的Xcode.问题是负责图像设计和开发管理人员,却不一定熟悉Xcode的操作,这时ios-sim便是一个解决方案. 曾经从事iOS开发的朋友, ...
- Servlet------>jsp自定义标签5(标签体内容改为大写)
5.把标签体内容改为大写(tld中的配置我就省略了,详细请看jsp自定义标签1) import java.io.IOException; import javax.servlet.jsp.JspExc ...
- ipconfig /flushdns
C:\Users\sas>ipconfig /flushdns Windows IP 配置 已成功刷新 DNS 解析缓存. C:\Users\sas>ipconfig --help 错误: ...
- HTTP Headers Client Identification
用户信息通过HTTP头部承载:不能实现用户唯一性标识. w HTTP The Definitive Guide Table 11-1 shows the seven HTTP request head ...
- Restful and 前后端分离---AutoTest newman--postman
http://www.cnblogs.com/zuoshaowei/p/6192863.html https://www.getpostman.com/docs/newman_intro swagge ...
- 该死的Kafka,远程连接Kafka超时以及解决办法
关于消息的发布与订阅,之前一直使用的是activeMQ基于JMS的消息队列进行操作的,最近听说有一个更高效的消息的发布与订阅技术,就是Kafka. 关于kafka的介绍,在这里就不做过多讲解了,因为我 ...
- MapReduce自定义InputFormat和OutputFormat
一.自定义InputFormat 需求:将多个小文件合并为SequenceFile(存储了多个小文件) 存储格式:文件路径+文件的内容 c:/a.txt I love Beijing c:/b.txt ...
- Python下的正则表达式原理和优化笔记
摘要: 本文旨在总结一些编写表达式的技巧和原理.鉴于介绍python中re模块的使用方法的文章太多.所以本文在基础方面都是略过,而在回溯原理和一些技巧方面记录一点点学习总结. 目录:[ - ] 基础规 ...
- python的scikit-learn的主要模块和基本使用
在从事数据科学的人中,最常用的工具就是R和Python了,每个工具都有其利弊,但是Python在各方面都相对胜出一些,这是因为scikit-learn库实现了很多机器学习算法. 加载数据(Data L ...
- 007-mac快捷键
锁屏:Ctrl + Command + Q touch-bar:方法:“系统偏好设置”>“键盘”>“自定Control Strip…”,将“锁定屏幕”图标拖拽到Touch Bar上即可.] ...