Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
package esmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"videoweb/pub/esmanager"
|
|
"videoweb/pub/mongos"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var db *mongos.Collection
|
|
|
|
// Init 初始化数据
|
|
func Init(ctx context.Context) {
|
|
initDBIndex(ctx)
|
|
initIndex()
|
|
return
|
|
}
|
|
|
|
// initIndex 初始化索引
|
|
func initIndex() {
|
|
if esmanager.Instance == nil {
|
|
return
|
|
}
|
|
initVideoIndex(videoIndex)
|
|
}
|
|
|
|
// initDBIndex 初始化数据库索引
|
|
func initDBIndex(ctx context.Context) {
|
|
//获取数据库表
|
|
db = mongos.Shop.Coll(TN.ESRecord)
|
|
// 获取数据库表
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.ESRecord.EsIndex, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
}
|
|
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(esmod:initDBIndex) Create es_record Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
func initVideoIndex(esIndex string) {
|
|
var setting = esmanager.M{
|
|
"settings": esmanager.M{
|
|
"number_of_shards": esmanager.NumberOfShards,
|
|
"number_of_replicas": esmanager.NumberOfReplicas,
|
|
"analysis": esmanager.M{
|
|
"analyzer": esmanager.M{
|
|
"ik": esmanager.M{
|
|
"tokenizer": esmanager.AnalyzerIkSmart,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"mappings": esmanager.M{
|
|
"properties": esmanager.M{
|
|
FN.ESVideo.Title: esmanager.M{
|
|
"type": "text",
|
|
"analyzer": esmanager.AnalyzerIkMaxWord,
|
|
"search_analyzer": esmanager.AnalyzerIkSmart,
|
|
},
|
|
FN.ESVideo.MediaType: esmanager.M{
|
|
"type": "integer",
|
|
},
|
|
FN.ESVideo.Remark: esmanager.M{
|
|
"type": "text",
|
|
"analyzer": esmanager.AnalyzerIkSmart,
|
|
"search_analyzer": esmanager.AnalyzerIkSmart,
|
|
},
|
|
FN.ESVideo.Desc: esmanager.M{
|
|
"type": "text",
|
|
"analyzer": esmanager.AnalyzerIkSmart,
|
|
"search_analyzer": esmanager.AnalyzerIkSmart,
|
|
},
|
|
FN.ESVideo.Keyworld: esmanager.M{
|
|
"type": "text",
|
|
"analyzer": esmanager.AnalyzerIkSmart,
|
|
"search_analyzer": esmanager.AnalyzerIkSmart,
|
|
},
|
|
FN.ESVideo.CreatorName: esmanager.M{
|
|
"type": "text",
|
|
"analyzer": esmanager.AnalyzerIkSmart,
|
|
"search_analyzer": esmanager.AnalyzerIkSmart,
|
|
},
|
|
FN.ESVideo.Status: esmanager.M{
|
|
"type": "integer",
|
|
},
|
|
//FN.ESVideo.IsHot: esmanager.M{
|
|
// "type": "boolean",
|
|
//},
|
|
//FN.ESVideo.Sort: esmanager.M{
|
|
// "type": "integer",
|
|
//},
|
|
FN.ESVideo.WatchTimes: esmanager.M{
|
|
"type": "integer",
|
|
},
|
|
FN.ESVideo.ShowTimes: esmanager.M{
|
|
"type": "integer",
|
|
},
|
|
FN.ESVideo.Comments: esmanager.M{
|
|
"type": "integer",
|
|
},
|
|
FN.ESVideo.AddedTime: esmanager.M{
|
|
"type": "date",
|
|
"format": "strict_date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss",
|
|
},
|
|
//FN.ESVideo.CreatedAt: esmanager.M{
|
|
// "type": "date",
|
|
// "format": "strict_date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss",
|
|
//},
|
|
},
|
|
},
|
|
}
|
|
if err := esmanager.Instance.InitIndices(esIndex, setting); err != nil {
|
|
panic(fmt.Sprintf("ElasticSearch Index %s initMediaIndex error", esIndex))
|
|
}
|
|
}
|