rdluck 4 years ago
parent
commit
3231087c2c
3 changed files with 138 additions and 122 deletions
  1. 1 2
      controllers/search.go
  2. 60 15
      services/elasticsearch.go
  3. 77 105
      services/industry_map.go

+ 1 - 2
controllers/search.go

@@ -27,7 +27,6 @@ func (this *SearchController) SearchList() {
 		br.ErrMsg = "请输入搜索词"
 		return
 	}
-	pageSize := 20
 	user := this.User
 	if user == nil {
 		br.Msg = "请重新登录"
@@ -47,7 +46,7 @@ func (this *SearchController) SearchList() {
 		categoryNameArr = append(categoryNameArr, v.CategoryName)
 	}
 	categoryName := strings.Join(categoryNameArr, ",")
-	result, err := services.SearchByKeyWord(keyWord, categoryName, pageSize)
+	result, err := services.SearchByKeyWord(keyWord, categoryName)
 	if err != nil {
 		br.Msg = "检索失败"
 		br.ErrMsg = "检索失败,Err:" + err.Error()

+ 60 - 15
services/elasticsearch.go

@@ -9,10 +9,6 @@ import (
 	"html"
 	"strconv"
 	"strings"
-
-	//"gopkg.in/olivere/elastic.v5"
-	//"gopkg.in/olivere/elastic.v5/config"
-
 	"github.com/PuerkitoBio/goquery"
 	"github.com/olivere/elastic/v7"
 	"github.com/olivere/elastic/v7/config"
@@ -151,24 +147,73 @@ func SaveData() {
 	fmt.Println("end")
 }
 
-func SearchByKeyWord(keyWord,categoryName string, pageSize int) (result []*models.SearchItem, err error) {
-	fmt.Println("get start keyWordArr")
+func SearchByKeyWord(keyWord, categoryName string) (result []*models.SearchItem, err error) {
+	pageSize := 20
 	keyWordArr, err := GetIndustryMapNameSlice(keyWord)
 	if err != nil {
 		go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "GetIndustryMapNameSlice:"+err.Error(), utils.EmailSendToUsers)
 	}
+	var sniff = false //<4>
+	cfg := &config.Config{
+		URL:      ES_URL,
+		Username: ES_USERNAME,
+		Password: ES_PASSWORD,
+	}
+
+	cfg.Sniff = &sniff
+	client, err := elastic.NewClientFromConfig(cfg)
+	if err != nil {
+		return
+	}
+	var esIndex = "cygx_article"
 
-	for k, v := range keyWordArr {
-		fmt.Println(k, v)
-		go func() {
-			esSearch(v,"")
-		}()
+	termsQuery := elastic.NewTermsQuery("category_name", categoryName)
+	searchMap := make(map[int]int)
+	for _, v := range keyWordArr {
+		keyWord = v
+		boolquery := elastic.NewBoolQuery()
+		boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
+		highlight := elastic.NewHighlight()
+		highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
+		highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+		searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(termsQuery).Query(boolquery).Do(context.Background())
+		if err != nil {
+			return result, err
+		}
+		if searchByMatch.Hits != nil {
+			for _, v := range searchByMatch.Hits.Hits {
+				articleJson, err := v.Source.MarshalJSON()
+				if err != nil {
+					return nil, err
+				}
+				article := new(models.CygxArticle)
+				err = json.Unmarshal(articleJson, &article)
+				if err != nil {
+					return nil, err
+				}
+				if _, ok := searchMap[article.ArticleId]; !ok {
+					searchItem := new(models.SearchItem)
+					searchItem.ArticleId, _ = strconv.Atoi(v.Id)
+					searchItem.Body = v.Highlight["BodyText"]
+					var title string
+					if len(v.Highlight["Title"]) > 0 {
+						title = v.Highlight["Title"][0]
+					} else {
+						title = article.Title
+					}
+					searchItem.Title = title
+					searchItem.PublishDate = article.PublishDate
+					result = append(result, searchItem)
+					searchMap[article.ArticleId] = article.ArticleId
+				}
+			}
+		}
 	}
 	return
 }
 
-func esSearch(keyWord,categoryName string)(result []*models.SearchItem, err error) {
-	pageSize:=20
+func esSearch(keyWord, categoryName string) (result []*models.SearchItem, err error) {
+	pageSize := 20
 	var sniff = false //<4>
 	cfg := &config.Config{
 		URL:      ES_URL,
@@ -183,7 +228,7 @@ func esSearch(keyWord,categoryName string)(result []*models.SearchItem, err erro
 	}
 	var esIndex = "cygx_article"
 
-	termsQuery:=elastic.NewTermsQuery("category_name",categoryName)
+	termsQuery := elastic.NewTermsQuery("category_name", categoryName)
 
 	boolquery := elastic.NewBoolQuery()
 	boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
@@ -225,4 +270,4 @@ func esSearch(keyWord,categoryName string)(result []*models.SearchItem, err erro
 		}
 	}
 	return
-}
+}

+ 77 - 105
services/industry_map.go

@@ -1,17 +1,13 @@
 package services
 
 import (
-	"encoding/json"
-	"fmt"
 	"hongze/hongze_cygx/models"
-	"hongze/hongze_cygx/utils"
 	"math/rand"
 	"reflect"
 	"sort"
 	"time"
 )
 
-
 func MakeTree(allNode []*models.CygxIndustryMapItems, node *models.CygxIndustryMapItems) {
 	childs, _ := haveChild(allNode, node) //判断节点是否有子节点并返回
 	if len(childs) > 0 {
@@ -37,76 +33,59 @@ func haveChild(allNode []*models.CygxIndustryMapItems, node *models.CygxIndustry
 	return
 }
 
-
 func GetIndustryMap() {
-	fmt.Println("start")
 	list, err := models.GetCygxIndustryMapByParentId(0)
 	if err != nil {
-		fmt.Println("GetCygxIndustryMapByParentId Err:",err.Error())
 		return
 	}
 	rootNode := list[0]
 	allNodes, err := models.GetCygxIndustryMapAll()
 	if err != nil {
-		fmt.Println("GetCygxIndustryMapAll Err:"+err.Error())
 		return
 	}
 	MakeTree(allNodes, rootNode)
-	resultJson,err:=json.Marshal(rootNode)
-	fmt.Println("Err:",err)
-	utils.FileLog.Info("allNodes:%s",string(resultJson))
-	fmt.Println("end")
 }
 
-
-func GetIndustryTree()(rootNode *models.CygxIndustryMapItems, err error) {
-	fmt.Println("start")
+func GetIndustryTree() (rootNode *models.CygxIndustryMapItems, err error) {
 	list, err := models.GetCygxIndustryMapByParentId(0)
 	if err != nil {
-		fmt.Println("GetCygxIndustryMapByParentId Err:",err.Error())
 		return
 	}
 	rootNode = list[0]
 	allNodes, err := models.GetCygxIndustryMapAll()
 	if err != nil {
-		fmt.Println("GetCygxIndustryMapAll Err:"+err.Error())
 		return
 	}
 	MakeTree(allNodes, rootNode)
-	resultJson,err:=json.Marshal(rootNode)
-	fmt.Println("Err:",err)
-	fmt.Println("allNodes:",string(resultJson))
-	//utils.FileLog.Info("allNodes:%s",string(resultJson))
-	fmt.Println("end")
 	return
 }
 
 //获取对应的层级map
-func getMap(tree *models.CygxIndustryMapItems,item *models.CygxIndustryMapItems,hasIdMap map[int]string,otherChildMapSlice map[int][]*models.CygxIndustryMapItems){
+func getMap(tree *models.CygxIndustryMapItems, item *models.CygxIndustryMapItems, hasIdMap map[int]string, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) {
 	//深度
 	depth := 1
 
 	//获取下级
-	childTree := getChildTree(tree,item)
-	depth = childTreeToSlice(childTree,hasIdMap,depth,otherChildMapSlice)
+	childTree := getChildTree(tree, item)
+	depth = childTreeToSlice(childTree, hasIdMap, depth, otherChildMapSlice)
 
 	//获取上级
 	var tmpParentSlice []*models.CygxIndustryMapItems
-	tmpParentSlice,_ = parentTreeToSlice(tree,item,0,tmpParentSlice,hasIdMap)
+	tmpParentSlice, _ = parentTreeToSlice(tree, item, 0, tmpParentSlice, hasIdMap)
 	//移除第一个最上级的行业(根节点)
-	tmpParentSlice = append(tmpParentSlice[:0],tmpParentSlice[1:len(tmpParentSlice)]...)
+	tmpParentSlice = append(tmpParentSlice[:0], tmpParentSlice[1:len(tmpParentSlice)]...)
 	//切片反转
 	tmpParentSlice = reverse(tmpParentSlice)
-	for _,v := range tmpParentSlice{
+	for _, v := range tmpParentSlice {
 		depth++
-		if _,ok := hasIdMap[v.IndustryMapId];ok==false{
-			otherChildMapSlice[depth] = append(otherChildMapSlice[depth],v)
+		if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
+			otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
 		}
 	}
 
 	//获取同级
 	//fmt.Println("===============")
-	depth = siblingTreeToSlice(tree,item,hasIdMap,depth,otherChildMapSlice)
+	depth = siblingTreeToSlice(tree, item, hasIdMap, depth, otherChildMapSlice)
 
 	//上级的同级~
 	/*for _,parentSlice := range tmpParentSlice{
@@ -116,34 +95,26 @@ func getMap(tree *models.CygxIndustryMapItems,item *models.CygxIndustryMapItems,
 }
 
 //获取行业图谱切片
-func GetIndustryMapNameSlice(industryName string)(nameSlice []string,err error){
-	tree,err := GetIndustryTree()
-	if err != nil{
-		fmt.Println("获取树失败")
+func GetIndustryMapNameSlice(industryName string) (nameSlice []string, err error) {
+	nameSlice = append(nameSlice, industryName)
+	tree, err := GetIndustryTree()
+	if err != nil {
 		return
 	}
-
-	//fmt.Println(tree)
-
 	//已经存在的行业id的map集合
 	hasIdMap := make(map[int]string)
-	itemList,err := models.GetFirstCygxIndustryListByName(industryName)
-	if err != nil{
-		fmt.Println("获取数据失败,",err)
+	itemList, err := models.GetFirstCygxIndustryListByName(industryName)
+	if err != nil {
 		return
 	}
 	//找不到对应的数据
-	if len(itemList) <= 0{
+	if len(itemList) <= 0 {
 		return
 	}
-	//for k,item := range list{
-	//	fmt.Println(k)
-	//	fmt.Println(item)
-	//}
-	//return
+
 	industryMapList := make(map[int][]*models.CygxIndustryMapItems)
-	for _,item := range itemList{
-		industryMapList[item.Level] = append(industryMapList[item.Level],item)
+	for _, item := range itemList {
+		industryMapList[item.Level] = append(industryMapList[item.Level], item)
 	}
 
 	//将查出来的根节点数据的key取出来,放在切片中,并对该切片做正序排列
@@ -157,11 +128,11 @@ func GetIndustryMapNameSlice(industryName string)(nameSlice []string,err error){
 	list := industryMapList[sortIndustryList[0]]
 
 	otherChildMapSlice := map[int][]*models.CygxIndustryMapItems{}
-	for _,item := range list{
-		hasIdMap[item.IndustryMapId]=""
+	for _, item := range list {
+		hasIdMap[item.IndustryMapId] = ""
 		//将自己的节点给加进去
-		otherChildMapSlice[0] = append(otherChildMapSlice[0],item)
-		getMap(tree,item,hasIdMap,otherChildMapSlice)
+		otherChildMapSlice[0] = append(otherChildMapSlice[0], item)
+		getMap(tree, item, hasIdMap, otherChildMapSlice)
 	}
 	var tmpSlice []*models.CygxIndustryMapItems
 
@@ -173,19 +144,19 @@ func GetIndustryMapNameSlice(industryName string)(nameSlice []string,err error){
 	sort.Ints(sortList)
 
 	//遍历该切片,根据下标key获取对应的数据,并插入到主数据中
-	for _,v := range sortList{
+	for _, v := range sortList {
 		tmpChildSlice := otherChildMapSlice[v]
 		randSlice(tmpChildSlice)
-		tmpSlice = append(tmpSlice,tmpChildSlice...)
+		tmpSlice = append(tmpSlice, tmpChildSlice...)
 		//fmt.Println(k,"=====")
 		//for _,tmpV := range otherChildMapSlice[v]{
 		//	fmt.Println(tmpV.IndustryMapName)
 		//}
 	}
 	//名字切片
-	for _,v := range tmpSlice{
+	for _, v := range tmpSlice {
 		//fmt.Println("k===",k,"=======v=======",v)
-		nameSlice = append(nameSlice,v.IndustryMapName)
+		nameSlice = append(nameSlice, v.IndustryMapName)
 	}
 
 	//fmt.Println(nameSlice)
@@ -203,19 +174,19 @@ func reverse(s []*models.CygxIndustryMapItems) []*models.CygxIndustryMapItems {
 }
 
 //获取当前节点的树
-func getChildTree(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems) (returnNode *models.CygxIndustryMapItems)  {
-	if rootNode.IndustryMapId == nowNode.IndustryMapId{
+func getChildTree(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems) (returnNode *models.CygxIndustryMapItems) {
+	if rootNode.IndustryMapId == nowNode.IndustryMapId {
 		returnNode = rootNode
 		return
 	}
-	if rootNode.Children != nil{
-		for _,v := range rootNode.Children {
-			if v.IndustryMapId == nowNode.IndustryMapId{
+	if rootNode.Children != nil {
+		for _, v := range rootNode.Children {
+			if v.IndustryMapId == nowNode.IndustryMapId {
 				returnNode = v
-			}else{
-				returnNode = getChildTree(v,nowNode)
+			} else {
+				returnNode = getChildTree(v, nowNode)
 			}
-			if returnNode != nil{
+			if returnNode != nil {
 				return
 			}
 		}
@@ -224,23 +195,23 @@ func getChildTree(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndu
 }
 
 //获取子树V2,与上一个子树规律不一样,因为上级
-func childTreeToSlice(rootNode *models.CygxIndustryMapItems,hasIdMap map[int]string,depth int,otherChildMapSlice map[int][]*models.CygxIndustryMapItems)(maxDepth int) {
-	if rootNode.Children != nil{
+func childTreeToSlice(rootNode *models.CygxIndustryMapItems, hasIdMap map[int]string, depth int, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) (maxDepth int) {
+	if rootNode.Children != nil {
 		depth++
 		maxDepth = depth
-		for _,v := range rootNode.Children {
+		for _, v := range rootNode.Children {
 			//判断是否已经录入,如果已经录入,那么不处理
-			if _,ok := hasIdMap[v.IndustryMapId];ok==false{
-				otherChildMapSlice[depth] = append(otherChildMapSlice[depth],v)
+			if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
+				otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
 			}
 		}
 
-		for _,v := range rootNode.Children {
+		for _, v := range rootNode.Children {
 			//判断是否已经录入,如果已经录入,那么不处理
-			if _,ok := hasIdMap[v.IndustryMapId];ok==false{
+			if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
 				hasIdMap[v.IndustryMapId] = ""
-				returnDepth := childTreeToSlice(v,hasIdMap,depth,otherChildMapSlice)
-				if returnDepth > maxDepth{
+				returnDepth := childTreeToSlice(v, hasIdMap, depth, otherChildMapSlice)
+				if returnDepth > maxDepth {
 					maxDepth = returnDepth
 				}
 			}
@@ -250,32 +221,32 @@ func childTreeToSlice(rootNode *models.CygxIndustryMapItems,hasIdMap map[int]str
 }
 
 //获取兄弟级树
-func siblingTreeToSlice(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems,hasIdMap map[int]string,depth int,otherChildMapSlice map[int][]*models.CygxIndustryMapItems)(maxDepth int)  {
-	if rootNode.Children != nil{
+func siblingTreeToSlice(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems, hasIdMap map[int]string, depth int, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) (maxDepth int) {
+	if rootNode.Children != nil {
 		depth++
 		maxDepth = depth
-		for _,v := range rootNode.Children {
+		for _, v := range rootNode.Children {
 			//如果父级id一致的情况下,那么代表这是兄弟节点或者是自己了
-			if v.ParentId == nowNode.ParentId{
+			if v.ParentId == nowNode.ParentId {
 				//判断是否已经录入,如果已经录入,那么不处理
-				if _,ok := hasIdMap[v.IndustryMapId];ok==false{
-					otherChildMapSlice[depth] = append(otherChildMapSlice[depth],v)
-					returnDepth := childTreeToSlice(v,hasIdMap,depth,otherChildMapSlice)
-					if returnDepth > maxDepth{
+				if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
+					otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
+					returnDepth := childTreeToSlice(v, hasIdMap, depth, otherChildMapSlice)
+					if returnDepth > maxDepth {
 						maxDepth = returnDepth
 					}
 				}
-			}else{
-				returnDepth := siblingTreeToSlice(v,nowNode,hasIdMap,depth,otherChildMapSlice)
-				if returnDepth > maxDepth{
+			} else {
+				returnDepth := siblingTreeToSlice(v, nowNode, hasIdMap, depth, otherChildMapSlice)
+				if returnDepth > maxDepth {
 					maxDepth = returnDepth
 				}
 			}
 		}
 
-		for _,v := range rootNode.Children{
+		for _, v := range rootNode.Children {
 			if v.ParentId == nowNode.ParentId {
-				if _,ok := hasIdMap[v.IndustryMapId];ok==false{
+				if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
 					hasIdMap[v.IndustryMapId] = ""
 				}
 
@@ -286,22 +257,22 @@ func siblingTreeToSlice(rootNode *models.CygxIndustryMapItems,nowNode *models.Cy
 }
 
 //获取上级树
-func parentTreeToSlice(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems,depth int,tmpReturnSlice []*models.CygxIndustryMapItems,hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems,ok bool)  {
+func parentTreeToSlice(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems, depth int, tmpReturnSlice []*models.CygxIndustryMapItems, hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems, ok bool) {
 	if depth != 0 {
 		returnSlice = tmpReturnSlice
 	}
 	depth++
-	returnSlice = append(returnSlice,rootNode)
+	returnSlice = append(returnSlice, rootNode)
 	//if _,ok := hasIdMap[rootNode.IndustryMapId];ok==false{
 	//	hasIdMap[rootNode.IndustryMapId] = ""
 	//}
-	if rootNode.Children != nil{
-		for _,v := range rootNode.Children {
-			if v.ParentId == nowNode.ParentId{
+	if rootNode.Children != nil {
+		for _, v := range rootNode.Children {
+			if v.ParentId == nowNode.ParentId {
 				ok = true
 				return
 			}
-			returnSlice,ok = parentTreeToSlice(v,nowNode,depth,returnSlice,hasIdMap)
+			returnSlice, ok = parentTreeToSlice(v, nowNode, depth, returnSlice, hasIdMap)
 			//如果返回的匹配完成,那么不进入下一次循环,直接返回
 			if ok {
 				return
@@ -335,25 +306,26 @@ func randSlice(slice []*models.CygxIndustryMapItems) {
 	}
 	return
 }
+
 //region数据修复
-func FixData(){
-	tree,err := GetIndustryTree()
-	if err != nil{
-		fmt.Println("获取树失败")
+func FixData() {
+	tree, err := GetIndustryTree()
+	if err != nil {
 	}
-	models.FixLevelData(tree.IndustryMapId,1)
-	fixData(tree,1)
+	models.FixLevelData(tree.IndustryMapId, 1)
+	fixData(tree, 1)
 	return
 }
 
-func fixData(item *models.CygxIndustryMapItems,depth int)  {
+func fixData(item *models.CygxIndustryMapItems, depth int) {
 	depth++
-	if item.Children == nil{
+	if item.Children == nil {
 		return
 	}
-	for _,v := range item.Children{
-		models.FixLevelData(v.IndustryMapId,depth)
-		fixData(v,depth)
+	for _, v := range item.Children {
+		models.FixLevelData(v.IndustryMapId, depth)
+		fixData(v, depth)
 	}
 }
-//endregion
+
+//endregion