1 Star 0 Fork 0

water/goweb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
page_es_result.go 4.99 KB
一键复制 编辑 原始数据 按行查看 历史
leijmdas 提交于 2个月前 . add
package pagees
import (
"encoding/json"
"gitee.com/leijmdas/gobase/goconfig/common/base/baseconsts"
"gitee.com/leijmdas/gobase/goconfig/common/base/basedto"
"gitee.com/leijmdas/gobase/goconfig/common/base/jsonutils"
"gitee.com/leijmdas/goweb/gopage/page"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
)
/*
@Title 文件名称: pageesresponse.go
@Description 描述: es响应消息
@Author 作者: leijianming@163.com 时间(2024-02-22 22:38:21)
@Update 作者: leijianming@163.com 时间(2024-02-21 22:38:21)
*/
func PageEsResultOfSearch(SearchResult *elastic.SearchResult, attach bool) (pes *PageEsResult) {
return DefaultResult().PageEsResultOfSearch(SearchResult, attach)
}
type PageEsResult struct {
basedto.BaseEntity
page.PageResult
RequestId string `json:"request_id"`
SearchResult *elastic.SearchResult `json:"search_result,omitempty"`
}
func DefaultResult() *PageEsResult {
var result = &PageEsResult{}
result.InitProxy(result)
return result
}
func NewIchubPageEsResult() *PageEsResult {
return DefaultResult()
}
func FailedPageEsResultErr(err error) *PageEsResult {
var result = DefaultResult()
result.Msg = err.Error()
return result
}
func FailedPageEsResult(msg string) *PageEsResult {
var result = DefaultResult()
result.Msg = msg
return result
}
func (pes *PageEsResult) FromHitsHits(hits []*elastic.SearchHit) *PageEsResult {
return pes
}
func (pes *PageEsResult) Hits2Sources(hits []*elastic.SearchHit) *[]json.RawMessage {
var rawmsg = make([]json.RawMessage, 0)
for _, v := range hits {
rawmsg = append(rawmsg, v.Source)
}
return &rawmsg
}
func (pes *PageEsResult) PageEsResultData2Map() {
if pes.Data != nil {
var data = make([]map[string]interface{}, 0)
var datastr = jsonutils.ToJsonStr(pes.Data)
jsonutils.FromJsonByte([]byte(datastr), &data)
pes.Data = data
}
}
func (pes *PageEsResult) Hits2Map(indexmulti bool, hits []*elastic.SearchHit) []map[string]any {
var result = make([]map[string]any, 0)
for _, v := range hits {
var record = make(map[string]any, 0)
jsonutils.FromJsonByte(v.Source, &record)
if indexmulti {
record["index"] = v.Index
}
result = append(result, record)
}
return result
}
func (pes *PageEsResult) PageEsResultOf(request *PageEsRequest, searchRes *elastic.SearchResult) {
pes.PageSize = request.PageSize
pes.PageCurrent = request.PageCurrent
pes.RequestId = request.RequestId
pes.InitPage()
pes.Success()
if searchRes.Status != 0 {
pes.FailMsg(searchRes.Error.Reason)
pes.Data = make([]map[string]any, 0)
pes.Total = 0
return
}
pes.SearchResult = searchRes
pes.Data = pes.Hits2Map(request.IfIndexMulti(), searchRes.Hits.Hits)
pes.Total = int(searchRes.Hits.TotalHits.Value)
if len(searchRes.Hits.Hits) > 0 {
pes.DataHighlight = searchRes.Hits.Hits[0].Highlight
}
}
func (pes *PageEsResult) PageEsResultOfSearch(searchRes *elastic.SearchResult, attach bool) *PageEsResult {
pes.InitPage()
pes.Success()
if searchRes.Status != 0 {
pes.FailMsg(searchRes.Error.Reason)
return pes
}
if attach {
pes.SearchResult = searchRes
}
pes.DataAgg = searchRes.Aggregations
if searchRes.Hits != nil {
if len(searchRes.Hits.Hits) > 0 {
pes.DataHighlight = searchRes.Hits.Hits[0].Highlight
}
pes.Data = pes.Hits2Map(true, searchRes.Hits.Hits) //pes.Data = pes.Hits2Sources(searchRes.Hits.Hits)
pes.Total = int(searchRes.Hits.TotalHits.Value)
} else {
pes.Data = make(map[string]any, 0)
pes.Total = 0
}
return pes
}
func (pes *PageEsResult) FailMsg(Msg string) *PageEsResult {
pes.PageResult.FailMsg(Msg)
return pes
}
func (pes *PageEsResult) InitPage() {
if pes.PageSize <= baseconsts.PAGE_SIZE_ZERO {
pes.PageSize = baseconsts.PAGE_SIZE_DEFAULT
} else if pes.PageSize > baseconsts.PAGE_SIZE_MAX {
pes.PageSize = baseconsts.PAGE_SIZE_MAX
}
if pes.PageCurrent <= 0 {
pes.PageCurrent = baseconsts.PAGE_CURRENT
}
}
func (pes *PageEsResult) From(result *basedto.IchubResult) *PageEsResult {
jsonutils.FromJsonByte(result.ToJsonBytes(), pes)
return pes
}
func (pes *PageEsResult) FromJson(body []byte) interface{} {
jsonutils.FromJsonByte(body, pes)
return pes
}
func (pes *PageEsResult) As(out interface{}) *PageEsResult {
if pes.IsSuccess() && pes.Data != nil {
var bytes, _ = jsonutils.ToJsonBytes(pes.Data)
jsonutils.FromJsonByte(bytes, out)
pes.Data = out
} else {
logrus.Error(pes)
}
return pes
}
func (pes *PageEsResult) DataAsArrays() []map[string]interface{} {
return pes.Data.([]map[string]interface{})
}
func (pes *PageEsResult) DataLen() int {
return len(pes.DataAsArrays())
}
func (pes *PageEsResult) DataFields(i int, fieldName string) any {
return pes.DataAsArrays()[i][fieldName]
}
func (pes *PageEsResult) Init() {
pes.Code = 200
pes.Msg = "成功"
pes.Data = nil
}
func (pes *PageEsResult) Shutdown() {
// pes.Code = 200
// pes.Msg = "成功"
pes.Data = nil
}
func (pes *PageEsResult) ValueOfRequest(req *PageEsRequest) {
pes.RequestId = req.RequestId
pes.PageCurrent = req.PageCurrent
pes.PageSize = req.PageSize
pes.CmdType = req.CmdType
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/leijmdas/goweb.git
git@gitee.com:leijmdas/goweb.git
leijmdas
goweb
goweb
v1.2.7

搜索帮助