Skip to content

Commit

Permalink
fulltext search config
Browse files Browse the repository at this point in the history
  • Loading branch information
phachon committed Jun 7, 2020
1 parent d59666e commit a8177f8
Show file tree
Hide file tree
Showing 17 changed files with 426 additions and 115 deletions.
11 changes: 2 additions & 9 deletions app/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ var (
version = flag.Bool("version", false, "mm-wiki version")

upgrade = flag.Bool("upgrade", false, "mm-wiki upgrade")
)

var (
Version = global.SYSTEM_VERSION

CopyRight = beego.Str2html(global.SYSTEM_COPYRIGHT)
Expand Down Expand Up @@ -219,7 +217,7 @@ func initDocumentDir() {
func checkUpgrade() {
if *upgrade == true {
logs.Info("Start checking whether MM-Wiki needs upgrading.")
versionConf, err := models.ConfigModel.GetConfigByKey(models.Config_Key_SystemVersion)
versionConf, err := models.ConfigModel.GetConfigByKey(models.ConfigKeySystemVersion)
if err != nil {
logs.Error("Get database mm-wiki version error: " + err.Error())
os.Exit(1)
Expand Down Expand Up @@ -274,10 +272,5 @@ func initSearch() {
}

func initWork() {
// 搜索索引 work
intervalTime, _ := beego.AppConfig.Int64("search::interval_time")
if intervalTime == 0 {
intervalTime = 30
}
work.InitDocSearchIndexWork(time.Duration(intervalTime) * time.Second)
work.DocSearchWorker.Start()
}
4 changes: 2 additions & 2 deletions app/controllers/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (this *AuthorController) Index() {

// is open auth login
ssoOpen := "0"
config, err := models.ConfigModel.GetConfigByKey(models.Config_Key_AuthLogin)
config, err := models.ConfigModel.GetConfigByKey(models.ConfigKeyAuthLogin)
if err == nil && len(config) > 0 && config["value"] == "1" {
ssoOpen = "1"
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func (this *AuthorController) AuthLogin() {
}

// is open auth login
config, err := models.ConfigModel.GetConfigByKey(models.Config_Key_AuthLogin)
config, err := models.ConfigModel.GetConfigByKey(models.ConfigKeyAuthLogin)
if err != nil || len(config) == 0 || config["value"] != "1" {
this.jsonError("系统未开启统一登录功能!")
}
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"fmt"
"github.com/phachon/mm-wiki/app/services"
"regexp"
"strings"

Expand Down Expand Up @@ -479,6 +480,11 @@ func (this *DocumentController) Delete() {
this.ErrorLog("删除文档 " + documentId + " 附件失败:" + err.Error())
}

// 删除文档索引
go func(documentId string) {
services.DocIndexService.ForceDelDocIdIndex(documentId)
}(documentId)

this.InfoLog("删除文档 " + documentId + " 成功")
this.jsonSuccess("删除文档成功", "", "/document/index?document_id="+document["parent_id"])
}
4 changes: 2 additions & 2 deletions app/controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ func (this *MainController) Default() {
// main title config
mainTitle := ""
mainDescription := ""
mainTitleConfig, err := models.ConfigModel.GetConfigByKey(models.Config_Key_MainTitle)
mainTitleConfig, err := models.ConfigModel.GetConfigByKey(models.ConfigKeyMainTitle)
if err != nil {
this.ErrorLog("查找 main_title 配置失败:" + err.Error())
} else {
if len(mainTitleConfig) > 0 {
mainTitle = mainTitleConfig["value"]
}
}
mainDescriptionConfig, err := models.ConfigModel.GetConfigByKey(models.Config_Key_MainDescription)
mainDescriptionConfig, err := models.ConfigModel.GetConfigByKey(models.ConfigKeyMainDescription)
if err != nil {
this.ErrorLog("查找 main_description 配置失败:" + err.Error())
} else {
Expand Down
18 changes: 12 additions & 6 deletions app/controllers/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package controllers
import (
"errors"
"fmt"
"github.com/astaxie/beego"
"github.com/phachon/mm-wiki/app"
"github.com/phachon/mm-wiki/app/models"
"github.com/phachon/mm-wiki/app/services"
"github.com/phachon/mm-wiki/app/utils"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
)

type PageController struct {
Expand Down Expand Up @@ -156,13 +158,13 @@ func (this *PageController) Edit() {
}

autoFollowDoc := "0"
autoFollowConfig, _ := models.ConfigModel.GetConfigByKey(models.Config_Key_AutoFollowDoc)
autoFollowConfig, _ := models.ConfigModel.GetConfigByKey(models.ConfigKeyAutoFollowdoc)
if len(autoFollowConfig) > 0 && autoFollowConfig["value"] == "1" {
autoFollowDoc = "1"
}

sendEmail := "0"
sendEmailConfig, _ := models.ConfigModel.GetConfigByKey(models.Config_Key_SendEmail)
sendEmailConfig, _ := models.ConfigModel.GetConfigByKey(models.ConfigKeySendEmail)
if len(sendEmailConfig) > 0 && sendEmailConfig["value"] == "1" {
sendEmail = "1"
}
Expand Down Expand Up @@ -272,16 +274,20 @@ func (this *PageController) Modify() {
logInfo["message"] = "更新文档时发送邮件通知失败:" + err.Error()
logInfo["level"] = models.Log_Level_Error
models.LogModel.Insert(logInfo)
beego.Error("更新文档时发送邮件通知失败:" + err.Error())
logs.Error("更新文档时发送邮件通知失败:" + err.Error())
}
}(documentId, this.User["username"], comment, url)
}
// follow doc
if isFollowDoc == "1" {
go func() {
models.FollowModel.FollowDocument(this.UserId, documentId)
_, _ = models.FollowModel.FollowDocument(this.UserId, documentId)
}()
}
// 更新文档索引
go func(documentId string) {
_ = services.DocIndexService.ForceUpdateDocIndexByDocId(documentId)
}(documentId)

this.InfoLog("修改文档 " + documentId + " 成功")
this.jsonSuccess("文档修改成功!", nil, "/document/index?document_id="+documentId)
Expand Down Expand Up @@ -460,7 +466,7 @@ func sendEmail(documentId string, username string, comment string, url string) e
}

// get send email open config
sendEmailConfig, err := models.ConfigModel.GetConfigByKey(models.Config_Key_SendEmail)
sendEmailConfig, err := models.ConfigModel.GetConfigByKey(models.ConfigKeySendEmail)
if err != nil {
return errors.New("发送邮件通知查找发送邮件配置失败:" + err.Error())
}
Expand Down
34 changes: 28 additions & 6 deletions app/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
const Table_Config_Name = "config"

const (
Config_Key_MainTitle = "main_title"
Config_Key_MainDescription = "main_description"
Config_Key_AutoFollowDoc = "auto_follow_doc_open"
Config_Key_SendEmail = "send_email_open"
Config_Key_AuthLogin = "sso_open"
Config_Key_SystemVersion = "system_version"
ConfigKeyMainTitle = "main_title"
ConfigKeyMainDescription = "main_description"
ConfigKeyAutoFollowdoc = "auto_follow_doc_open"
ConfigKeySendEmail = "send_email_open"
ConfigKeyAuthLogin = "sso_open"
ConfigKeySystemVersion = "system_version"
ConfigKeyFulltextSearch = "fulltext_search_open"
ConfigKeyDocSearchTimer = "doc_search_timer"
)

type Config struct {
Expand Down Expand Up @@ -81,6 +83,26 @@ func (c *Config) GetConfigs() (configs []map[string]string, err error) {
return
}

// get all configs key map
func (c *Config) GetConfigsKeyMap() (configMaps map[string]map[string]string, err error) {
configMaps = make(map[string]map[string]string)
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(
db.AR().From(Table_Config_Name))
if err != nil {
return
}
configs := rs.Rows()
for _, config := range configs {
key, ok := config["key"]
if ok {
configMaps[key] = config
}
}
return
}

// get config by many config_id
func (c *Config) GetConfigByConfigIds(configIds []string) (configs []map[string]string, err error) {
db := G.DB()
Expand Down
2 changes: 1 addition & 1 deletion app/models/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (f *Follow) GetFollowsByFollowIds(followIds []string) (follows []map[string
// create auto follow document
func (f *Follow) CreateAutoFollowDocument(userId string, documentId string) (id int64, err error) {

autoFollow, err := ConfigModel.GetConfigByKey(Config_Key_AutoFollowDoc)
autoFollow, err := ConfigModel.GetConfigByKey(ConfigKeyAutoFollowdoc)
if err != nil {
return 0, err
}
Expand Down
25 changes: 19 additions & 6 deletions app/models/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func (up *Upgrade) initHandleFunc() {
// v0.1.3 ~ v0.1.8
upgradeMap = append(upgradeMap, &upgradeHandle{Version: "v0.1.8", Func: up.v013ToV018})

// v0.1.8 ~ v0.2.1
//upgradeMap = append(upgradeMap, &upgradeHandle{Version: "v0.2.1", Func: up.v018ToV021})
// v0.1.8 ~ v0.2.0
upgradeMap = append(upgradeMap, &upgradeHandle{Version: "v0.2.0", Func: up.v018ToV020})

// v0.2.1 ~ v0.2.7
//upgradeMap = append(upgradeMap, &upgradeHandle{Version: "v0.2.7", Func: up.v021ToV027})
// v0.2.7 ~ v0.3.3
Expand Down Expand Up @@ -160,8 +161,20 @@ func (up *Upgrade) v013ToV018() error {
return nil
}

// upgrade v0.1.8 ~ v0.2.1
func (up *Upgrade) v018ToV021() error {
// upgrade v0.1.8 ~ v0.2.0
func (up *Upgrade) v018ToV020() error {
// 配置表增加数据
db := G.DB()
// 1. 配置表增加全文搜索开关
_, err := db.Exec(db.AR().Raw("INSERT INTO `mw_config` (config_id, name, key, value, create_time, update_time) VALUES ('开启全文搜索', 'fulltext_search_open', '1', unix_timestamp(now()), unix_timestamp(now()));"))
if err != nil {
return err
}
// 2. 配置表增加搜索索引时间间隔
_, err = db.Exec(db.AR().Raw("INSERT INTO `mw_config` (config_id, name, key, value, create_time, update_time) VALUES ('索引更新间隔', 'doc_search_timer', '3600', unix_timestamp(now()), unix_timestamp(now()));"))
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -195,7 +208,7 @@ func (up *Upgrade) createTable(sqlTable string) error {

func (up *Upgrade) upgradeAfter(version string) (err error) {
// update system version
config, err := ConfigModel.GetConfigByKey(Config_Key_SystemVersion)
config, err := ConfigModel.GetConfigByKey(ConfigKeySystemVersion)
if err != nil {
return
}
Expand All @@ -207,7 +220,7 @@ func (up *Upgrade) upgradeAfter(version string) (err error) {
}
_, err = ConfigModel.Insert(configValue)
} else {
_, err = ConfigModel.UpdateByKey(Config_Key_SystemVersion, version)
_, err = ConfigModel.UpdateByKey(ConfigKeySystemVersion, version)
}

return err
Expand Down
Loading

0 comments on commit a8177f8

Please sign in to comment.