package elastic
import (
"context"
"encoding/json"
"eta_gn/eta_api/models/data_manage"
"eta_gn/eta_api/utils"
"fmt"
"github.com/olivere/elastic/v7"
)
// EsAddOrEditEdbInfoData 新增/修改es中的指标数据
func EsAddOrEditEdbInfoData(indexName, docId string, item *data_manage.EdbInfoEs) (err error) {
defer func() {
if err != nil {
fmt.Println("EsAddOrEditData Err:", err.Error())
}
}()
client := utils.EsClient
resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
if err != nil {
fmt.Println("新增失败:", err.Error())
return err
}
if resp.Status == 0 {
fmt.Println("新增成功", resp.Result)
err = nil
} else {
fmt.Println("AddData", resp.Status, resp.Result)
}
return
}
// searchEdbInfoData 查询es中的指标数据
func searchEdbInfoData(indexName string, mustMap, mustNotMap []interface{}, shouldMapList []map[string]interface{}, sortList []interface{}, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
list = make([]*data_manage.EdbInfoList, 0)
defer func() {
if err != nil {
fmt.Println("searchEdbInfoData Err:", err.Error())
}
}()
client := utils.EsClient
//queryString := elastic.NewQueryStringQuery(keywordStr)
//boolQueryJson, err := json.Marshal(queryString)
//if err != nil {
// fmt.Println("boolQueryJson err:", err)
//} else {
// fmt.Println("boolQueryJson ", string(boolQueryJson))
//}
highlight := elastic.NewHighlight()
highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
highlight = highlight.PreTags("").PostTags("")
//query := elastic.RawStringQuery(`{"match_all":{}}`)
//关键字匹配
for _, shouldMap := range shouldMapList {
mustMap = append(mustMap, map[string]interface{}{
"bool": shouldMap,
})
}
queryMap := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": mustMap,
"must_not": mustNotMap,
//"should": shouldMap,
},
},
}
//根据条件数量统计
requestTotalHits := client.Count(indexName).BodyJson(queryMap)
total, err = requestTotalHits.Do(context.Background())
if err != nil {
return
}
queryMap["from"] = from
queryMap["size"] = size
// 如果有指定排序,那么就按照排序来
if len(sortList) > 0 {
queryMap["sort"] = sortList
}
jsonBytes, _ := json.Marshal(queryMap)
fmt.Println(string(jsonBytes))
//queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
//request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
//requestJson, err := json.Marshal(request)
//if err != nil {
// fmt.Println("requestJson err:", err)
//}
//fmt.Println("requestJson ", string(requestJson))
searchMap := make(map[string]string)
searchResp, err := request.Do(context.Background())
if err != nil {
return
}
fmt.Println(searchResp)
fmt.Println(searchResp.Status)
if searchResp.Status != 0 {
return
}
//total = searchResp.TotalHits()
if searchResp.Hits != nil {
for _, v := range searchResp.Hits.Hits {
if _, ok := searchMap[v.Id]; !ok {
itemJson, tmpErr := v.Source.MarshalJSON()
if tmpErr != nil {
err = tmpErr
fmt.Println("movieJson err:", err)
return
}
edbInfoItem := new(data_manage.EdbInfoList)
tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
if tmpErr != nil {
fmt.Println("json.Unmarshal movieJson err:", tmpErr)
err = tmpErr
return
}
if len(v.Highlight["EdbCode"]) > 0 {
edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
}
if len(v.Highlight["EdbName"]) > 0 {
edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
}
list = append(list, edbInfoItem)
searchMap[v.Id] = v.Id
}
}
}
return
}
// searchGeneralEdbInfoDataTotal
// @Description: 查询es中的指标数量(通用)
// @author: Roc
// @datetime 2024-12-23 11:17:35
// @param indexName string
// @param mustMap []interface{}
// @param mustNotMap []interface{}
// @param shouldMapList []map[string]interface{}
// @return total int64
// @return err error
func searchGeneralEdbInfoDataTotal(indexName string, mustMap, mustNotMap []interface{}, shouldMapList []map[string]interface{}) (total int64, err error) {
defer func() {
if err != nil {
fmt.Println("searchGeneralEdbInfoDataTotal Err:", err.Error())
}
}()
client := utils.EsClient
//关键字匹配
for _, shouldMap := range shouldMapList {
mustMap = append(mustMap, map[string]interface{}{
"bool": shouldMap,
})
}
queryMap := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": mustMap,
"must_not": mustNotMap,
//"should": shouldMap,
},
},
}
//根据条件数量统计
requestTotalHits := client.Count(indexName).BodyJson(queryMap)
total, err = requestTotalHits.Do(context.Background())
return
}
// searchGeneralEdbInfoDataList
// @Description: 查询es中的指标明细数据(通用)
// @author: Roc
// @datetime 2024-12-23 11:18:04
// @param indexName string
// @param mustMap []interface{}
// @param mustNotMap []interface{}
// @param shouldMapList []map[string]interface{}
// @param sortList []interface{}
// @param from int
// @param size int
// @return list []*data_manage.EdbInfoList
// @return err error
func searchGeneralEdbInfoDataList(indexName string, mustMap, mustNotMap []interface{}, shouldMapList []map[string]interface{}, sortList []interface{}, from, size int) (list []*data_manage.EdbInfoList, err error) {
list = make([]*data_manage.EdbInfoList, 0)
defer func() {
if err != nil {
fmt.Println("searchGeneralEdbInfoDataList Err:", err.Error())
}
}()
client := utils.EsClient
highlight := elastic.NewHighlight()
highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
highlight = highlight.PreTags("").PostTags("")
//query := elastic.RawStringQuery(`{"match_all":{}}`)
//关键字匹配
for _, shouldMap := range shouldMapList {
mustMap = append(mustMap, map[string]interface{}{
"bool": shouldMap,
})
}
queryMap := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": mustMap,
"must_not": mustNotMap,
//"should": shouldMap,
},
},
}
queryMap["from"] = from
queryMap["size"] = size
// 如果有指定排序,那么就按照排序来
if len(sortList) > 0 {
queryMap["sort"] = sortList
}
jsonBytes, _ := json.Marshal(queryMap)
fmt.Println(string(jsonBytes))
//queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
//request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
//requestJson, err := json.Marshal(request)
//if err != nil {
// fmt.Println("requestJson err:", err)
//}
//fmt.Println("requestJson ", string(requestJson))
searchMap := make(map[string]string)
searchResp, err := request.Do(context.Background())
if err != nil {
return
}
fmt.Println(searchResp)
fmt.Println(searchResp.Status)
if searchResp.Status != 0 {
return
}
//total = searchResp.TotalHits()
if searchResp.Hits != nil {
for _, v := range searchResp.Hits.Hits {
if _, ok := searchMap[v.Id]; !ok {
itemJson, tmpErr := v.Source.MarshalJSON()
if tmpErr != nil {
err = tmpErr
fmt.Println("movieJson err:", err)
return
}
edbInfoItem := new(data_manage.EdbInfoList)
tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
if tmpErr != nil {
fmt.Println("json.Unmarshal movieJson err:", tmpErr)
err = tmpErr
return
}
if len(v.Highlight["EdbCode"]) > 0 {
edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
}
if len(v.Highlight["EdbName"]) > 0 {
edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
}
list = append(list, edbInfoItem)
searchMap[v.Id] = v.Id
}
}
}
return
}
// SearchEdbInfoData
// @Description: 查询es中的指标数据
// @author: Roc
// @datetime 2024-11-29 10:22:25
// @param keywordStr string
// @param from int
// @param size int
// @param filterSource int
// @param source int
// @param frequencyList []string
// @param notFrequencyList []string
// @param noPermissionEdbInfoIdList []int
// @param noPermissionEdbClassifyIdList []int
// @param collectEdbInfoIdList []int
// @param edbTypeList []int
// @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
// @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
// @param sysUserId int
// @return total int64
// @return list []*data_manage.EdbInfoList
// @return err error
func SearchEdbInfoData(keywordStr string, from, size, filterSource, source int, frequencyList, notFrequencyList []string, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) {
indexName := utils.DATA_INDEX_NAME
list = make([]*data_manage.EdbInfoList, 0)
defer func() {
if err != nil {
fmt.Println("SearchEdbInfoData Err:", err.Error())
}
}()
highlight := elastic.NewHighlight()
highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
highlight = highlight.PreTags("").PostTags("")
mustMap := make([]interface{}, 0)
mustNotMap := make([]interface{}, 0)
switch filterSource {
case 2:
mustMap = append(mustMap, map[string]interface{}{
"term": map[string]interface{}{
"Frequency.keyword": "月度",
},
})
case 3:
case 4:
mustMap = append(mustMap, map[string]interface{}{
"term": map[string]interface{}{
"EdbType": 1,
},
})
case 5:
mustMap = append(mustMap, map[string]interface{}{
"term": map[string]interface{}{
"Source": 6,
},
})
case 6:
mustMap = append(mustMap, map[string]interface{}{
"match": map[string]interface{}{
"Frequency.keyword": "年度",
},
})
}
//指标来源
if source > 0 {
mustMap = append(mustMap, map[string]interface{}{
"term": map[string]interface{}{
"Source": source,
},
})
}
if len(frequencyList) > 0 {
mustMap = append(mustMap, map[string]interface{}{
"terms": map[string]interface{}{
"Frequency.keyword": frequencyList,
},
})
}
if len(notFrequencyList) > 0 {
mustNotMap = append(mustNotMap, map[string]interface{}{
"terms": map[string]interface{}{
"Frequency.keyword": notFrequencyList,
},
})
}
// noPermissionEdbInfoIdList 无权限指标id
if len(noPermissionEdbInfoIdList) > 0 {
mustNotMap = append(mustNotMap, map[string]interface{}{
"terms": map[string]interface{}{
"EdbInfoId": noPermissionEdbInfoIdList,
},
})
}
// noPermissionEdbInfoIdList 无权限指标id
if len(noPermissionEdbClassifyIdList) > 0 {
mustNotMap = append(mustNotMap, map[string]interface{}{
"terms": map[string]interface{}{
"ClassifyId": noPermissionEdbClassifyIdList,
},
})
}
// collectEdbInfoIdList 收藏的指标id
if len(collectEdbInfoIdList) > 0 {
mustMap = append(mustMap, map[string]interface{}{
"terms": map[string]interface{}{
"EdbInfoId": collectEdbInfoIdList,
},
})
}
// searchClassifyIdList 指定分类id列表
if len(searchClassifyIdList) > 0 {
mustMap = append(mustMap, map[string]interface{}{
"terms": map[string]interface{}{
"ClassifyId": searchClassifyIdList,
},
})
}
// searchPublicClassifyIdList 指定公共分类id列表
if len(searchPublicClassifyIdList) > 0 {
mustMap = append(mustMap, map[string]interface{}{
"terms": map[string]interface{}{
"EdbPublicClassifyId": searchPublicClassifyIdList,
},
})
}
// 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
if len(edbTypeList) > 0 {
mustMap = append(mustMap, map[string]interface{}{
"terms": map[string]interface{}{
"EdbType": edbTypeList,
},
})
}
if edbInfoType >= 0 {
mustMap = append(mustMap, map[string]interface{}{
"term": map[string]interface{}{
"EdbInfoType": edbInfoType,
},
})
}
shouldMapList := make([]map[string]interface{}, 0)
// 指标名称、编码匹配
if keywordStr != `` {
// 默认使用中文名字字段去匹配
keywordNameKey := `EdbName`
shouldMap := map[string]interface{}{
"should": []interface{}{
map[string]interface{}{
"match": map[string]interface{}{
"EdbCode": keywordStr,
},
},
map[string]interface{}{
"match": map[string]interface{}{
keywordNameKey: keywordStr,
},
},
},
}
shouldMapList = append(shouldMapList, shouldMap)
}
// 指标与用户的权限匹配
{
shouldTermList := make([]map[string]interface{}, 0)
//指标权限范围,0-全部;1-我的;2-公共
switch edbAuth {
case 1:
// 自己的指标
shouldTermList = append(shouldTermList, map[string]interface{}{
"term": map[string]interface{}{
"SysUserId": sysUserId,
},
})
case 2:
// 公开的指标
shouldTermList = append(shouldTermList, map[string]interface{}{
"term": map[string]interface{}{
"PublicStatus": utils.DataPublicSuccess,
},
})
default:
// 自己的指标
shouldTermList = append(shouldTermList, map[string]interface{}{
"term": map[string]interface{}{
"SysUserId": sysUserId,
},
})
// 分享给我的指标
shouldTermList = append(shouldTermList, map[string]interface{}{
"terms": map[string]interface{}{
"SharedUserIdList": []int{sysUserId},
},
})
// 公开的指标
shouldTermList = append(shouldTermList, map[string]interface{}{
"term": map[string]interface{}{
"PublicStatus": utils.DataPublicSuccess,
},
})
}
// 如果是包含了数据查看(基础指标)的搜索,那么就需要加上这个条件;否则只看自己的指标+共享指标+公开指标
if edbInfoType == 0 && utils.InArrayByInt(edbTypeList, 1) {
// 基础指标(数据查看)
shouldTermList = append(shouldTermList, map[string]interface{}{
"term": map[string]interface{}{
"EdbType": 1,
},
}, map[string]interface{}{
"term": map[string]interface{}{
"EdbInfoType": 0,
},
})
}
shouldMap := map[string]interface{}{
"should": shouldTermList,
}
shouldMapList = append(shouldMapList, shouldMap)
}
// 排序
sortList := make([]interface{}, 0)
// 如果没有关键字,那么就走指标id倒序
for orderKey, orderType := range sortMap {
sortEdbInfoId := map[string]interface{}{
orderKey: map[string]interface{}{
"order": orderType,
},
}
sortList = append(sortList, sortEdbInfoId)
}
total, err = searchGeneralEdbInfoDataTotal(indexName, mustMap, mustNotMap, shouldMapList)
if err != nil {
return
}
list, err = searchGeneralEdbInfoDataList(indexName, mustMap, mustNotMap, shouldMapList, sortList, from, size)
return
}
// searchEdbInfoDataTotal
// @Description: 查询es中的指标数量
// @author: Roc
// @datetime 2024-12-23 11:19:04
// @param indexName string
// @param query elastic.Query
// @return total int64
// @return err error
func searchEdbInfoDataTotal(indexName string, query elastic.Query) (total int64, err error) {
defer func() {
if err != nil {
fmt.Println("searchEdbInfoDataTotal Err:", err.Error())
}
}()
client := utils.EsClient
//根据条件数量统计
requestTotalHits := client.Count(indexName).Query(query)
total, err = requestTotalHits.Do(context.Background())
if err != nil {
return
}
return
}
// searchEdbInfoDataList
// @Description: 查询es中的指标明细数据
// @author: Roc
// @datetime 2024-12-23 11:18:48
// @param indexName string
// @param query elastic.Query
// @param sortList []*elastic.FieldSort
// @param from int
// @param size int
// @return list []*data_manage.EdbInfoList
// @return err error
func searchEdbInfoDataList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*data_manage.EdbInfoList, err error) {
list = make([]*data_manage.EdbInfoList, 0)
defer func() {
if err != nil {
fmt.Println("searchEdbInfoDataList Err:", err.Error())
}
}()
client := utils.EsClient
// 高亮
//highlight := elastic.NewHighlight()
//highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
//highlight = highlight.PreTags("").PostTags("")
//request := client.Search(indexName).Highlight(highlight).From(from).Size(size) // sets the JSON request
request := client.Search(indexName).From(from).Size(size) // sets the JSON request
// 如果有指定排序,那么就按照排序来
if len(sortList) > 0 {
for _, v := range sortList {
request = request.SortBy(v)
}
}
searchMap := make(map[string]string)
searchResp, err := request.Query(query).Do(context.Background())
if err != nil {
return
}
//fmt.Println(searchResp)
//fmt.Println(searchResp.Status)
if searchResp.Status != 0 {
return
}
//total = searchResp.TotalHits()
if searchResp.Hits != nil {
for _, v := range searchResp.Hits.Hits {
if _, ok := searchMap[v.Id]; !ok {
itemJson, tmpErr := v.Source.MarshalJSON()
if tmpErr != nil {
err = tmpErr
fmt.Println("movieJson err:", err)
return
}
edbInfoItem := new(data_manage.EdbInfoList)
tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
if tmpErr != nil {
fmt.Println("json.Unmarshal movieJson err:", tmpErr)
err = tmpErr
return
}
if len(v.Highlight["EdbCode"]) > 0 {
edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
}
if len(v.Highlight["EdbName"]) > 0 {
edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
}
list = append(list, edbInfoItem)
searchMap[v.Id] = v.Id
}
}
}
return
}
// searchEdbInfoDataV2 查询es中的指标数据
func searchEdbInfoDataV2(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
total, err = searchEdbInfoDataTotal(indexName, query)
if err != nil {
return
}
// 获取列表数据
list, err = searchEdbInfoDataList(indexName, query, sortList, from, size)
if err != nil {
return
}
return
}
// SearchEdbInfoDataByShared
// @Description: 查询es中的指标数据
// @author: Roc
// @datetime 2024-11-29 10:22:25
// @param keywordStr string
// @param from int
// @param size int
// @param filterSource int
// @param source int
// @param frequency string
// @param noPermissionEdbInfoIdList []int
// @param noPermissionEdbClassifyIdList []int
// @param collectEdbInfoIdList []int
// @param edbTypeList []int
// @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
// @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
// @param sysUserId int
// @return total int64
// @return list []*data_manage.EdbInfoList
// @return err error
func SearchEdbInfoDataByShared(keywordStr string, from, size, edbShare int, sourceList, classifyIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) {
indexName := utils.DATA_INDEX_NAME
list = make([]*data_manage.EdbInfoList, 0)
defer func() {
if err != nil {
fmt.Println("SearchEdbInfoData Err:", err.Error())
}
}()
query := elastic.NewBoolQuery()
//指标来源
if len(sourceList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range sourceList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("Source", termsList...))
}
// classifyIdList 指定分类下的指标
if len(classifyIdList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range classifyIdList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
}
// 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
if len(edbTypeList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range edbTypeList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
}
// 如果指定了分享状态,那么就添加分享状态的筛选
// 0:全部,1:未共享,2:已共享
switch edbShare {
case 1:
// 筛选 SharedUserIdList 为空的文档
query = query.MustNot(elastic.NewExistsQuery("SharedUserIdList"))
case 2:
// 筛选 SharedUserIdList 不为空的文档
query = query.Must(elastic.NewExistsQuery("SharedUserIdList"))
}
if edbInfoType >= 0 {
query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
}
// 指标名称、编码匹配
if keywordStr != `` {
// 默认使用中文名字字段去匹配
keywordNameKey := `EdbName`
query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
}
// 指标与用户的权限匹配
{
//指标权限范围,0-全部;1-我的;2-公共
switch edbAuth {
case 1:
// 自己的指标
query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
case 2:
// 公开的指标
query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
default:
tmpShouldQuery := elastic.NewBoolQuery()
// 自己的指标
tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
// 分享给我的指标
tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
//公开的指标
tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
//shouldQuery = shouldQuery.Should(tmpShouldQuery)
query = query.Must(tmpShouldQuery)
}
}
// 排序
sortList := make([]*elastic.FieldSort, 0)
// 如果没有关键字,那么就走指标id倒序
for orderKey, orderType := range sortMap {
switch orderType {
case "asc":
sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
case "desc":
sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
}
}
return searchEdbInfoDataV2(indexName, query, sortList, from, size)
}
// SearchEdbInfoDataByPublic
// @Description: 查询es中的指标数据
// @author: Roc
// @datetime 2024-12-05 13:33:36
// @param keywordStr string
// @param from int
// @param size int
// @param edbPublicList []int
// @param sourceList []int
// @param classifyIdList []int
// @param publicClassifyIdList []int
// @param edbTypeList []int
// @param edbInfoType int
// @param edbAuth int
// @param sysUserId int
// @param sortMap map[string]string
// @return total int64
// @return list []*data_manage.EdbInfoList
// @return err error
func SearchEdbInfoDataByPublic(keywordStr string, from, size int, edbPublicList, sourceList, classifyIdList, publicClassifyIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) {
indexName := utils.DATA_INDEX_NAME
list = make([]*data_manage.EdbInfoList, 0)
defer func() {
if err != nil {
fmt.Println("SearchEdbInfoData Err:", err.Error())
}
}()
query := elastic.NewBoolQuery()
//指标来源
if len(sourceList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range sourceList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("Source", termsList...))
}
// classifyIdList 指定分类下的指标
if len(classifyIdList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range classifyIdList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
}
// publicClassifyIdList 指定公共分类下的指标
if len(publicClassifyIdList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range publicClassifyIdList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("EdbPublicClassifyId", termsList...))
}
// 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
if len(edbTypeList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range edbTypeList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
}
// 如果指定了指标公开状态,那么就添加指标公开状态的筛选
// 公开状态;0:未公开;1:审批中;2:已驳回;3:已公开
if len(edbPublicList) > 0 {
termsList := make([]interface{}, 0)
for _, v := range edbPublicList {
termsList = append(termsList, v)
}
query = query.Must(elastic.NewTermsQuery("PublicStatus", termsList...))
}
if edbInfoType >= 0 {
query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
}
// 指标名称、编码匹配
if keywordStr != `` {
// 默认使用中文名字字段去匹配
keywordNameKey := `EdbName`
query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
}
// 指标与用户的权限匹配
{
//指标权限范围,0-全部;1-我的;2-公共
switch edbAuth {
case 1:
// 自己的指标
query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
case 2:
// 公开的指标
query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
default:
tmpShouldQuery := elastic.NewBoolQuery()
// 自己的指标
tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
// 分享给我的指标
tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
//公开的指标
tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
//shouldQuery = shouldQuery.Should(tmpShouldQuery)
query = query.Must(tmpShouldQuery)
}
}
// 排序
sortList := make([]*elastic.FieldSort, 0)
// 如果没有关键字,那么就走指标id倒序
for orderKey, orderType := range sortMap {
switch orderType {
case "asc":
sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
case "desc":
sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
}
}
return searchEdbInfoDataV2(indexName, query, sortList, from, size)
}
// EsDeleteEdbInfoData 删除es中的指标数据
func EsDeleteEdbInfoData(indexName, docId string) (err error) {
defer func() {
if err != nil {
fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
}
}()
client := utils.EsClient
resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
fmt.Println(resp)
if err != nil {
return
}
if resp.Status == 0 {
fmt.Println("删除成功")
} else {
fmt.Println("AddData", resp.Status, resp.Result)
}
return
}