Browse Source

fix:es映射补充

zqbao 7 months ago
parent
commit
3a7c564143
3 changed files with 73 additions and 3 deletions
  1. 1 3
      .gitignore
  2. 0 0
      test/conf/app_dev.conf
  3. 72 0
      test/es_test.go

+ 1 - 3
.gitignore

@@ -5,6 +5,4 @@
 *.exe~
 go.sum
 scheduler/etalogs/
-scheduler/conf/
-*_test.go
-/test/
+scheduler/conf/

+ 0 - 0
test/conf/app_dev.conf


+ 72 - 0
test/es_test.go

@@ -0,0 +1,72 @@
+package test
+
+import (
+	"context"
+	"eta/eta_mini_crm/utils"
+	"fmt"
+	"testing"
+)
+
+// indexName:索引名称
+// mappingJson:表结构
+func EsCreateIndex(indexName, mappingJson string) (err error) {
+	client := utils.EsClient
+
+	//定义表结构
+	exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
+	if err != nil {
+		return
+	}
+	if !exists {
+		resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
+		//BodyJson(bodyJson).Do(context.Background())
+		if err != nil {
+			fmt.Println("CreateIndex Err:" + err.Error())
+			return err
+		}
+		fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
+	} else {
+		fmt.Println(indexName + " 已存在")
+	}
+	return
+}
+
+func TestCreateIndex(t *testing.T) {
+	var mappingJson = `{
+  "mappings": {
+    "properties": {
+      "ReportPdfId": {"type": "integer"},
+      "PdfUrl": {"type": "text"},
+      "PdfName": {"type": "text"},
+      "Title": {"type": "text"},
+      "Author": {"type": "text"},
+      "Abstract": {"type": "text"},
+      "ClassifyIdFirst": {"type": "integer"},
+      "ClassifyNameFirst": {"type": "text"},
+      "ClassifyIdSecond": {"type": "integer"},
+      "ClassifyNameSecond": {"type": "text"},
+      "Stage": {"type": "integer"},
+      "PublishTime": {
+        "type": "text",
+        "fields": {
+          "keyword": {
+            "type": "keyword",
+            "ignore_above": 256
+          }
+        }
+      },
+      "ModifyTime": {"type": "date"},
+      "Pv": {"type": "integer"},
+      "Uv": {"type": "integer"},
+      "SysUserId": {"type": "integer"},
+      "SysRealName": {"type": "text"},
+      "State": {"type": "integer"}
+	}
+   }
+}`
+	err := EsCreateIndex(utils.MINI_REPORT_INDEX_NAME, mappingJson)
+	if err != nil {
+		fmt.Println(err)
+	}
+
+}