wcc 1 жил өмнө
parent
commit
857f0a039c

+ 31 - 0
elastic/elasticSim.go

@@ -577,3 +577,34 @@ func (e *Elastic) Save(index string, obj interface{}) bool {
 	}
 
 }
+
+// SaveDocument 保存单个索引
+func (e *Elastic) SaveDocument(index string, data map[string]interface{}) error {
+	client := e.GetEsConn()
+	defer e.DestoryEsConn(client)
+	// 从数据中获取 ID 字段
+	id, ok := data["id"].(string)
+	if !ok {
+		return errors.New("ID field not found in data")
+	}
+
+	// 将数据转换为 JSON 格式
+	docJSON, err := json.Marshal(data)
+	if err != nil {
+		return err
+	}
+
+	// 创建一个新的索引请求
+	indexRequest := client.Index().
+		Index(index).
+		Id(id).
+		BodyJson(string(docJSON))
+
+	// 执行索引请求
+	_, err = indexRequest.Do(context.Background())
+	if err != nil {
+		return err
+	}
+
+	return nil
+}

+ 45 - 10
elastic/elastic_test.go

@@ -8,18 +8,18 @@ import (
 
 var esClinet Elastic
 
-//func init() {
-//	esClinet = Elastic{
-//		S_esurl:  "127.0.0.1:19805",
-//		I_size:   2,
-//		Username: "es_all",
-//		Password: "TopJkO2E_d1x",
-//	}
-//	fmt.Println(esClinet)
+//	func init() {
+//		esClinet = Elastic{
+//			S_esurl:  "127.0.0.1:19805",
+//			I_size:   2,
+//			Username: "es_all",
+//			Password: "TopJkO2E_d1x",
+//		}
+//		fmt.Println(esClinet)
 //
-//	esClinet.InitElasticSize()
+//		esClinet.InitElasticSize()
 //
-//}
+// }
 func TestCount(t *testing.T) {
 	esClinet = Elastic{
 		S_esurl:  "http://192.168.3.241:40801",
@@ -143,3 +143,38 @@ func TestElastic_DeleteByID(t *testing.T) {
 	err := esClinet.DeleteByID("bidding_2023112216", "655c99c60687916fae58eec1")
 	assert.Equal(t, nil, err)
 }
+
+func TestElastic_SaveDocument(t *testing.T) {
+	esClinet = Elastic{
+		S_esurl:  "http://192.168.3.149:9201",
+		I_size:   2,
+		Username: "",
+		Password: "",
+	}
+	esClinet.InitElasticSize()
+
+	data := map[string]interface{}{
+		"id":              "658b7ce566cf0db42a395757",
+		"title":           "2024-01-08测试数据",
+		"detail":          "2024-01-08测试内容",
+		"buyer":           "测试采购单位",
+		"projectname":     "测试项目名称",
+		"area":            "测试",
+		"city":            "测试",
+		"district":        "测试",
+		"href":            "https://eproport.crecgec.com/#/notice/notice-detail?projectId=1739610805157965826&tenantId=2&indexnumber=0",
+		"purchasing":      "预埋钢板",
+		"purchasing_tag":  "工程施工,预埋,钢板",
+		"s_subscopeclass": "建筑工程_工程施工",
+		"s_topscopeclass": "建筑工程",
+		"site":            "中铁鲁班商务网",
+		"spidercode":      "a_ztlbsww_zbgg",
+		"toptype":         "结果",
+		"createtime":      1703755403,
+		"infoformat":      1,
+		"pici":            1703755244,
+	}
+
+	err := esClinet.SaveDocument("bidding", data)
+	assert.Equal(t, nil, err)
+}