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

GoFound 与 MySQL 集成优化方案

GoFound 与 MySQL 集成优化方案

1. 明确需求

  • 文章信息存储在 MySQL 数据库中。
  • 使用 GoFound 实现全文搜索功能。
  • 搜索时,先从 GoFound 中获取匹配的文章 ID,然后从 MySQL 中查询完整的文章信息。

2. 优化思路

  • 数据同步:将 MySQL 中的文章数据同步到 GoFound 中(文章 ID 和需要检索的字段,如标题、内容等)。
  • 搜索流程:用户输入搜索关键词,调用 GoFound 搜索接口,获取匹配的文章 ID 列表,然后从 MySQL 中查询完整的文章信息。
  • 性能优化:使用缓存(如 Redis)缓存热门搜索结果,对 GoFound 的搜索结果进行分页。

3. 代码优化实现

(1) 数据同步到 GoFound

在文章新增或更新时,将数据同步到 GoFound。

package service

import (
“github.com/newpanjing/gofound”
“gin-vue-admin/global”
“gin-vue-admin/model”
)

type ArticleService struct {
GoFoundClient *gofound.Client
}

func NewArticleService() *ArticleService {
return &ArticleService{
GoFoundClient: global.GoFoundClient,
}
}

// SyncArticleToGoFound 将文章同步到 GoFound
func (s *ArticleService) SyncArticleToGoFound(article *model.Article) error {
doc := gofound.Document{
ID: article.ID, // 文章 ID
Title: article.Title, // 文章标题
Content: article.Content, // 文章内容
}
return s.GoFoundClient.Add(doc)
}

(2) 搜索流程优化

在搜索时,先从 GoFound 获取文章 ID 列表,再从 MySQL 中查询完整信息。

package service

import (
“github.com/newpanjing/gofound”
“gin-vue-admin/global”
“gin-vue-admin/model”
)

type SearchService struct {
GoFoundClient *gofound.Client
}

func NewSearchService() *SearchService {
return &SearchService{
GoFoundClient: global.GoFoundClient,
}
}

// SearchArticles 搜索文章
func (s *SearchService) SearchArticles(query string, page, pageSize int) ([]model.Article, error) {
// 从 GoFound 中搜索
results, err := s.GoFoundClient.Search(query, pageSize, (page-1)*pageSize)
if err != nil {
return nil, err
}

// 提取文章 ID 列表
var articleIDs []uint
for _, result := range results {
    articleIDs = append(articleIDs, uint(result.ID))
}

// 从 MySQL 中查询完整文章信息
var articles []model.Article
if err := global.GVA_DB.Where("id IN ?", articleIDs).Find(&articles).Error; err != nil {
    return nil, err
}

return articles, nil

}

(3) 控制器调用搜索服务

在控制器中调用 SearchService,并返回搜索结果。

package api

import (
“github.com/gin-gonic/gin”
“gin-vue-admin/service”
“net/http”
“strconv”
)

type SearchApi struct {
SearchService *service.SearchService
}

func NewSearchApi() *SearchApi {
return &SearchApi{
SearchService: service.NewSearchService(),
}
}

func (s *SearchApi) SearchArticles(c *gin.Context) {
query := c.Query(“q”)
page, _ := strconv.Atoi(c.DefaultQuery(“page”, “1”))
pageSize, _ := strconv.Atoi(c.DefaultQuery(“pageSize”, “10”))

articles, err := s.SearchService.SearchArticles(query, page, pageSize)
if err != nil {
    c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    return
}

c.JSON(http.StatusOK, gin.H{"data": articles})

}

(4) 注册路由

在 router.go 中注册搜索路由。

package router

import (
“github.com/gin-gonic/gin”
“gin-vue-admin/api”
)

func InitRouter() *gin.Engine {
r := gin.Default()

searchApi := api.NewSearchApi()
r.GET("/search/articles", searchApi.SearchArticles)

return r

}

(5) 前端调用

在前端 Vue 项目中调用搜索接口,并展示结果。

<template>
  <div>
    <input v-model="query" @input="search" placeholder="Search articles...">
    <ul>
      <li v-for="article in articles" :key="article.id">
        <h3>{{ article.title }}</h3>
        <p>{{ article.content }}</p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
data() {
return {
query: ‘’,
articles: []
};
},
methods: {
async search() {
const response = await this.$http.get(‘/search/articles’, {
params: { q: this.query }
});
this.articles = response.data.data;
}
}
};
</script>

4. 性能优化建议

  • 缓存:使用 Redis 缓存热门搜索关键词及其结果,减少数据库和 GoFound 的压力。
  • 分页:在 GoFound 和 MySQL 查询中都支持分页,避免一次性加载过多数据。
  • 异步同步:使用消息队列(如 RabbitMQ 或 Kafka)异步同步 MySQL 数据到 GoFound,提高性能。
  • 索引优化:在 GoFound 中合理设置索引字段(如标题、内容),提升搜索效率。

5. 总结

通过以上优化,你可以实现从 MySQL 数据库中检索文章信息,并利用 GoFound 提供高效的全文搜索功能。代码结构清晰,易于维护,同时具备良好的扩展性和性能优化空间。

相关文章:

  • Coze扣子新功能详解
  • C++蓝桥杯基础篇(四)
  • RocketMQ消息是如何储存的?
  • 事务--实操演示
  • notepad++右键菜单不见了
  • Docker 私有仓库 Harbor 详解
  • Rust Slice(切片)类型
  • uniapp邪门事件
  • 【技术洞察】2024科技绘卷:浪潮、突破、未来
  • 算法刷题-哈希表的总结
  • CTF-RE: 基础模块-循环移位
  • scroll、offset、client三大家族和getBoundingClientRect方法
  • python concurrent.futures
  • 基于YOLO11深度学习的果园苹果检测与计数系统设计与实现【python源码+Pyqt5界面+数据集+训练代码】
  • 【Spring生命周期】Bean元信息配置阶段
  • Python Selenium自动化操作详解:从入门到实战
  • JavaScript中的函数基础知识
  • win10本地部署deepseek-r1步骤
  • 用户的声音 | 文档结构化信息提取方案测评:LLM、开源模型部署与云端API,谁是合适选择?
  • 在 Vue 项目中,为什么要在列表组件中写 key,其作用是什么?
  • 美政府被曝下令加强对格陵兰岛间谍活动,丹麦将召见美代办
  • 吴勇强、高颜已任南京市委常委
  • 2025江西跨境电子商务发展交流会召开,探索行业发展新趋势
  • 陕西澄城打造“中国樱桃第一县”:从黄土高原走向海外,年产值超30亿
  • 李干杰走访各民主党派中央和全国工商联机关
  • 碧桂园服务:拟向杨惠妍全资持有的公司提供10亿元贷款,借款将转借给碧桂园用作保交楼