123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945 |
- 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("<font color='red'>").PostTags("</font>")
- //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("<font color='red'>").PostTags("</font>")
- //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("<font color='red'>").PostTags("</font>")
- 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("<font color='red'>").PostTags("</font>")
- //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
- }
|