Browse Source

添加 根据ID 删除索引 数据

wcc 1 year ago
parent
commit
4956718a7e
2 changed files with 61 additions and 15 deletions
  1. 14 0
      elastic/elasticSim.go
  2. 47 15
      elastic/elastic_test.go

+ 14 - 0
elastic/elasticSim.go

@@ -511,3 +511,17 @@ func (e *Elastic) SetAlias(index, aliasName string) (err error) {
 
 	return
 }
+
+//DeleteByID 根据ID 删除索引数据;id 或者索引名称不存在,都不会报错
+func (e *Elastic) DeleteByID(index, id string) error {
+	client := e.GetEsConn()
+	defer e.DestoryEsConn(client)
+
+	_, err := client.Delete().Index(index).Id(id).Do(context.Background())
+
+	if err != nil && es.IsNotFound(err) {
+		return nil
+	}
+
+	return err
+}

+ 47 - 15
elastic/elastic_test.go

@@ -1,9 +1,7 @@
 package elastic
 
 import (
-	"encoding/json"
 	"fmt"
-	es "github.com/olivere/elastic/v7"
 	"github.com/stretchr/testify/assert"
 	"testing"
 )
@@ -31,18 +29,20 @@ func TestCount(t *testing.T) {
 	}
 	esClinet.InitElasticSize()
 
-	rangeQuery := es.NewRangeQuery("id").Gte("6463a8800000000000000000").Lt("6464fa000000000000000000")
-	termsQuery := es.NewTermsQuery("site", "元博网(采购与招标网)", "中国招标与采购网")
-	boolQuery := es.NewBoolQuery().Must(rangeQuery, termsQuery)
+	//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())
+	//}
 
-	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("qqq", string(bytes))
-	count1 := esClinet.Count("bidding", boolQuery)
-	fmt.Println(count1)
+	fmt.Println(esClinet.Count("bidding_20231121", nil))
 }
 
 func TestBulkSave(t *testing.T) {
@@ -87,6 +87,26 @@ func TestElastic_ExistsIndex(t *testing.T) {
 }
 
 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,
@@ -94,12 +114,11 @@ func TestElastic_SetAlias(t *testing.T) {
 		Password: "TopJkO2E_d1x",
 	}
 	esClinet.InitElasticSize()
-
-	err := esClinet.SetAlias("bidding_20231118", "bidding_pre")
+	err := esClinet.RemoveAlias("bidding_20231120", "bidding_pre")
 	assert.Equal(t, nil, err)
 }
 
-func TestElastic_RemoveAlias(t *testing.T) {
+func TestCleanIndex(t *testing.T) {
 	esClinet = Elastic{
 		S_esurl:  "http://127.0.0.1:19805",
 		I_size:   2,
@@ -111,3 +130,16 @@ func TestElastic_RemoveAlias(t *testing.T) {
 	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_202311", "655c99c60687916fae58eec1")
+	assert.Equal(t, nil, err)
+}