php7使用elasticsearch

1、安装

官网下载地址:https://www.elastic.co/downloads/elasticsearch

# 解压到非root目录,运行时使用非root账号且必须安装java环境

yum install java

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz

tar zxvf elasticsearch-6.2.3.tar.gz

useradd elasticsearch

password elasticsearch

chown elasticsearch:elasticsearch elasticsearch-6.2.3

cd elasticsearch-6.2.3

nohup ./bin/elasticsearch  &   #设置成常驻进程

# php扩展库引入  composer.json

{  "require": {    // ...    "elasticsearch/elasticsearch": "~6.0"    // ...  } }

# 测试数据导入

create table articles(

id int not null primary key auto_increment,

title varchar(200) not null comment '标题',

content text comment '内容'

);

insert into articles(title, content) values ('Laravel 测试1', '我的宝马多少马力'),

('我的宝马发动机多少', '我的保时捷马力不错'),

('Laravel 测试3', '我的宝马发动机多少');

2、api使用

<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller; use cmf\controller\AdminBaseController;
use think\Db;
use Elasticsearch\ClientBuilder; /* author@zhou
* 功能:分词系统
* return
*/
class ElasticController extends AdminBaseController
{
/* author@zhou
* 功能:生成索引
* return
*/
public function index(){
try {
$db = new \PDO('mysql:host=127.0.0.1;dbname=thinkcmf5', 'root', 'd1560a683a5e0d82'); $sql = 'select * from articles';
$query = $db->prepare($sql);
$query->execute();
$lists = $query->fetchAll();
print_r($lists);
} catch (Exception $e) {
echo $e->getMessage();
} $client = ClientBuilder::create()->build();
foreach ($lists as $row) {
$params = [
'body' => [
'id' => $row['id'],
'title' => $row['title'],
'content' => $row['content']
],
'id' => 'article_' . $row['id'],
'index' => 'articles_index',
'type' => 'articles_type'
];
$client->index($params);
} } /* author@zhou
* 功能:获取索引
* return
*/
public function getIndex(){
$client = ClientBuilder::create()->build();
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
'id' => 'article_1'
];
$res = $client->get($params);
print_r($res);
} /* author@zhou
* 功能:从索引中删除文档
* return
*/
public function delIndex(){
$client = ClientBuilder::create()->build();
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
'id' => 'article_1'
];
$res = $client->delete($params);
print_r($res); } /* author@zhou
* 功能:设置索引
* return
*/
public function createIndex(){
$client = ClientBuilder::create()->build();
$params['index'] = 'articles_index';
$params['body']['settings']['number_of_shards'] = 2;
$params['body']['settings']['number_of_replicas'] = 0;
$client->indices()->create($params); } /* author@zhou
* 功能:查询条件
* return
*/
public function search(){ $client = ClientBuilder::create()->build();
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
]; //多字段匹配
// $params['body']['query']['multi_match']['query'] = '我的宝马发动机多少';
// $params['body']['query']['multi_match']['fields'] = ["title","content"];
// $params['body']['query']['multi_match']['type'] ="most_fields"; // most_fields 多字段匹配度更高 best_fields 完全匹配占比更高
//
// //单个字段匹配
// $params['body']['query']['match']['content'] = '我的宝马多少马力'; //完全匹配
// $params['body']['query']['match_phrase']['content'] = '我的宝马多少马力'; //联合搜索 must,should,must_not
$params['body']['query']["bool"]['must']["match"]['content'] = "宝马";
$params['body']['query']["bool"]['must_not']["match"]['title'] = "宝马"; $res = $client->search($params);
print_r($res); } }

最新文章

  1. Mac制作U盘系统(OS X El Capitan)教程
  2. 如何将网页的title前面的图标替换成自己的图标
  3. android avd sdk root
  4. 急!JDBC问题,发生通信错误。错误位置:Reply.fill()。消息:数据不足。 ERRORCODE=-4499, SQLSTATE=08001
  5. PHP中常用的字符串格式化函数总结
  6. (转)分布式深度学习系统构建 简介 Distributed Deep Learning
  7. mac配置java开发环境: jdk1.7 +sdk1.7+maven +tomcat
  8. BZOJ-1925 地精部落 烧脑DP+滚动数组
  9. Env:autojump安装使用
  10. LR_问题_无法打开IE浏览器、监视服务器资源
  11. python_Opencv_opencv2.4.4+python配置问题
  12. UVA - 11400 Lighting System Design (区间DP)
  13. 【CF235C】Cyclical Quest(后缀自动机)
  14. P2P技术简介
  15. wps中如何插入参考文献
  16. 关于” 记一次logback传输日志到logstash根据自定义设置动态创建ElasticSearch索引” 这篇博客相关的优化采坑记录
  17. 基于 Markdown 编写接口文档
  18. redhat 6.5 安装和配置zabbix客户端
  19. https://blog.csdn.net/uftjtt/article/details/79044186
  20. 大数据 时间同步问题 解决hbase集群节点HRegionServer启动后自动关闭

热门文章

  1. weex 小结
  2. postgres启动过程分析
  3. RecyclerViewDemo
  4. spring test---測试SpringMvc初识
  5. mac系统不同java版本切换
  6. 多媒体开发之---h264 取流解码分析
  7. Wordpress播客网站搭建
  8. a column-oriented DBMS
  9. rails elasticsearch searchkick用法
  10. Safair css hack