elasticsearch 工具类
<?php
/**
* Created by PhpStorm.
* User: brady
* Date: 2018/4/8
* Time: 10:31
* esticsearch helper
*/ require APPPATH.'third_party/vendor/autoload.php'; use Elasticsearch\ClientBuilder; class O_es
{
protected $db_name;
protected $tb_name;
protected $client;
protected $host; //初始化
public function init($db_name,$tb_name)
{ try{
$this->db_name = $db_name;
$this->tb_name = $tb_name;
$this->host = config_item('db')['host'];
if(ENVIRONMENT !== 'development'){
$host=['172.18.30.69:9200'];
$this->client = ClientBuilder::create()->setHosts($host)->build();
} else {
$this->client = ClientBuilder::create()->build();
} return $this->client;
} catch(Exception $e){
$msg = $e->getMessage();
echo json_encode(['success'=>false,'msg'=>$msg]);exit;
}
} public function add_doc($id,$body=[])
{
$params = [
'index' => $this->db_name,
'type' => $this->tb_name,
'id' => $id,
'body' => $body
];
$response = $this->client->index($params);
if( isset($response['_shards']['successful']) && $response['_shards']['successful'] >0){
return true;
} else {
return false;
}
} //根据id获取完整的搜索结果
public function get_doc($id)
{ try{
$params = [
'index' => $this->db_name,
'type' => $this->tb_name,
'id' => (string)$id
]; $response = $this->client->get($params);
return $response;
} catch(Exception $e){
$msg = $e->getMessage();
echo json_encode(['success'=>false,'msg'=>$msg]);exit;
} } //根据id获取搜索结果
public function get_doc_source($id)
{
$params = [
'index' => $this->db_name,
'type' => $this->tb_name,
'id' => $id
]; $response = $this->client->getSource($params);
return $response;
} //文档搜索
public function search_index($search_index,$search_val)
{
try { $params = [
'index' => $this->db_name,
'type' => $this->tb_name,
'body' => [
'query' => [
'match' => [
$search_index => $search_val
]
]
]
]; $response = $this->client->search($params);
if($response['hits']['total'] > 0) {
return $response['hits']['hits'];
} else {
return false;//未搜索到
} } catch(Exception $e){
$msg = $e->getMessage();
echo json_encode(['success'=>false,'msg'=>$msg]);exit;
}
} public function search_index_mul($field,$string,$type='dis_max',$tie_breaker = 0.3)
{ try { foreach($field as $v){
$querys[] = ['match'=>[$v=>$string]];
} switch($type)
{
case 'dis_max':
{
$body = [
'query'=>[
$type=>[
'queries'=>[ ],
'tie_breaker'=>$tie_breaker
]
]
];
$body['query'][$type]['queries'] = $querys;
}
break; case 'bool':
{
$body = [
'query'=>[
$type=>[
'should'=>[ ]
]
]
];
$body['query'][$type]['should'] = $querys;
}
break; default :
{
$body = [
'query'=>[
$type=>[
'queries'=>[ ]
]
]
];
$body['query'][$type]['queries'] = $querys;
}
break; } $params = [
'index' => $this->db_name,
'type' => $this->tb_name,
'body' => $body
];
$response = $this->client->search($params);
if($response['hits']['total'] > 0) {
return $response['hits']['hits'];
} else {
return [];//未搜索到
} } catch(Exception $e){
$msg = $e->getMessage();
echo json_encode(['success'=>false,'msg'=>$msg]);exit;
}
} //文档删除
public function del_doc($id)
{
try{
$params = [
'index' => $this->db_name,
'type' => $this->tb_name,
'id' => (string)$id
];
$response = $this->client->delete($params);
return $response;
} catch(Exception $e){
$msg = $e->getMessage();
echo json_encode(['success'=>false,'msg'=>$msg]);exit;
}
} public function add_index()
{
$params = [
'index' => $this->db_name,
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
]
]
]; $response = $this->client->indices()->create($params);
print_r($response);
} public function del_index()
{
$deleteParams = [
'index' => $this->db_name
];
$response = $this->client->indices()->delete($deleteParams);
return $response;
} }
$this->load->model("O_es");
$this->load->model("Tb_articles");
$this->O_es->init(config_item("db")['db'],'articles');
//$res = $this->Tb_articles->get_list(1,1000,'*','desc');
// $this->O_es->del_index();
// foreach($res as $v){
// $res = $this->O_es->add_doc($v['id'],$v);
// dump("索引".$v['id'].$res);
// }
// $res = $this->O_es->add_doc('1',['testField' => 'hello 天安门']);
//$res = $this->O_es->add_doc('1',['title' => 'Quick brown rabbits','body'=>'Brown rabbits are commonly seen.']);
// $res = $this->O_es->add_doc('2',['title' => 'Keeping pets healthy','body'=>'My quick brown fox eats rabbits on a regular basis.']);
//dump($res);
$list = $this->O_es->search_index_mul(['title','content'],'bootstrap');
if(!empty($list))
{
foreach($list as $v){
dump($v['_source']);
}
}
elasticsearch 工具类的更多相关文章
- ElasticSearch 工具类封装(基于ElasticsearchTemplate)
1.抽象接口定义 public abstract class SearchQueryEngine<T> { @Autowired protected ElasticsearchTempla ...
- elasticsearch java工具类
docker运行elasticsearch docker pull elasticsearch:7.8.1 docker run -p 9200:9200 -p 9300:9300 -e " ...
- es2.4.6 java api 工具类
网上找了很久没找到2.4.X 想要的java api 工具 自己写了一个,分享一下 导入所需的jar <!-- ElasticSearch begin --> <dependency ...
- Maven基础&&Spring框架阶段常用工具类整理
常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...
- elasticsearch通用工具类
这几天写了一个关于es的工具类,主要封装了业务中常用es的常用方法. 本文中使用到的elasticsearch版本6.7,但实际上也支持es7.x以上版本,因为主要是对springboot提供的:El ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
随机推荐
- vue入门: 实现选中并显示修改功能
1.实现功能 2.工具 vue 3.代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- JAVA面向对象思想理解分析
1.面向对象是面向过程而言.两者都是一种思想.面向过程:强调的是功能行为.(强调过程.动作)面向对象:将功能封装进对象,强调了具备了功能的对象.(强调对象.事物)面向对象是基于面向过程的.将复杂的事情 ...
- httpd的prefork、worker、event
Apache(httpd) 有3种核心MPM(Multi-Processing Module,多进程处理模块)工作模式,分别是prefork,worker和event,其中httpd-2.2的even ...
- 【mysql处理远程登陆授权及数据库迁移备份问题】
Database changedMariaDB [mysql]> grant all PRIVILEGES on mysql.* to root@'%' identified by '123'; ...
- wamp环境下安装imagick扩展
先上图,如下是安装成功后的phpinfo()界面: 安装步骤: 1.先确定安装版本,比如我的的php : php7.0.12 x86 ts 那么就需要三方版本 要一致:imagick软件本身( 如x ...
- Node.js(一)----安装
1.下载 地址 https://nodejs.org/en/download/ 注: 系统为ubuntu 下载的源码包 tar.gz 或者 wget https://nodejs.org/dist/v ...
- liunx下搭建python开发环境
=============================================================================注意: 在linux下安装新的版本的pytho ...
- python中一些内置函数实例
lambda表达式 简单函数可用lambda表达式 1. def f1() return(123) r1=f1() print() 2. f2=lambda:123 r2=f2() print() 以 ...
- Python自动化运维——IP地址处理模块
Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:IPy 功能:辅助我们高效的完成IP的规划工作 安装: wget https://pypi.python.o ...
- AtCoder AGC028-F:Reachable Cells
越来越喜欢AtCoder了,遍地都是神仙题. 题意: 给定一个\(N\)行\(N\)列的迷宫,每一个格子要么是障碍,要么是空地.每一块空地写着一个数码.在迷宫中,每一步只允许向右.向下走,且只能经过空 ...