🎏 finish search all #1
This commit is contained in:
parent
7ab3cfa81e
commit
5cf79f95d9
40
app/http/controller/web/search_controller.go
Normal file
40
app/http/controller/web/search_controller.go
Normal file
@ -0,0 +1,40 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/model"
|
||||
"catface/app/model_es"
|
||||
"catface/app/utils/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Search struct {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 全局搜索:AnmName + Encounter
|
||||
* @param {*gin.Context} context
|
||||
* @return {*}
|
||||
*/
|
||||
func (s *Search) SearchAll(context *gin.Context) {
|
||||
query := context.GetString(consts.ValidatorPrefix + "query")
|
||||
|
||||
var animals []model.Animal
|
||||
var encounters []model.Encounter
|
||||
|
||||
// 1. Animal Name // TODO 增加字段的过滤,看前端了。
|
||||
animals = model.CreateAnimalFactory("").ShowByName(query)
|
||||
|
||||
// 2. Encounter
|
||||
encounterIds, _ := model_es.CreateEncounterESFactory(nil).QueryDocumentsMatchAll(query)
|
||||
|
||||
if len(encounterIds) > 0 {
|
||||
encounters = model.CreateEncounterFactory("").ShowByIDs(encounterIds)
|
||||
}
|
||||
|
||||
response.Success(context, consts.CurdStatusOkMsg, gin.H{
|
||||
"animals": animals,
|
||||
"encounters": encounters,
|
||||
})
|
||||
}
|
@ -10,6 +10,7 @@ import (
|
||||
"catface/app/http/validator/web/encounter"
|
||||
"catface/app/http/validator/web/encounter_like"
|
||||
"catface/app/http/validator/web/nlp"
|
||||
"catface/app/http/validator/web/search"
|
||||
"catface/app/http/validator/web/users"
|
||||
)
|
||||
|
||||
@ -81,4 +82,8 @@ func WebRegisterValidator() {
|
||||
// TAG NLP
|
||||
key = consts.ValidatorPrefix + "NlpTitle"
|
||||
containers.Set(key, nlp.Title{})
|
||||
|
||||
// TAG Search
|
||||
key = consts.ValidatorPrefix + "SearchAll"
|
||||
containers.Set(key, search.SearchAll{})
|
||||
}
|
||||
|
30
app/http/validator/web/search/searchAll.go
Normal file
30
app/http/validator/web/search/searchAll.go
Normal file
@ -0,0 +1,30 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/http/controller/web"
|
||||
"catface/app/http/validator/core/data_transfer"
|
||||
"catface/app/utils/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SearchAll struct {
|
||||
Query string `form:"query" json:"query"`
|
||||
}
|
||||
|
||||
func (s SearchAll) CheckParams(context *gin.Context) {
|
||||
if err := context.ShouldBind(&s); err != nil {
|
||||
response.ValidatorError(context, err)
|
||||
return
|
||||
}
|
||||
|
||||
extraAddBindDataContext := data_transfer.DataAddContext(s, consts.ValidatorPrefix, context)
|
||||
if extraAddBindDataContext == nil {
|
||||
response.ErrorSystem(context, "Animal Create 表单验证器json化失败", "")
|
||||
} else {
|
||||
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
|
||||
(&web.Search{}).SearchAll(extraAddBindDataContext)
|
||||
}
|
||||
|
||||
}
|
@ -173,6 +173,20 @@ func (e *Encounter) ShowByID(id int64) (temp *Encounter, err error) {
|
||||
// return
|
||||
}
|
||||
|
||||
func (e *Encounter) ShowByIDs(ids []int64, attrs ...string) (temp []Encounter) {
|
||||
db := e.DB.Table(e.TableName())
|
||||
|
||||
if len(attrs) > 0 {
|
||||
db = db.Select(attrs)
|
||||
}
|
||||
|
||||
err := db.Where("id in (?)", ids).Find(&temp).Error
|
||||
if err != nil {
|
||||
variable.ZapLog.Error("Encounter ShowByIDs Error", zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 过去 1 个月,发送过路遇表的 ids,同时去重。
|
||||
* @param {*} user_id
|
||||
|
@ -14,6 +14,10 @@ import (
|
||||
)
|
||||
|
||||
func CreateEncounterESFactory(encounter *model.Encounter) *Encounter {
|
||||
if encounter == nil { // UPDATE 这样写好丑。
|
||||
return &Encounter{}
|
||||
}
|
||||
|
||||
// 我把数值绑定到了工厂创建当中。
|
||||
return &Encounter{
|
||||
Id: encounter.Id,
|
||||
@ -75,6 +79,7 @@ func (e *Encounter) InsertDocument() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO 改正,仿 Insert
|
||||
func (e *Encounter) UpdateDocument(client *elasticsearch.Client, encounter *Encounter) error {
|
||||
ctx := context.Background()
|
||||
|
||||
@ -123,7 +128,7 @@ func (e *Encounter) UpdateDocument(client *elasticsearch.Client, encounter *Enco
|
||||
* @param {string} query
|
||||
* @return {*} 对应 Encounter 的 id,然后交给 MySQL 来查询详细的信息?
|
||||
*/
|
||||
func (e *Encounter) QueryDocumentsMatchAll(client *elasticsearch.Client, query string) ([]int64, error) {
|
||||
func (e *Encounter) QueryDocumentsMatchAll(query string) ([]int64, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建查询请求
|
||||
@ -151,7 +156,7 @@ func (e *Encounter) QueryDocumentsMatchAll(client *elasticsearch.Client, query s
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
res, err := req.Do(ctx, client)
|
||||
res, err := req.Do(ctx, variable.ElasticClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -149,6 +149,11 @@ func InitWebRouter() *gin.Engine {
|
||||
{
|
||||
nlp.POST("title", validatorFactory.Create(consts.ValidatorPrefix+"NlpTitle"))
|
||||
}
|
||||
|
||||
search := backend.Group("search")
|
||||
{
|
||||
search.GET("", validatorFactory.Create(consts.ValidatorPrefix+"SearchAll"))
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user