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

网站建设项目进度计划书购物车网站建设

网站建设项目进度计划书,购物车网站建设,找合伙做网站的,小说网站排名前十提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、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.yayakq.cn/news/194885/

相关文章:

  • 做网站云主机php网站的数据库在哪
  • 什么求职网站可以做几份简历网络服务器搭建与配置
  • 住房和城乡建设网站方案网站怎么做用户体验
  • 做房产网站有哪些网站建设的技巧
  • 手机网站建站教育模板长沙网站制作哪家专业
  • 织梦网站模板长沙企业推广
  • 有没有学做ppt发网站或论坛深圳办公室租金多少钱一平
  • 关于产品网站建设的问题广东seo加盟
  • 中国空间站航天员首次出舱泉州模板做网站
  • 做网站一定要用服务器吗设计工作室名字
  • html网站开发图片素材北京短视频制作
  • 北京网站备案注销大连招投标网官网
  • 郑州做网站公司排名保险网站建设平台
  • python 网站开发流程wordpress安装文件夹
  • 被骗去国外做网站网站推广网站用户群
  • 专注网站建设微信开发wordpress 在线人数
  • 学校网站建设管理制度什么是域名访问网站
  • 出名的建站网站怎么引流推广自己的产品
  • 网站上动态图片怎么做网站设计素材网站推荐
  • 汉中市住房和城乡建设局网站上海设计公司招聘信息
  • 河南移动官网网站建设wordpress flatsome
  • 淘宝做网站为什么那么便宜一个网站多个数据库
  • 2345网址导航app官网下载永康网站优化
  • 陕西交通建设集团公司网站com一级域名
  • 官方网站在哪里搜狗识图
  • 建设银行网站能买手机做关于车的网站
  • 网站分为几部分外包加工网官网下载
  • 建设部网站1667号公告在百度上怎么卖自己的产品
  • 未及时取消网站备案广西南宁做网站
  • 专业的图纸设计网站网站免费创建