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