package services import ( "context" "eta_gn/eta_api/models" saModel "eta_gn/eta_api/models/semantic_analysis" "eta_gn/eta_api/utils" "fmt" "github.com/olivere/elastic/v7" "strings" ) func NewClient() (client *elastic.Client, err error) { client, err = elastic.NewClient( elastic.SetURL(utils.ES_URL), elastic.SetBasicAuth(utils.ES_USERNAME, utils.ES_PASSWORD), elastic.SetSniff(false)) return } // 删除数据 func EsDeleteData(indexName, docId string) (err error) { client, err := NewClient() if err != nil { return } resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background()) fmt.Println(resp) if err != nil { return } if resp.Status == 0 { fmt.Println("删除成功") } else { fmt.Println("AddData", resp.Status, resp.Result) } return } // EsAddOrEditReport 新增编辑es报告 func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditReport Err:", err.Error()) } }() client, err := NewClient() if err != nil { return } // docId为报告ID+章节ID searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background()) if err != nil && !strings.Contains(err.Error(), "404") { fmt.Println("Get Err" + err.Error()) return } if searchById != nil && searchById.Found { resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{ "ReportId": item.ReportId, "ReportChapterId": item.ReportChapterId, "Title": item.Title, "Abstract": item.Abstract, "BodyContent": item.BodyContent, "PublishTime": item.PublishTime, "PublishState": item.PublishState, "Author": item.Author, "ClassifyIdFirst": item.ClassifyIdFirst, "ClassifyNameFirst": item.ClassifyNameFirst, "ClassifyIdSecond": item.ClassifyIdSecond, "ClassifyNameSecond": item.ClassifyNameSecond, "ClassifyId": item.ClassifyId, "ClassifyName": item.ClassifyName, "Categories": item.Categories, "StageStr": item.StageStr, }).Do(context.Background()) if err != nil { return err } //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, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if err != nil { fmt.Println("新增失败:", err.Error()) return err } if resp.Status == 0 && resp.Result == "created" { fmt.Println("新增成功" + docId) return nil } else { fmt.Println("AddData", resp.Status, resp.Result) } } return } // EsAddOrEditSaDoc 新增编辑语义分析文档 func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditSaDoc Err:", err.Error()) } }() client, e := NewClient() if e != nil { err = e return } // docId为语义分析文档ID+段落ID searchById, e := client.Get().Index(indexName).Id(docId).Do(context.Background()) if e != nil && !strings.Contains(e.Error(), "404") { err = fmt.Errorf("query sa doc err: %s", e.Error()) return } // 更新 if searchById != nil && searchById.Found { docMap := map[string]interface{}{ "SaDocId": item.SaDocId, "SaDocSectionId": item.SaDocSectionId, "ClassifyId": item.ClassifyId, "ClassifyName": item.ClassifyName, "Title": item.Title, "Theme": item.Theme, "BodyContent": item.BodyContent, "Author": item.Author, "CoverImg": item.CoverImg, "CreateTime": item.CreateTime, } resp, e := client.Update().Index(indexName).Id(docId).Doc(docMap).Do(context.Background()) if e != nil { err = fmt.Errorf("update sa doc err: %s", e.Error()) return } if resp.Status == 0 { return } fmt.Println("EditData", resp.Status, resp.Result) return } // 新增 resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if e != nil { err = fmt.Errorf("insert sa doc err: %s", e.Error()) return } if resp.Status == 0 && resp.Result == "created" { return } fmt.Println("AddData", resp.Status, resp.Result) return }