|
@@ -2,6 +2,7 @@ package services
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/olivere/elastic/v7"
|
|
|
"hongze/hz_eta_api/models"
|
|
@@ -207,6 +208,65 @@ func EsAddOrEditEnglishReport(indexName, docId string, item *models.ElasticEngli
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// EsAddOrEditSaCompare 新增编辑es语义分析文档对比
|
|
|
+func EsAddOrEditSaCompare(indexName, docId string, item *saModel.SaCompareElastic) (err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("EsAddOrEditSaCompare Err:", err.Error())
|
|
|
+ go alarm_msg.SendAlarmMsg("新增编辑es语义分析文档对比 EsAddOrEditSaCompare,Err:"+err.Error(), 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ client := utils.EsClient
|
|
|
+ // docId为报告ID
|
|
|
+ searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ if strings.Contains(err.Error(), "404") {
|
|
|
+ err = nil
|
|
|
+ } else {
|
|
|
+ fmt.Println("Get Err" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if searchById != nil && searchById.Found {
|
|
|
+ resp, e := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
|
|
|
+ "SaCompareId": item.SaCompareId,
|
|
|
+ "ClassifyName": item.ClassifyName,
|
|
|
+ "ClassifyId": item.ClassifyId,
|
|
|
+ "Title": item.Title,
|
|
|
+ "ResultImg": item.ResultImg,
|
|
|
+ "CreateTime": item.CreateTime,
|
|
|
+ "ModifyTime": item.ModifyTime,
|
|
|
+ "SysAdminId": item.SysAdminId,
|
|
|
+ "SysAdminName": item.SysAdminName,
|
|
|
+ }).Do(context.Background())
|
|
|
+ if e != nil {
|
|
|
+ err = e
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //fmt.Println(resp.Status, resp.Result)
|
|
|
+ if resp.Status == 0 {
|
|
|
+ fmt.Println("修改成功" + docId)
|
|
|
+ err = nil
|
|
|
+ } else {
|
|
|
+ fmt.Println("EditData", resp.Status, resp.Result)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
|
|
|
+ if e != nil {
|
|
|
+ err = e
|
|
|
+ fmt.Println("新增失败:", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Status == 0 && resp.Result == "created" {
|
|
|
+ fmt.Println("新增成功" + docId)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ fmt.Println("AddData", resp.Status, resp.Result)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// EsAddOrEditSaDoc 新增编辑语义分析文档
|
|
|
func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err error) {
|
|
|
defer func() {
|
|
@@ -267,3 +327,107 @@ func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err
|
|
|
fmt.Println("AddData", resp.Status, resp.Result)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// EsSearchSaCompareData 查询es中的文档对比信息
|
|
|
+func EsSearchSaCompareData(keywordStr string, from, size int) (total int, list []*saModel.SaCompareElastic, err error) {
|
|
|
+ list = make([]*saModel.SaCompareElastic, 0)
|
|
|
+ indexName := utils.EsSemanticAnalysisCompareIndexName
|
|
|
+ var tmpTotal int64
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("EsSearchSaCompareData Err:", err.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ client := utils.EsClient
|
|
|
+
|
|
|
+ mustMap := make([]interface{}, 0)
|
|
|
+ mustNotMap := make([]interface{}, 0)
|
|
|
+
|
|
|
+ //关键字匹配
|
|
|
+ shouldMap := map[string]interface{}{
|
|
|
+ "should": []interface{}{
|
|
|
+ map[string]interface{}{
|
|
|
+ "match_phrase": map[string]interface{}{
|
|
|
+ "Title": keywordStr,
|
|
|
+ //"Frequency.keyword": "月度",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "bool": shouldMap,
|
|
|
+ })
|
|
|
+
|
|
|
+ queryMap := map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "bool": map[string]interface{}{
|
|
|
+ "must": mustMap,
|
|
|
+ "must_not": mustNotMap,
|
|
|
+ //"should": shouldMap,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据条件数量统计
|
|
|
+ requestTotalHits := client.Count(indexName).BodyJson(queryMap)
|
|
|
+ tmpTotal, err = requestTotalHits.Do(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ total = int(tmpTotal)
|
|
|
+ // 分页查询
|
|
|
+ queryMap["from"] = from
|
|
|
+ queryMap["size"] = size
|
|
|
+ queryMap["sort"] = []map[string]interface{}{
|
|
|
+ map[string]interface{}{
|
|
|
+ "CreateTime.keyword": map[string]interface{}{
|
|
|
+ "order": "desc",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ map[string]interface{}{
|
|
|
+ "_score": map[string]interface{}{
|
|
|
+ "order": "desc",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ compareItem := new(saModel.SaCompareElastic)
|
|
|
+ tmpErr = json.Unmarshal(itemJson, &compareItem)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("json.Unmarshal itemJson err:", err)
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ list = append(list, compareItem)
|
|
|
+ searchMap[v.Id] = v.Id
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|