|
@@ -105,6 +105,55 @@ func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (e
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+//新增和修改数据
|
|
|
+func EsAddOrEditDataV4(indexName, docId string, item *ElasticTestArticleDetailV4) (err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("EsAddOrEditData Err:", err.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ client, err := NewClient()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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{}{
|
|
|
+ "BodyText": item.BodyText,
|
|
|
+ "Title": item.Title,
|
|
|
+ "PublishDate": item.PublishDate,
|
|
|
+ "IsSummary": item.IsSummary,
|
|
|
+ "IsReport": item.IsReport,
|
|
|
+ }).Do(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ fmt.Println(resp.Status, resp.Result)
|
|
|
+ if resp.Status == 0 {
|
|
|
+ fmt.Println("修改成功")
|
|
|
+ } 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("新增成功")
|
|
|
+ err = nil
|
|
|
+ } else {
|
|
|
+ fmt.Println("AddData", resp.Status, resp.Result)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
//删除数据
|
|
|
func EsDeleteData(indexName, docId string) (err error) {
|
|
|
client, err := NewClient()
|