Просмотр исходного кода

Merge commit '75421af90e419fd3ba35a096ecb43c16940ed486' into v1

* commit '75421af90e419fd3ba35a096ecb43c16940ed486':
  删除 es7 版本中type 字段

# Conflicts:
#	elastic/elasticSim.go
Jianghan 2 лет назад
Родитель
Сommit
1e18e5f863
2 измененных файлов с 77 добавлено и 5 удалено
  1. 5 5
      elastic/elasticSim.go
  2. 72 0
      elastic/elastic_test.go

+ 5 - 5
elastic/elasticSim.go

@@ -272,7 +272,7 @@ func (e *Elastic) GetNoLimit(index, query string) *[]map[string]interface{} {
 //	return nil
 //}
 
-func (e *Elastic) Count(index, itype string, query interface{}) int64 {
+func (e *Elastic) Count(index string, query interface{}) int64 {
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	if client != nil {
@@ -365,11 +365,11 @@ func (e *Elastic) Count(index, itype string, query interface{}) int64 {
 //}
 
 // UpdateBulk 批量修改文档
-func (e *Elastic) UpdateBulk(index, itype string, docs ...[]map[string]interface{}) {
+func (e *Elastic) UpdateBulk(index string, docs ...[]map[string]interface{}) {
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	bulkService := client.Bulk().Index(index).Refresh("true")
-	// bulkService.Type(itype)
+	//bulkService.Type(itype)
 	for _, d := range docs {
 		id := d[0]["_id"].(string)
 		doc := es.NewBulkUpdateRequest().Id(id).Doc(d[1])
@@ -389,7 +389,7 @@ func (e *Elastic) UpsertBulk(ctx context.Context, index string, ids []string, do
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	bulkService := client.Bulk().Index(index).Refresh("true")
-	// bulkService.Type("bidding")
+	//bulkService.Type("bidding")
 	for i := range ids {
 		doc := es.NewBulkUpdateRequest().Id(ids[i]).Doc(docs[i]).Upsert(docs[i])
 		bulkService.Add(doc)
@@ -409,7 +409,7 @@ func (e *Elastic) DeleteBulk(index string, ids []string) {
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	bulkService := client.Bulk().Index(index).Refresh("true")
-	bulkService.Type("bidding")
+	//bulkService.Type("bidding")
 	for i := range ids {
 		req := es.NewBulkDeleteRequest().Id(ids[i])
 		bulkService.Add(req)

+ 72 - 0
elastic/elastic_test.go

@@ -0,0 +1,72 @@
+package elastic
+
+import (
+	"encoding/json"
+	"fmt"
+	es "github.com/olivere/elastic/v7"
+	"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://127.0.0.1:19805",
+		I_size:   2,
+		Username: "es_all",
+		Password: "TopJkO2E_d1x",
+	}
+	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)
+}
+
+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)
+}