@@ -0,0 +1,154 @@
|
||||
package mediamod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/elastic"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
)
|
||||
|
||||
var es *elastic.Client
|
||||
|
||||
const ESTable = models.ESMediaTable
|
||||
|
||||
func InitESIndex() {
|
||||
es = elastic.Init()
|
||||
var setting = elastic.M{
|
||||
"settings": elastic.M{
|
||||
"number_of_shards": elastic.NumberOfShards,
|
||||
"number_of_replicas": elastic.NumberOfReplicas,
|
||||
"analysis": elastic.M{
|
||||
"analyzer": elastic.M{
|
||||
"ik": elastic.M{
|
||||
"tokenizer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"mappings": elastic.M{
|
||||
"properties": elastic.M{
|
||||
"title": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkSmart,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"tagsName": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkSmart,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := es.CreateIndices(ESTable, setting); err != nil {
|
||||
panic(fmt.Sprintf("%s index indeices err ==>[%+v]", ESTable, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Search 根据关键字搜索
|
||||
func Search(keywords string, from int64, size int64) (data []ESMediaSource, err error) {
|
||||
es = elastic.Init()
|
||||
query := elastic.M{
|
||||
"query": elastic.M{
|
||||
"bool": elastic.M{
|
||||
"must": elastic.A{
|
||||
{"multi_match": elastic.M{
|
||||
"query": keywords,
|
||||
"fields": []string{"title"}},
|
||||
},
|
||||
{"terms": elastic.M{"status": []int{1}}},
|
||||
{"range": elastic.M{"playTime": elastic.M{"lt": 300}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
"sort": elastic.A{{"hot": elastic.M{"order": "desc"}}},
|
||||
"from": from,
|
||||
"size": size,
|
||||
}
|
||||
if err = es.Search(ESTable, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Search", table, "CommonSearch", err),
|
||||
log.Any("keywords", keywords),
|
||||
log.Any("from", from),
|
||||
log.Any("size", size))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SearchWithTotal(keywords string, from int64, size int64) (data ESVideoSourceWithTotal, err error) {
|
||||
es = elastic.Init()
|
||||
query := elastic.M{
|
||||
"query": elastic.M{
|
||||
"bool": elastic.M{
|
||||
"must": elastic.A{
|
||||
{"multi_match": elastic.M{
|
||||
"query": keywords,
|
||||
"fields": []string{"title"}},
|
||||
},
|
||||
{"terms": elastic.M{"status": []int{1, 3}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
"sort": elastic.A{{"hot": elastic.M{"order": "desc"}}},
|
||||
"from": from,
|
||||
"size": size,
|
||||
}
|
||||
if err = es.SearchWithTotal(ESTable, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Search", table, "CommonSearch", err),
|
||||
log.Any("keywords", keywords),
|
||||
log.Any("from", from),
|
||||
log.Any("size", size))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DirectSearch 用户根据关键字搜索
|
||||
func DirectSearch(keywords string, vidType string, from int64, size int64) (data []ESMediaSource, err error) {
|
||||
es = elastic.Init()
|
||||
query := elastic.M{
|
||||
"query": elastic.M{
|
||||
"bool": elastic.M{
|
||||
"must": elastic.A{
|
||||
{"multi_match": elastic.M{
|
||||
"query": keywords,
|
||||
"fields": []string{"title"}},
|
||||
},
|
||||
{"term": elastic.M{"mediaType.keyword": vidType}},
|
||||
{"term": elastic.M{"isDelete": false}},
|
||||
{"term": elastic.M{"status": 1}},
|
||||
},
|
||||
},
|
||||
},
|
||||
// "sort": elastic.A{{"hot": elastic.M{"order": "desc"}}},
|
||||
"from": from,
|
||||
"size": size,
|
||||
}
|
||||
if err = es.Search(ESTable, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Search", table, "CommonSearch", err),
|
||||
log.Any("keywords", keywords),
|
||||
log.Any("from", from),
|
||||
log.Any("size", size))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteByCond(filter elastic.M) (err error) {
|
||||
es = elastic.Init()
|
||||
if err = es.BulkDelete(ESTable, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Search", table, "CommonSearch", err), log.Any("filter", filter))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SearchByCondWithTotal(filter elastic.M) (data ESVideoSourceWithTotal, err error) {
|
||||
es = elastic.Init()
|
||||
if err = es.SearchWithTotal(ESTable, &data, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Search", table, "CommonSearch", err), log.Any("filter", filter))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user