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); ???}}
thinkphp5 +elasticsearch
原文地址:https://www.cnblogs.com/zc123/p/9995994.html