123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package knowledge
- import (
- "context"
- "encoding/json"
- "eta_gn/eta_api/models/knowledge"
- "eta_gn/eta_api/utils"
- "fmt"
- "strconv"
- )
- // SearchChartInfoData 查询es中的图表数据
- func SearchKnowledgeResourceByEs(resourceType int, keywordStr string, showSysIds, classifyIds []int, sourceList []string, tagIds []int, isIncludeFile bool, from, size int) (list []*knowledge.KnowledgeResource, total int64, err error) {
- indexName := utils.EsKnowledgeResourceIndexName
- list = make([]*knowledge.KnowledgeResource, 0)
- defer func() {
- if err != nil {
- fmt.Println("SearchKnowledgeResource Err:", err.Error())
- utils.FileLog.Info("SearchKnowledgeResource Err:", err.Error())
- }
- }()
- client := utils.EsClient
- mustMap := make([]interface{}, 0)
- mustMap = append(mustMap, map[string]interface{}{
- "term": map[string]interface{}{
- "ResourceType": resourceType,
- },
- })
- //指标来源
- if len(showSysIds) > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "SysUserId": showSysIds,
- },
- })
- }
- if len(tagIds) > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "TagId": tagIds,
- },
- })
- }
- if len(sourceList) > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "SourceFrom": sourceList,
- },
- })
- }
- if len(classifyIds) > 0 {
- mustMap = append(mustMap, map[string]interface{}{
- "terms": map[string]interface{}{
- "ClassifyId": classifyIds,
- },
- })
- }
- mustMap = append(mustMap, map[string]interface{}{
- "term": map[string]interface{}{
- "IsDelete": 0,
- },
- })
- if !isIncludeFile {
- mustMap = append(mustMap, map[string]interface{}{
- "term": map[string]interface{}{
- "IsFile": 0,
- },
- })
- }
- shouldMap := map[string]interface{}{
- "should": []interface{}{
- map[string]interface{}{
- "match": map[string]interface{}{
- "Title": keywordStr,
- },
- },
- // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
- map[string]interface{}{
- "match": map[string]interface{}{
- "Title": map[string]interface{}{
- "query": keywordStr,
- "operator": "and",
- },
- },
- },
- },
- }
- mustMap = append(mustMap, map[string]interface{}{
- "bool": shouldMap,
- })
- queryMap := map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": mustMap,
- },
- },
- }
- //根据条件数量统计
- 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).Source(queryMap) // sets the JSON request
- 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
- }
- knowledgeItem := new(knowledge.KnowledgeResource)
- tmpErr = json.Unmarshal(itemJson, &knowledgeItem)
- if err != nil {
- fmt.Println("json.Unmarshal chartInfoJson err:", err)
- err = tmpErr
- return
- }
- list = append(list, knowledgeItem)
- searchMap[v.Id] = v.Id
- }
- }
- }
- return
- }
- // EsAddOrEditKnowledgeResource 新增/修改es中的知识资源数据
- func EsAddOrEditKnowledgeResource(item *knowledge.KnowledgeResource) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- utils.FileLog.Info("EsAddOrEditKnowledgeResource err:", err)
- }
- }()
- indexName := utils.EsKnowledgeResourceIndexName
- client := utils.EsClient
- itemJson, err := json.Marshal(item)
- if err != nil {
- fmt.Println("EsAddOrEditKnowledgeResource json.Marshal itemJson err:", err)
- return
- }
- request := client.Index().Index(indexName).Id(strconv.Itoa(item.KnowledgeResourceId)).BodyJson(itemJson)
- response, err := request.Do(context.Background())
- if err != nil {
- fmt.Println("add json:", string(itemJson))
- fmt.Println("EsAddOrEditKnowledgeResource err:", err)
- return
- }
- if response.Status == 0 {
- err = fmt.Errorf("add knowledge resource to es failed, response result is %s", response.Result)
- err = nil
- } else {
- fmt.Println("EsAddOrEditKnowledgeResource:", response.Status, response.Result)
- }
- return
- }
|