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 需要 ...
随机推荐
- Android下在onCreate中获取控件的宽度和高度(通过回调)
有时候需要在onCreate方法中知道某个View组件的宽度和高度等信息, 而直接调用View组件的getWidth().getHeight().getMeasuredWidth().getMeasu ...
- LA3485 Bridge[(辛普森自适应)微积分]
做此题完全是为了练积分. [普通求导版] Select Code #include<cstdio> #include<cmath> using namespace std; t ...
- ios 将图片做成圆形
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oiuyfdsa.png ...
- mysql如何用sql添加字段如何设置字符集和排序规则
alter table pay_company add sms_code2 varchar(16) CHARACTER SET UTF8 COLLATE utf8_general_ci DEFAULT ...
- <link>标签的rel属性全解析
<link>标签定义了当前文档与 Web 集合中其他文档的关系.link 元素是一个空元素,它仅包含属性.此元素只能存在于 head 部分,不过它可出现任何次数.在 HTML 中,&l ...
- 170419、Centos7下完美安装并配置mysql5.6
首先跟各位说声抱歉,原计划说每天一篇博文,最近由于实在太忙,封闭式开发一个项目,没有时间写博文,望大家见谅!!! 由于公司要搭建分布式服务,我把最近我所用到或者学习的技术或者遇到的问题跟大家分享一下! ...
- SharePoint 常用操作杂谈
前言 本文完全原创,转载请说明出处,希望对大家有用. 本篇博客是个人总结,一方面以便日后查看,另一方面希望能为其他人提供一些便利. 阅读目录 SharePoint 2010 UserProfile 添 ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- Servlet------>ServletConfig和ServletContext
原理图: 之一--------->servletConfig 有些时候,有些参数不适合写死,而且初始化servlet要用,可以通过这个头来调用servletConfig 例如:serlet数据库 ...
- git 学习(3)文件删除恢复
git学习(3) 撤销编辑 如果我们在编辑版本a的时候,如果在没有add之前,发现需要重新编辑版本a怎么办呢,可以通过git reset --hard comm_id, commit_id是版本a的提 ...