|
@@ -35,7 +35,7 @@ func NewClient() (client *elastic.Client, err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//创建文章阅读记录的Es索引
|
|
|
+// 创建文章阅读记录的Es索引
|
|
|
func CreateIndexNameArticleHistory() {
|
|
|
indexName := utils.IndexNameArticleHistory
|
|
|
mappingJson := `{
|
|
@@ -101,7 +101,7 @@ func CreateIndexNameArticleHistory() {
|
|
|
EsCreateIndex(indexName, mappingJson)
|
|
|
}
|
|
|
|
|
|
-//func UpdateWxUserLabel(cont context.Context) (err error) {
|
|
|
+// func UpdateWxUserLabel(cont context.Context) (err error) {
|
|
|
func AddAllArticleHistory(cont context.Context) (err error) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -261,7 +261,7 @@ func AddAllArticleHistory(cont context.Context) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//新增数据
|
|
|
+// 新增数据
|
|
|
func EsAddArticleHistoryData(item *models.EsUserInteraction) (err error) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -285,8 +285,8 @@ func EsAddArticleHistoryData(item *models.EsUserInteraction) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//indexName:索引名称
|
|
|
-//mappingJson:表结构
|
|
|
+// indexName:索引名称
|
|
|
+// mappingJson:表结构
|
|
|
func EsCreateIndex(indexName, mappingJson string) (err error) {
|
|
|
|
|
|
client := utils.Client
|
|
@@ -312,7 +312,7 @@ func EsCreateIndex(indexName, mappingJson string) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//新增和修改数据
|
|
|
+// 新增和修改数据
|
|
|
func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (err error) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -360,7 +360,7 @@ func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (e
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//新增和修改数据
|
|
|
+// 新增和修改数据
|
|
|
func EsAddOrEditDataV4(indexName, docId string, item *ElasticTestArticleDetailV4) (err error) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -409,7 +409,7 @@ func EsAddOrEditDataV4(indexName, docId string, item *ElasticTestArticleDetailV4
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//删除数据
|
|
|
+// 删除数据
|
|
|
func EsDeleteData(indexName, docId string) (err error) {
|
|
|
client := utils.Client
|
|
|
//if err != nil {
|
|
@@ -723,7 +723,7 @@ func EsSearchReport(indexName, keyWord string, startSize, pageSize, userId int)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//分页
|
|
|
+// 分页
|
|
|
func EsMultiMatchFunctionScoreQueryTimeSortPage(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
|
|
|
client := utils.Client
|
|
|
keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
|
|
@@ -887,42 +887,105 @@ func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func init23423() {
|
|
|
- EsArticleSearch("立高食品", 0, 10, "34")
|
|
|
-}
|
|
|
+//func init23423() {
|
|
|
+// EsArticleSearch("立高食品", 0, 10, "34")
|
|
|
+//}
|
|
|
|
|
|
-func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
|
|
|
+func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string, ikType int) (result []*models.SearchItem, total int64, err error) {
|
|
|
indexName := utils.IndexName
|
|
|
client := utils.Client
|
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
|
keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
- fmt.Println(keyWordArr)
|
|
|
keyWordLen := len(keyWordArr)
|
|
|
if keyWordLen <= 0 {
|
|
|
keyWordArr = append(keyWordArr, keyWord)
|
|
|
keyWordLen = len(keyWordArr)
|
|
|
}
|
|
|
- fmt.Println(keyWordArr)
|
|
|
+ //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
|
|
|
+ //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
|
|
|
mustMap := make([]interface{}, 0)
|
|
|
shouldMap := make(map[string]interface{}, 0)
|
|
|
- //shouldMapquery := make(map[string]interface{}, 0)
|
|
|
shouldMapquery := make([]interface{}, 0)
|
|
|
+
|
|
|
+ mustNotMap := make([]interface{}, 0)
|
|
|
+ shouldNotMap := make(map[string]interface{}, 0)
|
|
|
+ shouldNotMapquery := make([]interface{}, 0)
|
|
|
// @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
|
|
|
//keyWordWeight := GetWeight(keyWordLen)
|
|
|
- //var boost int
|
|
|
+ var boost int
|
|
|
//lenkeyWordArr := len(keyWordArr)
|
|
|
- for _, v := range keyWordArr {
|
|
|
- //if k == 0 {
|
|
|
- // boost = 2 * 1000
|
|
|
- //} else {
|
|
|
- // boost = 1000
|
|
|
- //}
|
|
|
+ for k, v := range keyWordArr {
|
|
|
+ if k == 0 {
|
|
|
+ boost = 2 * 1000
|
|
|
+ } else {
|
|
|
+ boost = 1
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果是 2:查询除了查询键入词之外的联想词
|
|
|
+ if k == 0 && ikType == 2 {
|
|
|
+ if v != "" {
|
|
|
+ shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
|
|
|
+ "function_score": map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "multi_match": map[string]interface{}{
|
|
|
+ //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
+ "fields": []interface{}{"Title"},
|
|
|
+ "query": v,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+ shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
|
|
|
+ "function_score": map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "multi_match": map[string]interface{}{
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
+ "fields": []interface{}{"Abstract"},
|
|
|
+ "query": v,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+ shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
|
|
|
+ "function_score": map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "multi_match": map[string]interface{}{
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
+ "fields": []interface{}{"Annotation"},
|
|
|
+ "query": v,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
|
|
|
+ "function_score": map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "multi_match": map[string]interface{}{
|
|
|
+ //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
+ "fields": []interface{}{"BodyText"},
|
|
|
+ "query": v,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //如果是 1:查询键入词
|
|
|
+ if k > 0 && ikType == 1 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
if v != "" {
|
|
|
shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
|
"function_score": map[string]interface{}{
|
|
|
"query": map[string]interface{}{
|
|
|
"multi_match": map[string]interface{}{
|
|
|
//"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
"fields": []interface{}{"Title"},
|
|
|
"query": v,
|
|
|
},
|
|
@@ -933,7 +996,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
"function_score": map[string]interface{}{
|
|
|
"query": map[string]interface{}{
|
|
|
"multi_match": map[string]interface{}{
|
|
|
- //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
"fields": []interface{}{"Abstract"},
|
|
|
"query": v,
|
|
|
},
|
|
@@ -944,18 +1007,20 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
"function_score": map[string]interface{}{
|
|
|
"query": map[string]interface{}{
|
|
|
"multi_match": map[string]interface{}{
|
|
|
- //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
"fields": []interface{}{"Annotation"},
|
|
|
"query": v,
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
})
|
|
|
+
|
|
|
shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
|
"function_score": map[string]interface{}{
|
|
|
"query": map[string]interface{}{
|
|
|
"multi_match": map[string]interface{}{
|
|
|
//"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
|
"fields": []interface{}{"BodyText"},
|
|
|
"query": v,
|
|
|
},
|
|
@@ -967,8 +1032,13 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
shouldMap = map[string]interface{}{
|
|
|
"should": shouldMapquery,
|
|
|
}
|
|
|
+
|
|
|
+ shouldNotMap = map[string]interface{}{
|
|
|
+ "should": shouldNotMapquery,
|
|
|
+ }
|
|
|
//排序
|
|
|
sortMap := make([]interface{}, 0)
|
|
|
+
|
|
|
//时间
|
|
|
sortMap = append(sortMap, map[string]interface{}{
|
|
|
"PublishDate": map[string]interface{}{
|
|
@@ -992,6 +1062,10 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
mustMap = append(mustMap, map[string]interface{}{
|
|
|
"bool": shouldMap,
|
|
|
})
|
|
|
+ mustNotMap = append(mustNotMap, map[string]interface{}{
|
|
|
+ "bool": shouldNotMap,
|
|
|
+ })
|
|
|
+
|
|
|
queryMap := map[string]interface{}{
|
|
|
"query": map[string]interface{}{
|
|
|
"bool": map[string]interface{}{
|
|
@@ -999,14 +1073,25 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
},
|
|
|
},
|
|
|
}
|
|
|
+ //把第一次键入词的筛选条件过滤掉
|
|
|
+ if ikType == 2 {
|
|
|
+ queryMap = map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "bool": map[string]interface{}{
|
|
|
+ "must": mustMap,
|
|
|
+ "must_not": mustNotMap,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
if orderColumn == "Matching" {
|
|
|
queryMap["sort"] = sortMap
|
|
|
}
|
|
|
queryMap["from"] = startSize
|
|
|
queryMap["size"] = pageSize
|
|
|
queryMap["highlight"] = highlightMap
|
|
|
- //jsonBytes, _ := json.Marshal(queryMap)
|
|
|
- //fmt.Println(string(jsonBytes))
|
|
|
+ jsonBytes, _ := json.Marshal(queryMap)
|
|
|
+ fmt.Println(string(jsonBytes))
|
|
|
//utils.FileLog.Info(string(jsonBytes))
|
|
|
request := client.Search(indexName).Source(queryMap) // sets the JSON request
|
|
|
searchByMatch, err := request.Do(context.Background())
|
|
@@ -1025,18 +1110,8 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
}
|
|
|
searchItem := new(models.SearchItem)
|
|
|
searchItem.ArticleId, _ = strconv.Atoi(v.Id)
|
|
|
- if len(v.Highlight["BodyText"]) > 0 || len(v.Highlight["Abstract"]) > 0 || len(v.Highlight["Annotation"]) > 0 {
|
|
|
+ if len(v.Highlight["BodyText"]) > 0 {
|
|
|
searchItem.Body = v.Highlight["BodyText"]
|
|
|
- if len(v.Highlight["Abstract"]) > 0 {
|
|
|
- for _, vRed := range v.Highlight["Abstract"] {
|
|
|
- searchItem.Body = append(searchItem.Body, vRed)
|
|
|
- }
|
|
|
- }
|
|
|
- if len(v.Highlight["Annotation"]) > 0 {
|
|
|
- for _, vRed := range v.Highlight["Annotation"] {
|
|
|
- searchItem.Body = append(searchItem.Body, vRed)
|
|
|
- }
|
|
|
- }
|
|
|
} else {
|
|
|
bodyRune := []rune(article.BodyText)
|
|
|
bodyRuneLen := len(bodyRune)
|
|
@@ -1068,9 +1143,5 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
|
}
|
|
|
total = searchByMatch.Hits.TotalHits.Value
|
|
|
}
|
|
|
- //fmt.Println(result)
|
|
|
- //for _, v := range result {
|
|
|
- // fmt.Println(v)
|
|
|
- //}
|
|
|
return
|
|
|
}
|