分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

thinkphp5 +elasticsearch

发布时间:2023-09-06 02:23责任编辑:彭小芳关键词:thinkphp

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved