码农笔录博客源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.0 KiB

5 years ago
package utils
import (
"beeblog/models"
"context"
"fmt"
3 years ago
beego "github.com/beego/beego/v2/server/web"
5 years ago
"github.com/olivere/elastic"
3 years ago
"log"
"os"
5 years ago
"reflect"
"strconv"
3 years ago
"time"
5 years ago
)
4 years ago
5 years ago
var client *elastic.Client
4 years ago
5 years ago
const mapping = `
{
"settings":{
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings":{
"beeblog":{
"properties":{
"UserId":{
"type":"long"
},
"BlogHtml":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"Ctime":{
"type":"date"
},
"Title":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"Browses":{
"type":"long"
}
}
}
}
}`
3 years ago
func init() {
host, _ := beego.AppConfig.String("eshost")
var err error
client, err = elastic.NewClient(
elastic.SetURL(host),
elastic.SetSniff(false),
elastic.SetHealthcheckInterval(10*time.Second),
elastic.SetGzip(true),
elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)))
if err != nil {
panic(err)
}
}
5 years ago
4 years ago
func Index() {
5 years ago
ctx := context.Background()
exists, err := client.IndexExists("beeblog").Do(ctx)
if err != nil {
panic(err)
}
if !exists {
createIndex, err := client.CreateIndex("beeblog").BodyString(mapping).Do(ctx)
if err != nil {
panic(err)
}
if !createIndex.Acknowledged {
fmt.Println("请求未被接收到")
}
fmt.Println(createIndex)
} else {
fmt.Println("已经存在beeblog索引")
}
}
4 years ago
func ESSave(blog *models.Blog) {
5 years ago
ctx := context.Background()
4 years ago
id := strconv.FormatInt(blog.Id, 10)
5 years ago
_, err := client.Index().Index("beeblog").Type("beeblog").Id(id).BodyJson(blog).Do(ctx)
5 years ago
if err != nil {
5 years ago
fmt.Println(err)
return
5 years ago
}
}
4 years ago
5 years ago
//删除
func ESDelete(id string) {
5 years ago
_, err := client.Delete().Index("beeblog").
5 years ago
Type("beeblog").
Id(id).
Do(context.Background())
if err != nil {
println(err.Error())
return
}
}
4 years ago
func Search(key string) (*models.Blog, error) {
5 years ago
ctx := context.Background()
4 years ago
query := elastic.NewMultiMatchQuery(key, "Title", "BlogHtml")
5 years ago
searchResult, err := client.Search().
4 years ago
Index("beeblog"). // 指定index,返回一个*SearchService对象
5 years ago
//Type("beeblog").
4 years ago
Query(query). // 设置查询体,返回同一个*SearchService对象
5 years ago
//Sort("user", true). // 按照user升序排列
//From(0).Size(10). // 从第一条数据,找十条,即0-9
4 years ago
Pretty(true). // 使查询request和返回的结果格式美观
Do(ctx) // 返回一个*SearchResult
5 years ago
if err != nil {
4 years ago
println("search error", err.Error())
return nil, err
5 years ago
}
fmt.Printf("找到 [%d] 组tweets\n", searchResult.Hits.TotalHits)
// 查看匹配到多少组数据
fmt.Printf("找到 [%d] 组tweets\n", searchResult.TotalHits())
var typ models.Blog
for _, item := range searchResult.Each(reflect.TypeOf(typ)) { //从搜索结果中取数据的方法
t := item.(models.Blog)
fmt.Printf("%#v\n", t)
4 years ago
return &t, nil
5 years ago
}
if err != nil {
panic(err)
}
4 years ago
return nil, nil
}