瀏覽代碼

Es新建索引

xingzai 3 年之前
父節點
當前提交
89a7931365
共有 2 個文件被更改,包括 75 次插入69 次删除
  1. 73 68
      services/task.go
  2. 2 1
      utils/config.go

+ 73 - 68
services/task.go

@@ -2,8 +2,13 @@ package services
 
 import (
 	"fmt"
+	"github.com/PuerkitoBio/goquery"
 	"github.com/astaxie/beego/toolbox"
+	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/utils"
+	"html"
+	"strconv"
+	"strings"
 	//"github.com/astaxie/beego/cache"
 )
 
@@ -194,74 +199,74 @@ searchItem.Body = v.Highlight["BodyText"]
 searchItem.Title = title
 searchItem.PublishDate = article.PublishDate
 */
-//func CreateIndex() {
-//	indexName := "cygx_article_v4" //utils.IndexName
-//	mappingJson := `{
-//  "mappings": {
-//    "dynamic": true,
-//    "properties": {
-//      "ArticleId": {
-//        "type": "integer"
-//      },
-//      "BodyText": {
-//        "type": "text",
-//        "term_vector": "with_positions_offsets",
-//		"analyzer": "ik_smart"
-//      },
-//      "PublishDate": {
-//        "type": "keyword"
-//      },
-//      "SubCategoryName": {
-//        "type": "text",
-//        "term_vector": "with_positions_offsets",
-//		"analyzer": "ik_smart"
-//      },
-//      "Title": {
-//        "type": "text",
-//        "term_vector": "with_positions_offsets",
-//		"analyzer": "ik_smart"
-//      },
-//      "TitleEn": {
-//        "type": "text",
-//        "term_vector": "with_positions_offsets"
-//      },
-//      "UpdateFrequency": {
-//        "type": "text",
-//        "term_vector": "with_positions_offsets"
-//      }
-//    }
-//  }
-//}`
-//	EsCreateIndex(indexName, mappingJson)
-//}
-//
-//func AddAllArticle() {
-//	allList, err := models.GetArticleAll()
-//	if err != nil {
-//		fmt.Println("GetArticleAll Err:", err.Error())
-//		return
-//	}
-//
-//	indexName := "cygx_article_v4"
-//
-//	for _, v := range allList {
-//		content := html.UnescapeString(v.Body)
-//		doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
-//		if err != nil {
-//			fmt.Println("create doc err:", err.Error())
-//			return
-//		}
-//		bodyText := doc.Text()
-//
-//		item := new(ElasticTestArticleDetail)
-//		item.ArticleId = v.ArticleId
-//		item.Title = v.Title
-//		item.PublishDate = v.PublishDate
-//		item.BodyText = bodyText
-//		EsAddOrEditData(indexName, strconv.Itoa(v.ArticleId), item)
-//		fmt.Println(v.ArticleId)
-//	}
-//}
+func CreateIndex() {
+	indexName := "cygx_article_v4" //utils.IndexName
+	mappingJson := `{
+ "mappings": {
+   "dynamic": true,
+   "properties": {
+     "ArticleId": {
+       "type": "integer"
+     },
+     "BodyText": {
+       "type": "text",
+       "term_vector": "with_positions_offsets",
+		"analyzer": "ik_smart"
+     },
+     "PublishDate": {
+       "type": "keyword"
+     },
+     "SubCategoryName": {
+       "type": "text",
+       "term_vector": "with_positions_offsets",
+		"analyzer": "ik_smart"
+     },
+     "Title": {
+       "type": "text",
+       "term_vector": "with_positions_offsets",
+		"analyzer": "ik_smart"
+     },
+     "TitleEn": {
+       "type": "text",
+       "term_vector": "with_positions_offsets"
+     },
+     "UpdateFrequency": {
+       "type": "text",
+       "term_vector": "with_positions_offsets"
+     }
+   }
+ }
+}`
+	EsCreateIndex(indexName, mappingJson)
+}
+
+func AddAllArticle() {
+	allList, err := models.GetArticleAll()
+	if err != nil {
+		fmt.Println("GetArticleAll Err:", err.Error())
+		return
+	}
+
+	indexName := "cygx_article_v3"
+
+	for _, v := range allList {
+		content := html.UnescapeString(v.Body)
+		doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
+		if err != nil {
+			fmt.Println("create doc err:", err.Error())
+			return
+		}
+		bodyText := doc.Text()
+
+		item := new(ElasticTestArticleDetail)
+		item.ArticleId = v.ArticleId
+		item.Title = v.Title
+		item.PublishDate = v.PublishDate
+		item.BodyText = bodyText
+		EsAddOrEditData(indexName, strconv.Itoa(v.ArticleId), item)
+		fmt.Println(v.ArticleId)
+	}
+}
 
 // "term_vector": "with_positions_offsets"
 

+ 2 - 1
utils/config.go

@@ -75,7 +75,8 @@ func init() {
 		WxPublicAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
 		WxPublicId = "gh_b67e0049fb8c"
 		//IndexName="cygx_article_v1"
-		IndexName = "cygx_article_v2"
+		//IndexName = "cygx_article_v2"
+		IndexName = "cygx_article_v3"
 	} else {
 		WxAppId = "wxcc32b61f96720d2f"
 		WxAppSecret = "06894933fafb24dafead7eaae09c08e0"