package elastic import ( "fmt" "github.com/stretchr/testify/assert" "testing" ) 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) // // esClinet.InitElasticSize() // // } func TestCount(t *testing.T) { esClinet = Elastic{ S_esurl: "http://192.168.3.241:40801", I_size: 2, Username: "", Password: "", } esClinet.InitElasticSize() //rangeQuery := es.NewRangeQuery("id").Gte("6463a8800000000000000000").Lt("6464fa000000000000000000") //termsQuery := es.NewTermsQuery("site", "元博网(采购与招标网)", "中国招标与采购网") //boolQuery := es.NewBoolQuery().Must(rangeQuery, termsQuery) // //bytes, err := json.Marshal(boolQuery) //if err != nil { // fmt.Println("err", err.Error()) //} //fmt.Println("qqq", string(bytes)) //count1 := esClinet.Count("bidding", boolQuery) //fmt.Println(count1) fmt.Println(esClinet.Count("bidding_20231121", nil)) } func TestBulkSave(t *testing.T) { esClinet = Elastic{ S_esurl: "http://127.0.0.1:19805", I_size: 2, Username: "es_all", Password: "TopJkO2E_d1x", } esClinet.InitElasticSize() id := "6398a381063a7b816e072adf" esUpdate := map[string]interface{}{ "area": "新疆", "city": "伊犁哈萨克自治州", "district": "奎屯市", } updateEs := []map[string]interface{}{ {"_id": id}, esUpdate, } arru := make([][]map[string]interface{}, 0) arru = append(arru, updateEs) esClinet.UpdateBulk("bidding", arru...) fmt.Println(arru) } func TestElastic_ExistsIndex(t *testing.T) { esClinet = Elastic{ S_esurl: "http://127.0.0.1:19805", I_size: 2, Username: "es_all", Password: "TopJkO2E_d1x", } esClinet.InitElasticSize() res, err := esClinet.ExistsIndex("bidding") assert.Equal(t, nil, err) assert.Equal(t, true, res) } func TestElastic_SetAlias(t *testing.T) { //esClinet = Elastic{ // S_esurl: "http://127.0.0.1:19805", // I_size: 2, // Username: "es_all", // Password: "TopJkO2E_d1x", //} esClinet = Elastic{ S_esurl: "http://127.0.0.1:19905", I_size: 2, Username: "jybid", Password: "Top2023_JEB01i@31", } esClinet.InitElasticSize() err := esClinet.SetAlias("bidding_20231120", "bidding_pre2") assert.Equal(t, nil, err) } func TestElastic_RemoveAlias(t *testing.T) { esClinet = Elastic{ S_esurl: "http://127.0.0.1:19805", I_size: 2, Username: "es_all", Password: "TopJkO2E_d1x", } esClinet.InitElasticSize() err := esClinet.RemoveAlias("bidding_20231120", "bidding_pre") assert.Equal(t, nil, err) } func TestCleanIndex(t *testing.T) { esClinet = Elastic{ S_esurl: "http://127.0.0.1:19805", I_size: 2, Username: "es_all", Password: "TopJkO2E_d1x", } esClinet.InitElasticSize() err := esClinet.RemoveAlias("bidding_20231118", "bidding_pre") assert.Equal(t, nil, err) } func TestElastic_DeleteByID(t *testing.T) { esClinet = Elastic{ S_esurl: "http://127.0.0.1:19805", I_size: 2, Username: "es_all", Password: "TopJkO2E_d1x", } esClinet.InitElasticSize() 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) } func TestElastic_GetById(t *testing.T) { esClinet = Elastic{ S_esurl: "http://127.0.0.1:19805", I_size: 2, Username: "es_all", Password: "TopJkO2E_d1x", } esClinet.InitElasticSize() err, doc := esClinet.GetById("bidding", "59ed510d40d2d9bbe85dd221") assert.Equal(t, nil, err) fmt.Println("doc", doc) }