Jelajahi Sumber

涌益咨询搜索

xyxie 1 tahun lalu
induk
melakukan
29e72f5772

+ 26 - 6
controllers/data_manage/yongyi_data.go

@@ -9,6 +9,7 @@ import (
 	"github.com/tealeg/xlsx"
 	"os"
 	"path/filepath"
+	"strings"
 	"time"
 )
 
@@ -152,15 +153,34 @@ func (this *EdbInfoController) YongyiSearchList() {
 		return
 	}
 
+	list := make([]*data_manage.BaseFromYongyiIndexSearchItem, 0)
+	var err error
 	//关键字
 	keyword := this.GetString("Keyword")
+	if keyword != "" {
+		keyWordArr := strings.Split(keyword, " ")
 
-	// todo es 模糊搜索
-	list, err := data_manage.GetYongyiItemList(keyword)
-	if err != nil {
-		br.ErrMsg = "获取失败,Err:" + err.Error()
-		br.Msg = "获取失败"
-		return
+		if len(keyWordArr) > 0 {
+			condition := ""
+			for _, v := range keyWordArr {
+				condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
+			}
+			list, err = data_manage.GetYongyiItemList(condition)
+			if err != nil {
+				br.ErrMsg = "获取失败,Err:" + err.Error()
+				br.Msg = "获取失败"
+				return
+			}
+		}
+
+	} else {
+		// todo es 模糊搜索
+		list, err = data_manage.GetYongyiItemList("")
+		if err != nil {
+			br.ErrMsg = "获取失败,Err:" + err.Error()
+			br.Msg = "获取失败"
+			return
+		}
 	}
 
 	br.Ret = 200

+ 19 - 2
models/data_manage/base_from_yongyi.go

@@ -34,6 +34,20 @@ type BaseFromYongyiIndexList struct {
 	Paging        *paging.PagingItem `description:"分页数据"`
 }
 
+type EsBaseFromYongyiIndex struct {
+	YongyiIndexId int64
+	IndexCode     string
+	IndexName     string
+	Frequency     string
+	Unit          string
+	ClassifyId    int64
+	StartDate     string
+	EndDate       string
+	TerminalCode  string
+	CreateTime    string
+	ModifyTime    string
+}
+
 type YongyiSingleDataResp struct {
 	YongyiIndexId int
 	ClassifyId    int
@@ -103,9 +117,12 @@ type BaseFromYongyiIndexSearchItem struct {
 }
 
 // GetYongyiItemList 模糊查询Yongyi数据库指标列表
-func GetYongyiItemList(keyword string) (items []*BaseFromYongyiIndexSearchItem, err error) {
+func GetYongyiItemList(condition string) (items []*BaseFromYongyiIndexSearchItem, err error) {
 	o := orm.NewOrmUsingDB("data")
-	sql := "SELECT * FROM base_from_yongyi_index WHERE CONCAT(index_name,index_code) LIKE '%" + keyword + "%'"
+	sql := "SELECT * FROM base_from_yongyi_index  WHERE 1=1"
+	if condition != "" {
+		sql += condition
+	}
 	_, err = o.Raw(sql).QueryRows(&items)
 	return
 }

+ 105 - 0
services/elastic/elastic.go

@@ -1504,3 +1504,108 @@ func SearchEdbInfoDataByAdminId(indexName, keywordStr string, from, size, filter
 
 	return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
 }
+
+// SearchBaseFromYongyiIndex 查询es中的涌溢咨询指标
+func SearchBaseFromYongyiIndex(indexName, keywordStr string, from, size int) (total int64, list []*data_manage.EsBaseFromYongyiIndex, err error) {
+	list = make([]*data_manage.EsBaseFromYongyiIndex, 0)
+	defer func() {
+		if err != nil {
+			fmt.Println("EsAddOrEditData Err:", err.Error())
+		}
+	}()
+	client := utils.EsClient
+
+	highlight := elastic.NewHighlight()
+	highlight = highlight.Fields(elastic.NewHighlighterField("IndexName"))
+	highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+
+	mustMap := make([]interface{}, 0)
+
+	//关键字匹配
+	shouldMap := map[string]interface{}{
+		"should": []interface{}{
+			map[string]interface{}{
+				"match": map[string]interface{}{
+					"IndexName": keywordStr,
+				},
+			},
+			// 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
+			map[string]interface{}{
+				"match": map[string]interface{}{
+					"IndexName": map[string]interface{}{
+						"query":    keywordStr,
+						"operator": "and",
+					},
+					//"Frequency.keyword": "月度",
+				},
+			},
+		},
+	}
+	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).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
+				}
+				item := new(data_manage.EsBaseFromYongyiIndex)
+				tmpErr = json.Unmarshal(itemJson, &item)
+				if err != nil {
+					fmt.Println("json.Unmarshal chartInfoJson err:", err)
+					err = tmpErr
+					return
+				}
+
+				list = append(list, item)
+				searchMap[v.Id] = v.Id
+			}
+		}
+	}
+	return
+}

+ 2 - 0
utils/config.go

@@ -110,6 +110,7 @@ var (
 	EsEnglishReportIndexName       string //英文研报ES索引
 	MY_CHART_INDEX_NAME            string //研究图库(MY ETA)索引
 	EsSemanticAnalysisDocIndexName string //ES语义分析文档索引名
+	EsIndexBaseFromYongyiIndex     string //数据源涌溢咨询指标
 	SmartReportIndexName           string //智能研报ES索引
 )
 
@@ -434,6 +435,7 @@ func init() {
 		EsEnglishReportIndexName = config["es_english_report_index_name"]
 		EsSemanticAnalysisDocIndexName = config["es_semantic_analysis_doc_index_name"]
 		SmartReportIndexName = config["es_smart_report_index_name"]
+		EsIndexBaseFromYongyiIndex = config["es_index_base_from_yongyi_index"]
 	}
 
 	CrmEtaServerUrl = config["crm_eta_server_url"]