Skip to content

Commit c96e1f8

Browse files
author
lidehua
committed
整合es
1 parent 2df6964 commit c96e1f8

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Go template
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Dependency directories (remove the comment below to include it)
17+
# vendor/
18+
19+
.idea/

api/syn.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package api
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"my/models"
6+
"net/http"
7+
)
8+
9+
func Syn(c *gin.Context) {
10+
err := models.BookMysqlToEs()
11+
error := models.SectionMysqlToEs()
12+
if err != nil {
13+
c.JSON(http.StatusOK, gin.H{
14+
"code": "500",
15+
"msg": err,
16+
})
17+
return
18+
}
19+
if error != nil {
20+
c.JSON(http.StatusOK, gin.H{
21+
"code": "500",
22+
"msg": error,
23+
})
24+
return
25+
}
26+
c.JSON(http.StatusOK, gin.H{
27+
"code": "200",
28+
"msg": "操作成功",
29+
})
30+
}

models/book.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/jinzhu/gorm"
67
orm "my/database"
@@ -23,14 +24,18 @@ func Books() ([]*Book, error) {
2324
return books, nil
2425
}
2526

26-
func BookMysqlToEs() {
27+
func BookMysqlToEs() (err error) {
2728
var books []*Book
2829
error := orm.DB.Find(&books).Error
2930
if error != nil && error != gorm.ErrRecordNotFound {
3031
fmt.Errorf("BookMysqlToEs() 查询返回失败")
31-
return
32+
return error
3233
}
3334
for i := range books {
34-
orm.Es.Index().Index("book").Id(strconv.Itoa(books[i].Id)).BodyJson(books[i])
35+
_, err := orm.Es.Index().Index("book").Id(strconv.Itoa(books[i].Id)).BodyJson(books[i]).Do(context.Background())
36+
if err != nil {
37+
fmt.Sprintf(" BookMysqlToEs Index err:%s", err.Error())
38+
}
3539
}
40+
return nil
3641
}

models/section.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/jinzhu/gorm"
67
orm "my/database"
@@ -11,7 +12,7 @@ type Section struct {
1112
Id int `gorm:"size:20;primary_key;AUTO_INCREMENT;not null" json:"id"`
1213
BookId int `gorm:"type:bigint(20);"column:book_id" json:"bookId"`
1314
SectionTitle string `gorm:"type:varchar(255);"column:section_title" json:"sectionTitle"`
14-
SectionSeq string `gorm:"type:varchar(255);"column:section_seq" json:"sectionSeq"`
15+
SectionSeq int `gorm:"type:bigint(20);"column:section_seq" json:"sectionSeq"`
1516
SectionContent string `gorm:"type:text;"column:section_content" json:"sectionContent"`
1617
}
1718

@@ -24,14 +25,18 @@ func Sections() ([]*Section, error) {
2425
return s, nil
2526
}
2627

27-
func SectionMysqlToEs() {
28+
func SectionMysqlToEs() (err error) {
2829
var s []*Section
2930
error := orm.DB.Find(&s).Error
3031
if error != nil && error != gorm.ErrRecordNotFound {
3132
fmt.Errorf("SectionMysqlToEs() 查询返回失败")
32-
return
33+
return error
3334
}
3435
for i := range s {
35-
orm.Es.Index().Index("section").Id(strconv.Itoa(s[i].Id)).BodyJson(s[i])
36+
_, err := orm.Es.Index().Index("section").Id(strconv.Itoa(s[i].Id)).BodyJson(s[i]).Do(context.Background())
37+
if err != nil {
38+
fmt.Sprintf(" SectionMysqlToEs Index err:%s", err.Error())
39+
}
3640
}
41+
return nil
3742
}

router/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ func InnitRouter() *gin.Engine {
1212
Engine.GET("/book/section/:num", BookSectionByNum)
1313
Engine.GET("/section/list", Books)
1414
Engine.GET("/section/{}", Books)
15+
Engine.GET("/syn", Syn)
1516
return Engine
1617
}

0 commit comments

Comments
 (0)