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 需要 ...
随机推荐
- std::unique_lock
/*与Mutex RAII相关,方便线程上锁,相比std::lock_guard提供了更好的上锁解锁控制,反正我是没看出来也是在构造时上锁,在析构时解锁,感觉和lock_gurad大差不差都是在线程函 ...
- ps -aux | egrep 多个值
ps -aux |egrep "(schedule.jar|positec.jar|time_server.jar|tomcat-xweb/)"
- HDU 3333 Turing Tree (线段树)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- JS获取时间戳+C#水煎戳转换
JS获取了当前毫秒的时间戳. var timestamp=new Date().getTime(); //第二种方法: //var timestamp = (new Date()).valueOf() ...
- Xcode6 部署iphone4s出现的问题 No architectures to compile for
手贱升级到了Xcode6.结果一打开来就爆了各种错误换个警告; 各种百度和谷歌,终于搞定了,然后插上4S,结果还是报错. 解决: 1.找到 Build Settings 2.点击 Architectu ...
- centos 安装 Vmare tool
增强工具的作用 和host共享文件夹 鼠标自动捕捉释放 高分屏 自适应窗口 1.打开centOs虚机进入图形界面,点击工具栏上的虚拟机->重新安装WMare Tools 2.挂载光驱 查询光驱完 ...
- pipreqs
安装:pip3 install pipreqs 作用:帮你检测当前程序所有的安装模块,并输入到requirements.txt 执行:pipreqs ./ (必须在当前程序目录下) pycharm会 ...
- 网络编程 - UDP协议
UDP协议 服务端 ''' UDP 协议 又称 数据报协议 SOCK_DGRAM ''' from socket import * # 一般不这样做 会重名 但写socket可以这样写 因为要用到太多 ...
- ledecode Reverse Words in a String III
557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...
- 005-Shell echo命令
一.概述 Shell 的 echo 指令,用于字符串的输出.命令格式: echo string 可以使用echo实现更复杂的输出格式控制. 1.显示普通字符串: echo "It is a ...