浏览代码

es研报搜索增加发布状态的条件

kobe6258 8 月之前
父节点
当前提交
081ec0df2c
共有 2 个文件被更改,包括 141 次插入32 次删除
  1. 85 21
      common/component/es/es.go
  2. 56 11
      domian/report/report_service.go

+ 85 - 21
common/component/es/es.go

@@ -466,6 +466,44 @@ func (es *ESClient) Delete(indexName string, id int) bool {
 
 
 }
 }
 
 
+func (es *ESClient) Exist(docId int) (exist bool, err error) {
+
+	getRequest := esapi.GetRequest{
+		Index:      "my_index",
+		DocumentID: strconv.Itoa(docId),
+	}
+	// 执行请求
+	res, err := getRequest.Do(context.Background(), es.es())
+	if err != nil {
+		logger.Error("es获取文档是否存在失败: %v", err)
+	}
+	defer res.Body.Close()
+
+	// 检查文档是否存在
+	if res.IsError() {
+		// 如果文档不存在,通常返回 404 Not Found
+		if res.StatusCode == 404 {
+			logger.Info("文档不存在.")
+			return false, nil
+		} else {
+			// 其他错误
+			var e map[string]interface{}
+			if err = json.NewDecoder(res.Body).Decode(&e); err != nil {
+				logger.Error("解析es应答失败: %v", err)
+				return false, err
+			} else {
+				// Print the response status and error information.
+				logger.Error("[%s] %s: %s\n", res.Status(), e["error"].(map[string]interface{})["type"], e["error"].(map[string]interface{})["原因"])
+				return false, nil
+			}
+		}
+	} else {
+		// 如果文档存在
+		logger.Info("doc存在")
+		return true, nil
+	}
+}
+
 //
 //
 //func CreateIndex(indexName string) error {
 //func CreateIndex(indexName string) error {
 //	resp, err := esClient.es().Indices.
 //	resp, err := esClient.es().Indices.
@@ -480,26 +518,52 @@ func (es *ESClient) Delete(indexName string, id int) bool {
 //}
 //}
 
 
 // DeleteIndex 删除索引
 // DeleteIndex 删除索引
-//func DeleteIndex(indexName string) error {
-//	_, err := esClient.es().Indices. // 表明是对索引的操作,而Index则表示是要操作具体索引下的文档
-//						Delete(indexName).
-//						Do(context.Background())
-//	if err != nil {
-//		fmt.Printf("delete index failed,err:%v\n", err)
-//		return err
-//	}
-//	fmt.Printf("delete index successed,indexName:%s", indexName)
-//	return nil
-//}
 //
 //
-//// CreateDocument 创建文档
-//func CreateDocument(indexName string, id string, doc interface{}) {
-//	// 添加文档
-//	resp, err := esClient.esOp.Index(indexName).Id(id).Document(doc).Do(context.Background())
-//	if err != nil {
-//		logger.Error("indexing document failed, err:%v\n", err)
-//		return
+//	func DeleteIndex(indexName string) error {
+//		_, err := esClient.es().Indices. // 表明是对索引的操作,而Index则表示是要操作具体索引下的文档
+//							Delete(indexName).
+//							Do(context.Background())
+//		if err != nil {
+//			fmt.Printf("delete index failed,err:%v\n", err)
+//			return err
+//		}
+//		fmt.Printf("delete index successed,indexName:%s", indexName)
+//		return nil
 //	}
 //	}
-//	logger.Info("result:%#v\n", resp.Result)
-//	return
-//}
+//
+// CreateDocument 创建文档
+func (es *ESClient) CreateDocument(indexName string, id int, doc interface{}) (success bool) {
+	jsonDoc, _ := json.Marshal(doc)
+	logger.Info("查询语句: %s", string(jsonDoc))
+	// 添加文档
+	indexRequest := esapi.IndexRequest{
+		Index:      indexName,
+		DocumentID: strconv.Itoa(id),
+		Body:       strings.NewReader(string(jsonDoc)),
+		Refresh:    "true",
+	}
+
+	// 执行请求
+	res, err := indexRequest.Do(context.Background(), es.es())
+	if err != nil {
+		logger.Error("ES创建文档失败: %s", err)
+		return false
+	}
+	defer res.Body.Close()
+
+	// 检查文档是否成功创建
+	if res.IsError() {
+		var e map[string]interface{}
+		if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
+			logger.Error("解析ES应答失败: %s", err)
+		} else {
+			// Print the response status and error information.
+			logger.Error("[%s] %s: %s\n", res.Status(), e["error"].(map[string]interface{})["类型"], e["错误"].(map[string]interface{})["原因"])
+		}
+		return false
+	} else {
+		// 如果文档成功创建
+		logger.Info("创建文档成功")
+		return true
+	}
+}

