123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626 |
- package elastic
- import (
- "context"
- "encoding/json"
- "eta_gn/eta_api/models/data_manage"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/olivere/elastic/v7"
- )
- // AddOrEditChartInfoToEsData 新增/修改es中的图表数据
- func AddOrEditChartInfoToEsData(indexName, docId string, item *data_manage.ChartEsInfo) (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
- }
- fmt.Println(resp)
- if resp.Status == 0 {
- fmt.Println("新增成功", resp.Result)
- err = nil
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- return
- }
- func SearchChartInfoData(keywordStr string, showSysId int, sourceList []int, noPermissionChartIdList []int, from, size int) (list []*data_manage.ChartInfo, total int64, err error) {
- indexName := utils.CHART_INDEX_NAME
- list = make([]*data_manage.ChartInfo, 0)
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditData 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("ChartName"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- mustMap := make([]interface{}, 0)
- mustNotMap := make([]interface{}, 0)
- //图表来源
- if showSysId > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "term": map[string]interface{}{
- "SysUserId": showSysId,
- //"Frequency.keyword": "月度",
- },
- })
- }
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "Source": sourceList,
- },
- })
- //关键字匹配
- //shouldMap := map[string]interface{}{
- // "should": []interface{}{
- // map[string]interface{}{
- // "match": map[string]interface{}{
- // "ChartName": keywordStr,
- // //"Frequency.keyword": "月度",
- // },
- // },
- // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
- // map[string]interface{}{
- // "match": map[string]interface{}{
- // "ChartName": map[string]interface{}{
- // "query": keywordStr,
- // "operator": "and",
- // },
- // //"Frequency.keyword": "月度",
- // },
- // },
- // map[string]interface{}{
- // "match": map[string]interface{}{
- // "ChartNameEn": keywordStr,
- // //"Frequency.keyword": "月度",
- // },
- // },
- // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
- // map[string]interface{}{
- // "match": map[string]interface{}{
- // "ChartNameEn": map[string]interface{}{
- // "query": keywordStr,
- // "operator": "and",
- // },
- // //"Frequency.keyword": "月度",
- // },
- // },
- // },
- //}
- // 默认使用中文名字字段去匹配
- keywordNameKey := `ChartName`
- // 如果没有中文,则使用英文名称字段去匹配
- if !utils.ContainsChinese(keywordStr) {
- keywordNameKey = `ChartNameEn`
- }
- shouldMap := map[string]interface{}{
- "should": []interface{}{
- map[string]interface{}{
- "match": map[string]interface{}{
- keywordNameKey: keywordStr,
- //"Frequency.keyword": "月度",
- },
- },
- // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
- map[string]interface{}{
- "match": map[string]interface{}{
- keywordNameKey: map[string]interface{}{
- "query": keywordStr,
- "operator": "and",
- },
- //"Frequency.keyword": "月度",
- },
- },
- },
- }
- mustMap = append(mustMap, map[string]interface{}{
- "bool": shouldMap,
- })
- // noPermissionChartIdList 无权限图表id
- if len(noPermissionChartIdList) > 0 {
- mustNotMap = append(mustNotMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "ChartInfoId": noPermissionChartIdList,
- //"Frequency.keyword": "月度",
- },
- })
- }
- 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
- jsonBytes, _ := json.Marshal(queryMap)
- fmt.Println(string(jsonBytes))
- 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
- }
- 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
- }
- chartInfoItem := new(data_manage.ChartInfo)
- tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
- if err != nil {
- fmt.Println("json.Unmarshal chartInfoJson err:", err)
- err = tmpErr
- return
- }
- if len(v.Highlight["ChartName"]) > 0 {
- chartInfoItem.ChartName = v.Highlight["ChartName"][0]
- }
- list = append(list, chartInfoItem)
- searchMap[v.Id] = v.Id
- }
- }
- }
- //for _, v := range result {
- // fmt.Println(v)
- //}
- return
- }
- func SearchChartInfoDataByShared(keywordStr string, from, size, chartShare int, sourceList, chartTypeList, classifyIdList []int, chartAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.ChartInfoView, err error) {
- indexName := utils.CHART_INDEX_NAME
- list = make([]*data_manage.ChartInfoView, 0)
- defer func() {
- if err != nil {
- fmt.Println("SearchChartInfoData 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...))
- }
- //图表类型
- if len(chartTypeList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range chartTypeList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartType", termsList...))
- }
- // classifyIdList 指定分类下的图表
- if len(classifyIdList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range classifyIdList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartClassifyId", termsList...))
- }
- // 如果指定了分享状态,那么就添加分享状态的筛选
- // 0:全部,1:未共享,2:已共享
- switch chartShare {
- case 1:
- // 筛选 SharedUserIdList 为空的文档
- query = query.MustNot(elastic.NewExistsQuery("SharedUserIdList"))
- case 2:
- // 筛选 SharedUserIdList 不为空的文档
- query = query.Must(elastic.NewExistsQuery("SharedUserIdList"))
- }
- // 图表名称、编码匹配
- if keywordStr != `` {
- // 默认使用中文名字字段去匹配
- keywordNameKey := `ChartName`
- query = query.Must(elastic.NewMatchQuery(keywordNameKey, keywordStr))
- }
- // 图表与用户的权限匹配
- {
- //图表权限范围,0-全部;1-我的;2-公共
- switch chartAuth {
- 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 searchChartInfoData(indexName, query, sortList, from, size)
- }
- func SearchChartInfoDataByPublic(keywordStr string, from, size int, chartPublicList, sourceList, chartTypeList, classifyIdList, publicClassifyIdList []int, chartAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.ChartInfoView, err error) {
- indexName := utils.CHART_INDEX_NAME
- list = make([]*data_manage.ChartInfoView, 0)
- defer func() {
- if err != nil {
- fmt.Println("SearchChartInfoData 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...))
- }
- // 图表类型
- if len(chartTypeList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range chartTypeList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartType", termsList...))
- }
- // classifyIdList 指定分类下的图表
- if len(classifyIdList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range classifyIdList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartClassifyId", termsList...))
- }
- // publicClassifyIdList 指定公共分类下的图表
- if len(publicClassifyIdList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range publicClassifyIdList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartPublicClassifyId", termsList...))
- }
- // 如果指定了图表公开状态,那么就添加图表公开状态的筛选
- // 公开状态;0:未公开;1:审批中;2:已驳回;3:已公开
- if len(chartPublicList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range chartPublicList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("PublicStatus", termsList...))
- }
- // 图表名称、编码匹配
- if keywordStr != `` {
- // 默认使用中文名字字段去匹配
- keywordNameKey := `ChartName`
- query = query.Must(elastic.NewMatchQuery(keywordNameKey, keywordStr))
- }
- // 图表与用户的权限匹配
- {
- //图表权限范围,0-全部;1-我的;2-公共
- switch chartAuth {
- 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 searchChartInfoData(indexName, query, sortList, from, size)
- }
- // searchChartInfoData 查询es中的图表数据
- func searchChartInfoData(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*data_manage.ChartInfoView, err error) {
- total, err = searchChartInfoDataTotal(indexName, query)
- if err != nil {
- return
- }
- // 获取列表数据
- list, err = searchChartInfoDataList(indexName, query, sortList, from, size)
- if err != nil {
- return
- }
- return
- }
- // searchChartInfoDataTotal 查询es中的图表数量
- func searchChartInfoDataTotal(indexName string, query elastic.Query) (total int64, err error) {
- defer func() {
- if err != nil {
- fmt.Println("searchChartInfoDataTotal Err:", err.Error())
- }
- }()
- client := utils.EsClient
- //根据条件数量统计
- requestTotalHits := client.Count(indexName).Query(query)
- total, err = requestTotalHits.Do(context.Background())
- if err != nil {
- return
- }
- return
- }
- // searchChartInfoData 查询es中的图表数据
- func searchChartInfoDataList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*data_manage.ChartInfoView, err error) {
- list = make([]*data_manage.ChartInfoView, 0)
- defer func() {
- if err != nil {
- fmt.Println("searchChartInfoDataList Err:", err.Error())
- }
- }()
- client := utils.EsClient
- // 高亮
- //highlight := elastic.NewHighlight()
- //highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
- //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("MarshalJSON err:", err)
- return
- }
- chartInfoItem := new(data_manage.ChartInfoView)
- tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
- if err != nil {
- fmt.Println("json.Unmarshal chartInfoJson err:", err)
- err = tmpErr
- return
- }
- if len(v.Highlight["ChartName"]) > 0 {
- chartInfoItem.ChartName = v.Highlight["ChartName"][0]
- }
- list = append(list, chartInfoItem)
- searchMap[v.Id] = v.Id
- }
- }
- }
- return
- }
- // SearchChartInfoDataV2 查询es中的图表数据
- func SearchChartInfoDataV2(keywordStr string, showSysId, chartAuth int, sourceList []int, collectChartInfoIdList, searchClassifyIdList, searchPublicClassifyIdList []int, sortMap map[string]string, from, size int) (total int64, list []*data_manage.ChartInfoView, err error) {
- indexName := utils.CHART_INDEX_NAME
- list = make([]*data_manage.ChartInfoView, 0)
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditData 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...))
- }
- // collectChartInfoIdList 指定的图表id
- if len(collectChartInfoIdList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range collectChartInfoIdList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartInfoId", termsList...))
- }
- // classifyIdList 指定分类下的图表
- if len(searchClassifyIdList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range searchClassifyIdList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartClassifyId", termsList...))
- }
- // publicClassifyIdList 指定公共分类下的图表
- if len(searchPublicClassifyIdList) > 0 {
- termsList := make([]interface{}, 0)
- for _, v := range searchPublicClassifyIdList {
- termsList = append(termsList, v)
- }
- query = query.Must(elastic.NewTermsQuery("ChartPublicClassifyId", termsList...))
- }
- // 图表名称、编码匹配
- if keywordStr != `` {
- // 默认使用中文名字字段去匹配
- keywordNameKey := `ChartName`
- query = query.Must(elastic.NewMatchQuery(keywordNameKey, keywordStr))
- }
- // 图表与用户的权限匹配
- {
- //图表权限范围,0-全部;1-我的;2-公共
- switch chartAuth {
- case 1:
- // 自己的图表
- query = query.Must(elastic.NewTermQuery(`SysUserId`, showSysId))
- case 2:
- // 公开的图表
- query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
- default:
- tmpShouldQuery := elastic.NewBoolQuery()
- // 自己的图表
- tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, showSysId))
- // 分享给我的图表
- tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, showSysId))
- //公开的图表
- 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 searchChartInfoData(indexName, query, sortList, from, size)
- }
|