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常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
随机推荐
- cookie与session的区别,你真的明白吗?
当我们访问网页时,http是属于无状态的,为什么呢?接下来由我慢慢讲解,在cookie的到来之前,你第一次访问页面的时候和最后一次访问页面服务器是不知道的,不知道那一次访问的页面是你.当用户登录的时候 ...
- meclipse6.5破解
package com.test.ssh.common; import java.text.DecimalFormat; import java.text.NumberFormat; import ...
- 2、开发环境搭建-window平台
一.搭建ReactNative环境 首先安装node.js和python2.xx版本,不要装python3.xx,这个官方是特别说明的,请注意.NodeJs官方下载:https://nodejs.or ...
- BZOJ1569: [JSOI2008]Blue Mary的职员分配(dp 暴力)
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 483 Solved: 189[Submit][Status][Discuss] Descriptio ...
- WebMagic 启动例子报错
报错内容: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/Http ...
- javascript--淘宝页面的放大镜效果
放大镜效果需求: 鼠标放入原图中,会出现一个黄色的遮盖层和一个放大的图片,鼠标移动时候,遮盖层会跟着鼠标一起移动,同时放大的图片会跟着一起移动. 实现过程: 1.鼠标移入,遮盖层和大图片显示 2.鼠标 ...
- JSP/Servlet开发——第一章 动态网页基础
1.动态网页:在服务端运行的使用程序语言设计的交互网页 : ●动态网站并不是指具有动画功能的网站,而是指网站内容可根据不同情况动态变更的网站(股票网站),一般情况下动态网站通过数据库进行架构. ●动态 ...
- 【ospf-路由过滤】
- JDK5后的特性整理
为了大家对JDK有一个全面的了解,下面是我从网上查找并整理了JDK5以后的所有关键新特性!(将会持续更新中) JDK5新特性 自动装箱与拆箱 枚举 静态导入 可变参数(Varargs) 内省(intr ...
- Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found
今天在发布应用的时候,出来了一个报错:An assembly specified in the application dependencies manifest was not found 情况如下 ...