|
@@ -240,7 +240,7 @@ func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-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
|
|
indexName := utils.IndexName
|
|
client := utils.Client
|
|
client := utils.Client
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
@@ -250,9 +250,15 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
keyWordArr = append(keyWordArr, keyWord)
|
|
keyWordArr = append(keyWordArr, keyWord)
|
|
keyWordLen = len(keyWordArr)
|
|
keyWordLen = len(keyWordArr)
|
|
}
|
|
}
|
|
|
|
+ //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
|
|
|
|
+ //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
|
|
mustMap := make([]interface{}, 0)
|
|
mustMap := make([]interface{}, 0)
|
|
shouldMap := make(map[string]interface{}, 0)
|
|
shouldMap := make(map[string]interface{}, 0)
|
|
shouldMapquery := make([]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 发布时间 "
|
|
// @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
|
|
//keyWordWeight := GetWeight(keyWordLen)
|
|
//keyWordWeight := GetWeight(keyWordLen)
|
|
var boost int
|
|
var boost int
|
|
@@ -263,6 +269,65 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
} else {
|
|
} else {
|
|
boost = 1
|
|
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 != "" {
|
|
if v != "" {
|
|
shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
"function_score": map[string]interface{}{
|
|
"function_score": map[string]interface{}{
|
|
@@ -316,20 +381,24 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
shouldMap = map[string]interface{}{
|
|
shouldMap = map[string]interface{}{
|
|
"should": shouldMapquery,
|
|
"should": shouldMapquery,
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ shouldNotMap = map[string]interface{}{
|
|
|
|
+ "should": shouldNotMapquery,
|
|
|
|
+ }
|
|
//排序
|
|
//排序
|
|
sortMap := make([]interface{}, 0)
|
|
sortMap := make([]interface{}, 0)
|
|
|
|
|
|
- sortMap = append(sortMap, map[string]interface{}{
|
|
|
|
- "_score": map[string]interface{}{
|
|
|
|
- "order": "desc",
|
|
|
|
- },
|
|
|
|
- })
|
|
|
|
//时间
|
|
//时间
|
|
sortMap = append(sortMap, map[string]interface{}{
|
|
sortMap = append(sortMap, map[string]interface{}{
|
|
"PublishDate": map[string]interface{}{
|
|
"PublishDate": map[string]interface{}{
|
|
"order": "desc",
|
|
"order": "desc",
|
|
},
|
|
},
|
|
})
|
|
})
|
|
|
|
+ //sortMap = append(sortMap, map[string]interface{}{
|
|
|
|
+ // "_score": map[string]interface{}{
|
|
|
|
+ // "order": "desc",
|
|
|
|
+ // },
|
|
|
|
+ //})
|
|
//高亮
|
|
//高亮
|
|
highlightMap := make(map[string]interface{}, 0)
|
|
highlightMap := make(map[string]interface{}, 0)
|
|
highlightMap = map[string]interface{}{
|
|
highlightMap = map[string]interface{}{
|
|
@@ -347,6 +416,10 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
mustMap = append(mustMap, map[string]interface{}{
|
|
mustMap = append(mustMap, map[string]interface{}{
|
|
"bool": shouldMap,
|
|
"bool": shouldMap,
|
|
})
|
|
})
|
|
|
|
+ mustNotMap = append(mustNotMap, map[string]interface{}{
|
|
|
|
+ "bool": shouldNotMap,
|
|
|
|
+ })
|
|
|
|
+
|
|
queryMap := map[string]interface{}{
|
|
queryMap := map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"bool": map[string]interface{}{
|
|
"bool": map[string]interface{}{
|
|
@@ -354,6 +427,17 @@ 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" {
|
|
if orderColumn == "Matching" {
|
|
queryMap["sort"] = sortMap
|
|
queryMap["sort"] = sortMap
|
|
}
|
|
}
|