当前位置: 首页 > news >正文

桂阳网站设计深圳公关公司

桂阳网站设计,深圳公关公司,网站建设技术文章,jquery 苹果网站提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、thinkphp中使用Elasticsearch 7.0进行多表的搜索二、使用步骤1.引入库2.读入数据 总结 前言 提示:thinkphp中使用Elasticsearch 7.0进行多表的…

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、thinkphp中使用Elasticsearch 7.0进行多表的搜索
  • 二、使用步骤
    • 1.引入库
    • 2.读入数据
  • 总结


前言

提示:thinkphp中使用Elasticsearch 7.0进行多表的搜索:

thinkphp数据库配置文件

 // Elasticsearch数据库配置信息'elasticsearch' => ['scheme' =>'http','host' => '127.0.0.1','port' => '9200','user' => '','pass' => '','timeout' => 2,],

提示:以下是本篇文章正文内容,下面案例可供参考

一、thinkphp中使用Elasticsearch 7.0进行多表的搜索

示例:thinkphp中使用Elasticsearch 7.0进行多表的搜索

二、使用步骤

1.引入库

直接上代码如下(示例):

composer require "elasticsearch/elasticsearch": "7.0.*"

2.读入数据

代码如下(示例):


namespace app\common\model;
use think\facade\Db;
use think\Model;
use Elasticsearch\ClientBuilder;
class Article extends Model
{protected $client;public function __construct($data = []){parent::__construct($data);try {	$this->client = ClientBuilder::create()->setHosts([config('database.connections.elasticsearch.host') . ':' . config('database.connections.elasticsearch.port')])->build();} catch (\Exception $e) {// 输出连接错误信息echo $e->getMessage();exit;}}// 添加文档到Elasticsearchpublic function addDocument(){$articles = Db::name('article')->select();foreach ($articles as $article) {$data = ['id' => $article['id'], // 文章表的 ID'title' => $article['title'],'content' => $article['content'],'category_id' => $article['category_id'], // 文章表的 ID];$params = ['index' => 'articles', // 索引名称'id' => $data['id'], // 文章 ID 作为文档的唯一标识'body' => $data,];$response = $this->client->index($params);}return $response;}// 搜索文档public function searchDocuments($index,$query){$params = ['index' => $index,'body' => ['query' => ['multi_match' => ['query' => $query,'fields' => ['title', 'content'],],],],];$response = $this->client->search($params);return $response;}
}
<?php
/*** Created by PhpStorm.* User: wangkxin@foxmail.com* Date: 2023/9/2* Time: 17:55*/namespace app\common\model;
use think\facade\Db;
use think\Model;
use Elasticsearch\ClientBuilder;class Book extends Model
{protected $client;public function __construct($data = []){parent::__construct($data);try {// $host = config('database.connections.elasticsearch.host');// $port = config('database.connections.elasticsearch.port');$this->client = ClientBuilder::create()->setHosts([config('database.connections.elasticsearch.host') . ':' . config('database.connections.elasticsearch.port')])->build();} catch (\Exception $e) {// 输出连接错误信息echo $e->getMessage();exit;}}// 添加文档到Elasticsearchpublic function addDocument(){$books = Db::name('book')->select();foreach ($books as $book) {$data = ['id' => $book['id'], // 书籍表的 ID'user_id' => $book['user_id'], // 书籍表作者ID'book' => $book['book'],];$params = ['index' => 'books', // 索引名称'id' => $data['id'], // 文章 ID 作为文档的唯一标识'body' => $data,];$response = $this->client->index($params);}return $response;}// 搜索文档public function searchDocuments($index,$query){$params = ['index' => $index,'body' => ['query' => ['multi_match' => ['query' => $query,'fields' => ['book'],],],],];$response = $this->client->search($params);return $response;}
}
<?php
/*** Created by PhpStorm.* User: wangkxin@foxmail.com* Date: 2023/9/2* Time: 15:27* 全局搜素模型*/namespace app\common\model;use think\Model;
use Elasticsearch\ClientBuilder;class ElasticsearchModel extends Model
{protected $client;public function __construct($data = []){parent::__construct($data);try {$this->client = ClientBuilder::create()->setHosts([config('database.connections.elasticsearch.host') . ':' . config('database.connections.elasticsearch.port')])->build();} catch (\Exception $e) {// 输出连接错误信息echo $e->getMessage();exit;}}public function globalSearch($keyword){// 搜索articles索引$articles = $this->searchIndex('articles', $keyword);// 搜索books索引$books = $this->searchIndex('books', $keyword);// 合并搜索结果$result = array_merge($articles, $books);return $result;}protected function searchIndex($index, $keyword){$params = ['index' => $index,'body' => ['query' => ['multi_match' => ['query' => $keyword,'fields' => ['title', 'content','book'],],],],];// 执行搜索请求$response = $this->client->search($params);// 解析结果$result = [];if (isset($response['hits']['hits'])) {foreach ($response['hits']['hits'] as $hit) {$result[] = $hit['_source'];$result['index'] = $index;}}return $result;}}
<?php
/*** Created by PhpStorm.* User: wangkxin@foxmail.com* Date: 2023/9/2* Time: 18:53*/namespace app\index\controller;use app\common\model\ElasticsearchModel;class SearchController
{//全局搜索个表间的数据public function search($keyword){$searchModel = new ElasticsearchModel();$result = $searchModel->globalSearch($keyword);return json($result);}
}
namespace app\index\controller;
use app\BaseController;
use app\common\model\Article as ElasticArticle;
use app\common\model\Book as ElasticBook;
use app\Request;
use Elasticsearch\ClientBuilder;class Demo1 extends BaseController
{
//新增索引,建议在模型中新增 ,删除, 修改 或者使用观察者模式更新ES索引public function addDocument(){$elasticsearchArticle = new ElasticArticle();$response = $elasticsearchArticle->addDocument();$elasticsearchBook = new ElasticBook();$response1 = $elasticsearchBook->addDocument();return json($response);// print_r(json($response));// print_r(json($response1));}/*** 单独搜索文章表例子*/public function search(Request $request){$keyword = $request->param('keyword');$elasticsearchModel = new ElasticArticle();$index = 'articles';$query = '你';$response = $elasticsearchModel->searchDocuments($index, $query);return json($response);}//单独搜搜书籍表public function searchBook(Request $request){$keyword = $request->param('keyword');$elasticsearchModel = new ElasticBook();$index = 'books';$query = '巴黎';$response = $elasticsearchModel->searchDocuments($index, $query);return json($response);}public function deleteIndex(){$client = ClientBuilder::create()->build();$params = ['index' => 'my_index', // 索引名称];$response = $client->indices()->delete($params);if ($response['acknowledged']) {return '索引删除成功';} else {return '索引删除失败';}}}

使用的表

CREATE TABLE `article` (`id` int(11) NOT NULL AUTO_INCREMENT,`category_id` int(11) DEFAULT NULL,`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,`content` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=107 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;CREATE TABLE `book` (`id` int(11) NOT NULL AUTO_INCREMENT,`user_id` int(11) DEFAULT NULL,`book` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;CREATE TABLE `elasticsearch_model` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',`model_name` varchar(255) NOT NULL DEFAULT '' COMMENT '模型名称',`index_name` varchar(255) NOT NULL DEFAULT '' COMMENT '索引名称',`created_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',`updated_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间',PRIMARY KEY (`id`),UNIQUE KEY `index_name_unique` (`index_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='Elasticsearch 模型配置表';

结果
在这里插入图片描述
在这里插入图片描述

windwos 上记住 安装 Elasticsearch 7.0 库, 和 kibana-7.0.0-windows-x86_64 图像管理工具
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结

提示:这是简单例子, 注意’fields’ => [‘title’, ‘content’], 尝试使用搜索number型字段,索引报错, 貌似只支持txt类型字段搜索
例如:以上就是今天要讲的内容,本文仅仅简单介绍了Elasticsearch的使用

http://www.hengruixuexiao.com/news/13796.html

相关文章:

  • 自己做的网站在浏览器上显示不安全中国北京出啥大事了
  • 微信2023新版下载seo网站排名优化教程
  • 个人网站备案怎么写厦门seo管理
  • wordpress roleseo 优化技术难度大吗
  • 网站数据库管理系统2023b站推广大全
  • 网站秒杀怎么做十大小说网站排名
  • 高网站建设山东网络优化公司排名
  • 阿里云建网站步骤网络营销学院
  • 网站为什么做等保如何让百度搜索到自己的网站
  • 做网站建设费用预算移动网站优化排名
  • seo怎么做最佳广州网站营销优化qq
  • 厦门做网站多个人网页生成器
  • 杭州网站建设设计公司百度地图导航网页版
  • 国税政务公开网站建设什么平台免费推广效果最好
  • 中国建设官方网站登录旅游网站的网页设计
  • 温州做网站什么是口碑营销
  • wordpress 浮动div南京市网站seo整站优化
  • wordpress 搭建wikiseo课程培训课程
  • 怎么样申请网站域名国际新闻 军事
  • 网站备案必须在公司注册地seo教程最新
  • 内网网站建设方面政策云南最新消息
  • 哪个网站做任务赚钱多百度旧版本下载
  • 网易企业邮箱怎么样优化方案官网电子版
  • 郑州东区网站建设扬州网络优化推广
  • 课程网站建设特色上海aso苹果关键词优化
  • 哪些网站是做外贸生意的浏览器地址栏怎么打开
  • 深圳模板网站多少钱aso优化推广
  • 长沙网站建计优化网站标题名词解释
  • 建设政府网站申请seo优化裤子关键词
  • 无锡做网站公司哪家比较好seo竞价培训