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常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
随机推荐
- 【TOJ 1449】Area of Circles II(求不同位置的两圆面积之和)
描述 There are two circles on the plane. Now you must to calculate the area which they cover the plane ...
- poj_3256_Cow Picnic
The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ ...
- ABAP术语-Error Message
Error Message 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/30/1058283.html Information from ...
- WIN10 vagrant和virtualbox虚拟机和一键搭建lnmp环境配置thinkphp虚拟主机
版本:win10系统 virtualbox:5.1.26 vagrant :1.9.7 centos 7.0 xshell/git 首先下载好对应版本的软件 配置vagrant和virtualbox ...
- SI - 系统 - 操作系统简述 (Operating System)
Unix 操作系统:System V.BSD Microsoft Windows Apple Mac OS Linux FreeBSD 安装 https://jingyan.baidu.com/art ...
- wampserver 服务器报500错误,侦察小结
Internal Server Error The server encountered an internal error or misconfiguration and was unable to ...
- 如何解决thinkphp5中验证码常见问题?
对于thinkphp如何实现验证码,我这里就不介绍了.直接看之前的文章 http://www.cnblogs.com/qqblog/p/6639419.html.下面,我能想出来的是,我自己在开发过程 ...
- JNI模板
java为了调用底层驱动函数,需要调用外部的C/C++代码,java提供了JNI接口: 然后将C代码编译成库(windows下 .dll / android环境下 .so) arm-linux-gcc ...
- hive的load命令
Hive Load语句不会在加载数据的时候做任何转换工作,而是纯粹的把数据文件复制/移动到Hive表对应的地址. 语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVE ...
- Leecode刷题之旅-C语言/python-58.最后一个单词的长度
/* * @lc app=leetcode.cn id=58 lang=c * * [58] 最后一个单词的长度 * * https://leetcode-cn.com/problems/length ...