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

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 提供高效的全文搜索功能。代码结构清晰,易于维护,同时具备良好的扩展性和性能优化空间。


http://www.kler.cn/a/552936.html

相关文章:

  • 寒假总结与心得
  • 侯捷 C++ 课程学习笔记:设计模式在面向对象开发中的应用
  • Python 爬虫入门:从基础到实战
  • 修改项目的一些前端记录(自用)
  • MySQL-慢SQL解析及调试分析思路
  • 可变列二维数组【C语言】
  • 内网常见问题处理
  • java数据结构_优先级队列(堆)_6.1
  • 开源元搜索引擎SearXNG:使用Docker详细搭建部署与使用
  • 【OS安装与使用】part4-ubuntu22.04安装anaconda
  • 【R语言】绘图
  • ONNX Runtime 与 CUDA、cuDNN 的版本对应
  • “三次握手”与“四次挥手”:TCP传输控制协议连接过程
  • 在Kubernetes上部署DeepSeek-R1进行高效AI推理
  • C#```
  • 一文读懂Docker之Docker Compose
  • 论文笔记-WSDM2024-LLMRec
  • 02.19 构造函数
  • MYSQL数据库特殊查询-INFORMATION_SCHEMA
  • 鉴源实验室·智能网联汽车协议数据传输安全分析与防护