+ 56 - 11
domian/report/report_service.go

@@ -321,12 +321,35 @@ func SyncETAReportList(list []eta.ETAReport) (err error) {
 	logger.Info("同步研报数量%d", len(list))
 	logger.Info("同步研报数量%d", len(list))
 	var reports []reportDao.Report
 	var reports []reportDao.Report
 	for _, etaRp := range list {
 	for _, etaRp := range list {
+		var coverSrc int
+		var permissions []etaDao.ChartPermission
+		permissions, err = etaDao.GetSecondPermissionsByClassifyID(etaRp.ClassifyID)
+		if err != nil || len(permissions) == 0 {
+			logger.Error("获取研报二级品种信息失败:%v", err)
+			coverSrc = 0
+		} else {
+			permissionsId := permissions[0].ChartPermissionID
+			var ids []int
+			ids, err = mediaDao.GetIdsByPermissionId(permissionsId)
+			if err != nil {
+				logger.Error("获取图片资源失败:%v", err)
+			}
+			if ids == nil || len(ids) == 0 {
+				coverSrc = 0
+			} else {
+				src := rand.NewSource(time.Now().UnixNano())
+				r := rand.New(src)
+				// 从切片中随机选择一个元素
+				randomIndex := r.Intn(len(ids))
+				coverSrc = ids[randomIndex]
+			}
+		}
 		//authorNames := strings.Split(etaRp.Author, ",")
 		//authorNames := strings.Split(etaRp.Author, ",")
 		//authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
 		//authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
 		//for _, authorName := range authorNamesWithOutEmpty {
 		//for _, authorName := range authorNamesWithOutEmpty {
 		status := etaStatus(etaRp.State)
 		status := etaStatus(etaRp.State)
 		destRp := convertEtaReport(etaRp, status)
 		destRp := convertEtaReport(etaRp, status)
-		//destRp.Author = authorName
+		destRp.CoverSrc = coverSrc
 		reports = append(reports, destRp)
 		reports = append(reports, destRp)
 	}
 	}
 	//}
 	//}
@@ -353,18 +376,40 @@ func syncESAndSendMessage(reports []reportDao.Report) (err error) {
 	}
 	}
 	//同步es
 	//同步es
 	for _, report := range reports {
 	for _, report := range reports {
-		update := UpdateESReport{
-			Title:         report.Title,
-			Author:        report.Author,
-			PublishedTime: report.PublishedTime,
-			Abstract:      report.Abstract,
-			Status:        string(report.Status),
+		var exist bool
+		exist, err = elastic().Exist(report.ID)
+		if err != nil {
+			logger.Error("查询es失败,reportId::%d,err:%v", report.ID, err)
 		}
 		}
-		success := elastic().Update(htConfig.GetReportIndex(), report.ID, update)
-		if !success {
-			logger.Error("更新es失败,reportId::%d,err:%v", report.ID, err)
+		if exist {
+			update := UpdateESReport{
+				Title:         report.Title,
+				Author:        report.Author,
+				PublishedTime: report.PublishedTime,
+				Abstract:      report.Abstract,
+				Status:        string(report.Status),
+			}
+			success := elastic().Update(htConfig.GetReportIndex(), report.ID, update)
+			if !success {
+				logger.Error("更新es失败,reportId::%d,err:%v", report.ID, err)
+			}
+		} else {
+			insert := ESReport{
+				ReportID:      report.ID,
+				OrgId:         report.OrgID,
+				Title:         report.Title,
+				Author:        report.Author,
+				Source:        report.Source,
+				Abstract:      report.Abstract,
+				CoverSrc:      report.CoverSrc,
+				Status:        report.Status,
+				PublishedTime: report.PublishedTime,
+			}
+			success := elastic().CreateDocument(htConfig.GetReportIndex(), report.ID, insert)
+			if !success {
+				logger.Error("创建es文档失败,reportId::%d,err:%v", report.ID, err)
+			}
 		}
 		}
-
 	}
 	}
 
 
 	//err = elastic().BulkInsert(htConfig.GetReportIndex(), esReports)
 	//err = elastic().BulkInsert(htConfig.GetReportIndex(), esReports)