|
@@ -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,25 +250,91 @@ 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
|
|
//lenkeyWordArr := len(keyWordArr)
|
|
//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 != "" {
|
|
if v != "" {
|
|
shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
"function_score": map[string]interface{}{
|
|
"function_score": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
//"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
//"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
"fields": []interface{}{"Title"},
|
|
"fields": []interface{}{"Title"},
|
|
"query": v,
|
|
"query": v,
|
|
},
|
|
},
|
|
@@ -279,7 +345,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
"function_score": map[string]interface{}{
|
|
"function_score": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
- //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
"fields": []interface{}{"Abstract"},
|
|
"fields": []interface{}{"Abstract"},
|
|
"query": v,
|
|
"query": v,
|
|
},
|
|
},
|
|
@@ -290,7 +356,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
"function_score": map[string]interface{}{
|
|
"function_score": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
- //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
"fields": []interface{}{"Annotation"},
|
|
"fields": []interface{}{"Annotation"},
|
|
"query": v,
|
|
"query": v,
|
|
},
|
|
},
|
|
@@ -303,6 +369,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
"query": map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
"multi_match": map[string]interface{}{
|
|
//"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
|
|
//"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
|
|
|
|
+ "boost": boost, //给查询的值赋予权重
|
|
"fields": []interface{}{"BodyText"},
|
|
"fields": []interface{}{"BodyText"},
|
|
"query": v,
|
|
"query": v,
|
|
},
|
|
},
|
|
@@ -314,14 +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{}{
|
|
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{}{
|
|
@@ -339,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{}{
|
|
@@ -346,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
|
|
}
|
|
}
|
|
@@ -354,6 +446,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
|
|
queryMap["highlight"] = highlightMap
|
|
queryMap["highlight"] = highlightMap
|
|
jsonBytes, _ := json.Marshal(queryMap)
|
|
jsonBytes, _ := json.Marshal(queryMap)
|
|
fmt.Println(string(jsonBytes))
|
|
fmt.Println(string(jsonBytes))
|
|
|
|
+ //utils.FileLog.Info(string(jsonBytes))
|
|
request := client.Search(indexName).Source(queryMap) // sets the JSON request
|
|
request := client.Search(indexName).Source(queryMap) // sets the JSON request
|
|
searchByMatch, err := request.Do(context.Background())
|
|
searchByMatch, err := request.Do(context.Background())
|
|
if searchByMatch != nil {
|
|
if searchByMatch != nil {
|