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("").PostTags("") 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("").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("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) }